牛客网暑期ACM多校训练营(第一场)D.Two Graphs(图的同构)

链接: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.

题意:

  n个点,m1条边的图E1,n个点,m2条边的图E2。求图E2有多少子图跟图E1同构。

题解:

  用STL的全排列函数next_permutation()枚举映射。对于每一种映射枚举每一条边判断合法性。

  总情况数要除以图E1的自同构数去重。

//Sinhaeng Hhjian
#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main(){
	int p[10], n, m1, m2, u, v, a[10][10], b[10][10];
	while(~scanf("%d%d%d", &n, &m1, &m2)){
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		int ans=0, d=0;
		for(int i=1;i<=m1;i++){
			scanf("%d%d", &u, &v);
			a[u][v]=a[v][u]=1;
		}
		for(int i=1;i<=m2;i++){
			scanf("%d%d", &u, &v);
			b[u][v]=b[v][u]=1;
		}
		for(int i=1;i<=8;i++)
			p[i]=i;
		do{
			int flag=1;
			for(int i=1;i<=n && flag;i++)
				for(int j=1;j<=n && flag;j++)
					if(a[i][j] && !a[p[i]][p[j]])
						flag=0;
			if(flag)
				d++;
		}while(next_permutation(p+1, p+n+1));
		do{
			int flag=1;
			for(int i=1;i<=n && flag;i++)
				for(int j=1;j<=n && flag;j++)
					if(a[i][j] && !b[p[i]][p[j]])
						flag=0;
			if(flag)
				ans++;
		}while(next_permutation(p+1, p+n+1));
		printf("%d\n", ans/d);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhjian6666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值