牛客网暑期ACM多校训练营(第一场) - D Two Graphs

题目

 

题目描述 

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值