牛客多校第一场 D.Two Graphs 暴力全排列+hash图去重

链接: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;
}

猜你喜欢

转载自blog.csdn.net/swust5120166213/article/details/81155402
今日推荐