链接:https://www.nowcoder.com/acm/contest/139/D
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
Two undirected simple graphs
and
where
are isomorphic when there exists a bijection
on V satisfying
if and only if {x, y} ∈ E
2.
Given two graphs
and
, count the number of graphs
satisfying the following condition:
*
.
* G 1 and G are isomorphic.
Given two graphs
*
* G 1 and G are isomorphic.
输入描述:
The input consists of several test cases and is terminated by end-of-file.1
The first line of each test case contains three integers n, m
and m2
where |E1
| = m1
and |E2
| = m2
.1
The i-th of the following m
lines contains 2 integers ai
and bi
which denote {ai
, bi
} ∈ E1
.2
The i-th of the last m
lines contains 2 integers ai
and bi
which denote {ai
, bi
} ∈ E2
.
输出描述:
For each test case, print an integer which denotes the result.
备注:
* 1 ≤ n ≤ 8
*
i
* 1 ≤ a
, bi
≤ n
* The number of test cases does not exceed 50.
图的同构的概念: 离散数学:图的同构
因为n的范围较小,所以直接全排列枚举映射,对图中的每一条边映射一个值,这样的一组集合就能代表一种图的同构
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> p; typedef long long ll; map<p,int>mp1; map<p,int>mp2; map<int,int>mp; set<int>se; const int mod=1e6+7; const int N=1e6+10; int a[10]; int main(){ int n,m1,m2,x,y; while(~scanf("%d%d%d",&n,&m1,&m2)){ mp1.clear();mp2.clear();mp.clear();se.clear(); for(int i=1;i<=m1;i++){ scanf("%d%d",&x,&y);mp1[{x,y}]=i;se.insert(x);se.insert(y); } for(int i=1;i<=m2;i++){ scanf("%d%d",&x,&y);mp2[{x,y}]=i; } int ans=0; for(int i=1;i<=n;i++) a[i]=i; set<ll>ss;ss.clear(); do{ mp.clear(); int flag=1; for(int i=1;i<=n;i++) mp[i]=a[i];ll tmp=0; for(auto it=mp1.begin();it!=mp1.end();it++){ p a=it->first;p b1={mp[a.first],mp[a.second]}; p b2={mp[a.second],mp[a.first]}; if(mp2[b1]!=0){ tmp+=(1<<mp2[b1]);} else if(mp2[b2]!=0){tmp+=(1<<mp2[b2]);} else flag=0; } if(flag) ss.insert(tmp); }while(next_permutation(a+1,a+n+1)); printf("%d\n",ss.size()); } return 0; }