链接: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
题意:找出一个图与另一个图同构的子图个数
思路: 要判断是否同构,就需要把原图映射一个成新图,这个新图相对结构不变,就只有图的编号的顺序被打乱了而已,然后在与另一个图去一一比较边的对应位置,我们这里因为点是一样的,所以我们先去确定点的对应顺序,然后再去判断是否边一一对应,全排列即可,然后去下重就行
accode
#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define ULL unsigned long long
using namespace std;
const int maxn = 10;
int n,m1,m2;
int va[10];
int ma1[10][10];
int ma2[10][10];
int main()
{
while(~scanf("%d%d%d",&n,&m1,&m2))
{
memset(ma1,0,sizeof(ma1));
memset(ma2,0,sizeof(ma2));
for(int i = 1; i<=n; i++)
{
va[i] = i;
}
for(int i = 0; i<m1; i++)
{
int u,v;
scanf("%d%d",&u,&v);
ma1[u][v] = 1;
ma1[v][u] = 1;
}
for(int i = 0; i<m2; i++)
{
int u,v;
scanf("%d%d",&u,&v);
ma2[u][v] = 1;
ma2[v][u] = 1;
}
set<ULL>S;
do
{
ULL tmp = 0;
int falg = 1;
for(int i = 1; i<=n; i++)
{
for(int j = i+1;j<=n;j++){
if(ma1[i][j]==1&&ma2[va[i]][va[j]] == 1){
int xx = va[i];
int yy = va[j];
if(xx>yy){
swap(xx,yy);
}
tmp |= ((ULL)1<<(xx*n+yy));
}
else if(ma1[i][j]==1&&ma2[va[i]][va[j]]==0){
falg = 0;
break;
}
}
if(falg==0){
break;
}
}
if(falg){
// cout<<tmp<<endl;
S.insert(tmp);
}
}while(next_permutation(va+1,va+1+n));
cout<<S.size()<<endl;
}
}