链接: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.
题意:给出两张图G1,G2,问G2有多少个子图与G1是同构的
思路:最多只有8个点,所以可以把所有的映射关系一一枚举出来,然后在判断是否与G1同构,G1中有的每一条边,该边的两个点所映射过去G2的两个点也必须存在一条边,因为可能有重复的情况,用了状压的思想来去重
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
int mp1[10][10],mp2[10][10];
int a[10];
map<int,int> mp;
int main()
{
int n,m1,m2;
while(scanf("%d%d%d",&n,&m1,&m2) != EOF) {
for(int i = 1; i <= n; i++) a[i] = i;
mp.clear();
memset(mp1,0,sizeof(mp1));
memset(mp2,0,sizeof(mp2));
int x,y;
for(int i = 1; i <= m1; i++) {
scanf("%d%d",&x,&y);
mp1[x][y] = mp1[y][x] = 1;
}
for(int i = 1; i <= m2; i++) {
scanf("%d%d",&x,&y);
mp2[x][y] = mp2[y][x] = i;
}
int ans = 0;
do {
int flag = 1,b = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(mp1[i][j]) {
if(mp2[a[i]][a[j]] == 0) flag = 0;
else b |= 1 << mp2[a[i]][a[j]];
}
}
}
if(flag == 1 && mp[b] == 0) {
mp[b] = 1;
ans++;
}
}while(next_permutation(a + 1,a + 1 + n));
printf("%d\n",ans);
}
return 0;
}