链接: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.
两个图点的个数相同,问图二的子图有多少个是图一的同构。
思路:
数据量比较小直接暴力搜索。通过查找边,记录边的路径,看能否找到同构。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=10;
int n,m1,m2;
int look[N][N],vis[N][N],pre[N],w[N],ans;
map<ll,int>mp;
struct Node
{
int x,y;
}node[N];
void dfs(int rt)
{
ll pos=0,i;
if(rt==n+1)
{
for(i=1;i<=m1;i++)//查找图一的每一条边是否都能在图二中找到映射
{
if(vis[pre[node[i].x]][pre[node[i].y]]==0)
return;
pos|=(1<<vis[pre[node[i].x]][pre[node[i].y]]);//记录路径
}
if(mp[pos])
return;
mp[pos]=1;
ans++;
return;
}
for(int i=1;i<=n;i++)//搜索图二中的每一个点
{
if(w[i])
continue;
pre[rt]=i;
w[i]=1;
dfs(rt+1);
w[i]=0;
}
}
int main()
{
int i,j;
while(~scanf("%d%d%d",&n,&m1,&m2))
{
mp.clear();
memset(look,0,sizeof(look));
memset(vis,0,sizeof(vis));
memset(w,0,sizeof(w));
ans=0;
for(i=1;i<=n;i++)
pre[i]=i;//两个图点之间的映射关系
for(i=1;i<=m1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
look[x][y]=look[y][x]=1;
node[i].x=x;
node[i].y=y;
}
for(i=1;i<=m2;i++)
{
int x,y;
scanf("%d%d",&x,&y);
vis[x][y]=vis[y][x]=i;
}
dfs(1);
printf("%d\n",ans);
}
return 0;
}