D - Two Graphs(图同构)

Two undirected simple graphs G1=⟨V,E1⟩ and G2=⟨V,E2⟩ where V={1,2,⋯,n} are isomorphic when there exists a bijection ϕ on V satisfying {ϕ(x),ϕ(y)}∈E1 if and only if {x,y}∈E2.

Given two graphs G1=⟨V,E1⟩ and G2=⟨V,E2⟩, count the number of graphs G=⟨V,E⟩ satisfying the following condition:

  • E⊆E2.

  • G1 and G are isomorphic.

Input
The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains three integers n, m1 and m2 where |E1|=m1 and |E2|=m2.

The i-th of the following m1 lines contains 2 integers ai and bi which denote {ai,bi}∈E1.

The i-th of the last m2 lines contains 2 integers ai and bi which denote {ai,bi}∈E2.

  • 1≤n≤8
  • 1≤m1≤m2≤n(n−1)2
  • 1≤ai,bi≤n
  • The number of test cases does not exceed 50.

Output
For each test case, print an integer which denotes the result.

Example
Input
3 1 2
1 3
1 2
2 3
4 2 3
1 2
1 3
4 1
4 2
4 3
Output
2
3
找图E1的多少子图和E2同构,直接枚举,注意去掉E1自同构的数量

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m1,m2,x,y,flag,a[10],g1[10][10],g2[10][10];
int fin(int X[10][10],int Y[10][10])//找图X和图Y的同构数 
{
    
    
	for(int i=1;i<=n;i++) a[i]=i;
    int ans=0;
    do{
    
    
        flag=1;
        for(int i=1;i<=n;i++)
		{
    
    
         for(int j=1;j<=n;j++)
		   if(X[i][j]==1&&Y[a[i]][a[j]]==0)//有一条边连接情况不同,就不是同构 
			{
    
    
                flag=0;break;
            }
            if(!flag) break;
        }
            ans+=flag;
    }while(next_permutation(a+1,a+n+1));
    return ans;
}
int main()
{
    
    
    while(scanf("%d%d%d",&n,&m1,&m2)==3)
	{
    
    
        memset(g1,0,sizeof(g1));
        memset(g2,0,sizeof(g2));
        for(int i=0;i<m1;i++)
		{
    
    
            scanf("%d%d",&x,&y);
            g1[x][y]=g1[y][x]=1;
        }
        for(int i=0;i<m2;i++)
		{
    
    
            scanf("%d%d",&x,&y);
            g2[x][y]=g2[y][x]=1;
        }
        int ans=fin(g1,g2),sum=fin(g1,g1);
        printf("%d\n",ans/sum);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_43540515/article/details/113109431
今日推荐