链接:https://www.nowcoder.com/acm/contest/139/D
来源:牛客网
题目描述
Two undirected simple graphs and where are isomorphic when there exists a bijection on V satisfying if and only if {x, y} ∈ E2.
Given two graphs and , count the number of graphs satisfying the following condition:
* .
* G1 and G are isomorphic.
输入描述:
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.
输出描述:
For each test case, print an integer which denotes the result.
示例1
输入
复制
3 1 2 1 3 1 2 2 3 4 2 3 1 2 1 3 4 1 4 2 4 3
输出
复制
2 3
备注:
* 1 ≤ n ≤ 8 * * 1 ≤ ai, bi ≤ n * The number of test cases does not exceed 50.
题目大意:有两个图,,要求出满足如下条件的的个数:
1、
2、图G与图G1同构
题目思路:由于图中只有8个点,所以我们可以考虑枚举G2的所有的图的情况,再用邻接矩阵来进行判断两个图是否同构,最后还需要减掉G1与G1同构的情况,就是得到最终答案。
#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<"["<<x<<"]"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
const int mod=1e9+7;
int n,m1,m2;
int a[10];
int mp1[10][10],mp2[10][10];
int main(){
while(~scanf("%d%d%d",&n,&m1,&m2)){
memset(mp1,0,sizeof(mp1));
memset(mp2,0,sizeof(mp2));
for(int i=1;i<=n;i++) a[i]=i;
int ans=0,res=0;
for(int i=0;i<m1;i++){
int u,v;
scanf("%d%d",&u,&v);
mp1[u][v]=mp1[v][u]=1;
}
for(int i=0;i<m2;i++){
int u,v;
scanf("%d%d",&u,&v);
mp2[u][v]=mp2[v][u]=1;
}
do{
int flag=1;
for(int i=1;i<=n;i++){
if(!flag) break;
for(int j=1;j<=n;j++){
if(mp1[i][j]==1 && mp2[a[i]][a[j]]==0){
flag=0;
break;
}
}
}
ans+=flag;
}while(next_permutation(a+1,a+n+1));
for(int i=1;i<=n;i++) a[i]=i;
do{
int flag=1;
for(int i=1;i<=n;i++){
if(!flag) break;
for(int j=1;j<=n;j++){
if(mp1[i][j]==1 && mp1[a[i]][a[j]]==0){
flag=0;
break;
}
}
}
res+=flag;
}while(next_permutation(a+1,a+n+1));
printf("%d\n",ans/res);
}
return 0;
}