牛客多校第一場 D.Two Graphs 暴力全排列+hash圖去重
阿新 • • 發佈:2018-12-22
連結: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個點給你兩個圖,圖E1有m1條邊,一個圖E2有m2條邊,問你E2能全部對映到E1的方案數,一個點不同則一個對映也不同,
題解:有兩種解法,每種解法都是全排列暴力列舉那個E1對應了E2那個點,具體看程式碼。第一種是E1可以對映到E2 a1次,E1可以對映到E1 a2次那麼答案一定重複了a2次,答案就是a1/a2。第二種方法就是記錄每一次全排列中可以成功的對映下來的圖的hash值,然後去重就可以了,對於圖的hash,我深深的感到了世界的惡意,我用類似字串的雙hash都過不了,我不知道這個世界怎麼了,一定要用 t+=(ull)1<<(11*大標號+小標號) (大小標號先後順序無影響,本題大小標號不區分也可以過),可以才過了太尼瑪絕望了,程式碼附上我嘗試的多種hash去重方法!
解法一:
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=1e5+5;
const int inf=2e9+1e8+1234;
const ll linf=8e18+9e17;
const int mod=1e9+7;
const double e=exp(1.0);
const double pi=acos(-1);
bool mp1[10][10],mp2[10][10];
int a[10];
int main()
{
int n,m1,m2;
while(scanf("%d%d%d",&n,&m1,&m2)!=EOF)
{
memset(mp1,0,sizeof mp1);
memset(mp2,0,sizeof mp2);
int st,en;
for(int i=0;i<m1;i++){
scanf("%d%d",&st,&en);
mp1[st][en]=1;
mp1[en][st]=1;
}
for(int i=0;i<m2;i++){
scanf("%d%d",&st,&en);
mp2[st][en]=1;
mp2[en][st]=1;
}
int a1=0,a2=0;
for(int i=1;i<=n;i++)a[i]=i;
do{
int f=1;
for(int i=1;i<=n&&f;i++)
for(int j=1;j<=n&&f;j++)
if(mp1[i][j]&&!mp2[a[i]][a[j]])f=0;
a1+=f;
}while(next_permutation(a+1,a+n+1));
for(int i=1;i<=n;i++)a[i]=i;
do{
int f=1;
for(int i=1;i<=n&&f;i++)
for(int j=1;j<=n&&f;j++)
if(mp1[i][j]&&!mp1[a[i]][a[j]])f=0;
a2+=f;
}while(next_permutation(a+1,a+n+1));
printf("%d\n",a1/a2);
}
return 0;
}
解法二:
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=1e5+5;
const int base=233;
const int inf=2e9+1e8+1234;
const ll linf=8e18+9e17;
const int mod=1e9+7;
const double e=exp(1.0);
const double pi=acos(-1);
int mp1[10][10],mp2[10][10],a[10];
struct node{
ull a,b;
bool operator = (const node &t)const{
if(a==t.a&&b==t.b)return 1;
else return 0;
}
bool operator < (const node &t)const{
if(a==t.a)return b<t.b;
else return a<t.a;
}
}ans[maxn];
bool cmp(node a,node b)
{
if(a.a==b.a)return a.b>b.b;
return a.a>b.a;
}
int main()
{
int n,m1,m2;
while(scanf("%d%d%d",&n,&m1,&m2)!=EOF)
{
memset(mp1,0,sizeof mp1);
memset(mp2,0,sizeof mp2);
int st, en;
for(int i=0;i<m1;i++)
{
scanf("%d%d",&st,&en);
mp1[st][en]=1;
mp1[en][st]=1;
}
for(int i=0;i<m2;i++)
{
scanf("%d%d",&st,&en);
mp2[st][en]=1;
mp2[en][st]=1;
}
for(int i=1;i<=n;i++)a[i]=i;
set<ull>s;
set<node>ss;
int cnt=0;
do{
ull t=1;
ull t1=1,t2=1;
int f=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(mp1[i][j]&&mp2[a[i]][a[j]]){
st=a[i],en=a[j];
//if(st>en)swap(st,en);
t+=((ull)1<<(st*11+en));
t1*=(ull)(st*11+en);
t2*=(ull)(st+13*en);
}
else if(mp1[i][j]&&!mp2[a[i]][a[j]])f=0;
if(!f)break;
}
if(f)s.insert(t),ans[cnt].a=t1,ans[cnt++].b=t2,ss.insert((node){t1,t2});
}while(next_permutation(a+1,a+1+n));
sort(ans,ans+cnt,cmp);
int num=0;
for(int i=1;i<cnt;i++)
{
if(ans[i].a!=ans[num].a||ans[i].b!=ans[num].b)ans[++num]=ans[i];
}
//printf("%d\n",num+1);
printf("%d\n",s.size());
}
return 0;
}