链接: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.
题意:问B的子图有多少个和A同构;
思路:首先明白什么是同构,对于图的话,简单来说,节点编号变但是图形没变,可以称这两个图同构;那么我们直接对节点进行全排列,然后遍历m1条边,看两个点构成的边是否在B中存在,当然这个过程中还会有A的自同构(边的两个节点编号没变成别的数,比如顶点互换),除去就好了;
下面附上代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define long long LL
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define lson o<<1
#define rson o<<1|1
using namespace std;
int g1[100][100];
int g2[100][100];
int u[100], v[100];
int a[10];
int main()
{
int n, m1, m2;
while(cin >> n >> m1 >> m2)
{
int x, y;
memset(a, 0, sizeof(a));
memset(g1, 0, sizeof(g1));
memset(g2, 0, sizeof(g2));
for(int i = 0; i < m1; i++)
{
scanf("%d %d",&u[i], &v[i]);
g1[u[i]][v[i]] = g1[v[i]][u[i]] = 1;
}
for(int i = 0; i < m2; i++)
{
scanf("%d %d", &x, &y);
g2[x][y] = g2[y][x] = 1;
}
for(int i = 1; i <= n; i++)
a[i] = i;
int ams = 0;int ans = 0;
do{
int ans1 = 1, ans2 = 1;
for(int i = 0; i < m1; i++)
{
x = a[u[i]];
y = a[v[i]];
if(!g1[x][y])//没有自同构
ans1 = 0;
if(!g2[x][y])//不同构
ans2 = 0;
}
ans += ans1;
ams += ans2;
}while(next_permutation(a + 1, a + n + 1));
printf("%d\n", ams/ans);
}
return 0;
}