链接: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.
题目大意:给两个图,判断图2有多少个子图与图1同构
用全排列枚举所有的情况,然后还要算一遍自同构,也就是说如 两个图都是 1-2,1-3,看上去是有两种映射方式,但实际上我们是算成一种
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10;
int mapf[maxn][maxn];
int maps[maxn][maxn];
int num[maxn];
int main()
{
int n,m1,m2;
while(~scanf("%d%d%d",&n,&m1,&m2))
{
memset(mapf,0,sizeof(mapf));
memset(maps,0,sizeof(maps));
for(int i=0;i<m1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mapf[u][v]=mapf[v][u]=1;
}
for(int i=0;i<m2;i++)
{
int u,v;
scanf("%d%d",&u,&v);
maps[u][v]=maps[v][u]=1;
}
for(int i=1;i<=n;i++)
{
num[i]=i;
}
int ans=0;
do
{
int flag=1;
for(int i=1;i<=n;i++)
{
if(!flag) break;
for(int j=1;j<=n;j++)
{
if(mapf[i][j]==1&&maps[num[i]][num[j]]==0)
{
flag=0;
break;
}
}
}
ans+=flag;
}while(next_permutation(num+1,num+n+1));
for(int i=1;i<=n;i++)
{
num[i]=i;
}
int res=0;
do
{
int flag=1;
for(int i=1;i<=n;i++)
{
if(!flag) break;
for(int j=1;j<=n;j++)
{
if(mapf[i][j]==1&&mapf[num[i]][num[j]]==0)
{
flag=0;
break;
}
}
}
res+=flag;
}while(next_permutation(num+1,num+n+1));
cout<<ans/res<<endl;
}
return 0;
}