链接: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.
输入
3 1 2
1 3
1 2
2 3
4 2 3
1 2
1 3
4 1
4 2
4 3
输出
2
3
题意求G2的子图与G1 的同构的个数
方法:暴力
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const int maxn=1e2+5;
const int INF=0x3f3f3f3f;
bool vis[maxn];
int n;
int m1,m2;
map<long long,int> mp;
struct edge
{
int from,to;
}d1[maxn],d2[maxn];
int G1[maxn][maxn];
int G2[maxn][maxn];
int a[maxn];
int ans;
int main()
{
while(~scanf("%d%d%d",&n,&m1,&m2))
{
memset(G1,0,sizeof G1);
memset(G2,0,sizeof G2);
mp.clear();
int u,v;
for(int i=1;i<=m1;i++)
scanf("%d%d",&u,&v),G1[u][v]=G1[v][u]=1;
for(int i=1;i<=m2;i++)
scanf("%d%d",&u,&v),G2[u][v]=G2[v][u]=i;//给每个边打上编号,便于后面去重
for(int i=1;i<=n;i++) a[i]=i;
ans=0;
do
{
int flag=0;
long long x=0;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(G1[i][j])//把一个点a[i],a[j](经过排序之后)映射到边G1[i][j]上去
{
if(G2[a[i]][a[j]]==0) flag=1;//G2中点a[i],a[j]直接没有边
else x|=1<<G2[a[i]][a[j]];//x是每个边的id与运算的结果
//如果一个子图中两条边是一样的,那么就是重复的情况,就要保证每个子图的id不能全一样
}
if(flag||mp.count(x)) continue;
mp[x]=1;
ans++;
}while(next_permutation(a+1,a+1+n));//枚举点的全排列
printf("%d\n",ans);
}
return 0;
}