题目描述
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.
题意:
给你一个小图,一个大图。
问你在大图中能找到多少个形状和小图一样的。
POINT:
对每一个点遍历一个映射,总共n!种。
比如小图里的1对应大图里的2, 小图2对应大图3.这样去暴力枚举映射。可以开一个数组来进行全排列。
然后小图里有边的,去看看大图中对应的点也有没有边。
当全都满足了。我们把对应的边进行状态压缩成一个状态。然后去判重就可以了。
因为每种答案的用的边的状态肯定是不一样的。
问题就相当于,在大图中挑出和【小图一样形状的边】,问你能挑几种。
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<map>
#include<string.h>
#include<algorithm>
using namespace std;
int a[10];
int mp1[11][11];
int mp2[11][11];
map<long long,int> p;
int main()
{
int n,m1,m2;
while(~scanf("%d%d%d",&n,&m1,&m2)){
memset(mp1,0,sizeof mp1);
memset(mp2,0,sizeof mp2);
p.clear();
for(int i=1;i<=m1;i++){
int u,v;scanf("%d%d",&u,&v);
mp1[u][v]=mp1[v][u]=1;
}
for(int i=1;i<=m2;i++){
int u,v;scanf("%d%d",&u,&v);
mp2[u][v]=mp2[v][u]=i;
}
for(int i=1;i<=n;i++)
a[i]=i;
int ans=0;
do
{
int flag=1;
long long to=0;
for(int i=1;i<=n&&flag;i++){
for(int j=1;j<=n&&flag;j++){
if(mp1[i][j]==1){
if(mp2[a[i]][a[j]]==0)
flag=0;
to|=1LL<<(mp2[a[i]][a[j]]);
}
}
}
if(flag&&p[to]==0){
ans++;
p[to]=1;
}
}while(next_permutation(a+1,a+1+n));
cout<<ans<<endl;
}
return 0;
}