5
25
2016
1

[51Nod1676] 无向图同构

首先树同构我是写过的……就是BJOI那题

然后图同构我是不会的……

然后就花了点头盾看了题解。

原来和树Hash差不多,只要进行迭代就好了

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int M=4005,N=205;
const long long P1=808080808080803ll,P2=888888888888887ll;

int n,m,hA[N],hB[N],enA,enB,test;
long long valA_1[N],valB_1[N],valA_2[N],valB_2[N],tmp1[N],tmp2[N],Vt1[N],Vt2[N];

struct Edge{
int b,next;
}eA[M<<1],eB[M<<1];

void AddEdgeA(int sa,int sb){
eA[++enA].b=sb;
eA[enA].next=hA[sa];
hA[sa]=enA;
}

void AddEdgeB(int sa,int sb){
eB[++enB].b=sb;
eB[enB].next=hB[sa];
hB[sa]=enB;
}

void Solve_A(){
for(int i=1;i<=n;i++){
	int cnt=0;
	for(int j=hA[i];j;j=eA[j].next){
		int v=eA[j].b;cnt++;
		tmp1[cnt]=valA_1[v];
		tmp2[cnt]=valA_2[v];
	}
	sort(tmp1+1,tmp1+cnt+1);
	sort(tmp2+1,tmp2+cnt+1);
	long long Ha1=0,Ha2=0;
    for(int j=1;j<=cnt;j++){Ha1=(Ha1*233ll+tmp1[j])%P1;Ha2=(Ha2*666ll+tmp2[j])%P2;}
    Vt1[i]=Ha1;Vt2[i]=Ha2;
}
for(int i=1;i<=n;i++){valA_1[i]=Vt1[i];valA_2[i]=Vt2[i];}
}

void Solve_B(){
for(int i=1;i<=n;i++){
	int cnt=0;
	for(int j=hB[i];j;j=eB[j].next){
		int v=eB[j].b;cnt++;
		tmp1[cnt]=valB_1[v];
		tmp2[cnt]=valB_2[v];
	}
	sort(tmp1+1,tmp1+cnt+1);
	sort(tmp2+1,tmp2+cnt+1);
	long long Ha1=0,Ha2=0;
    for(int j=1;j<=cnt;j++){Ha1=(Ha1*233ll+tmp1[j])%P1;Ha2=(Ha2*666ll+tmp2[j])%P2;}
    Vt1[i]=Ha1;Vt2[i]=Ha2;
}
for(int i=1;i<=n;i++){valB_1[i]=Vt1[i];valB_2[i]=Vt2[i];}
}

void Solve(){
scanf("%d %d",&n,&m);
memset(hA,0,sizeof(hA));
memset(hB,0,sizeof(hB));
enA=0;enB=0;
for(int i=1;i<=m;i++){
	int u,v;
	scanf("%d %d",&u,&v);
	AddEdgeA(u,v);AddEdgeA(v,u);
}
for(int i=1;i<=m;i++){
	int u,v;
	scanf("%d %d",&u,&v);
	AddEdgeB(u,v);AddEdgeB(v,u);
}
for(int i=1;i<=n;i++)valA_1[i]=valA_2[i]=valB_1[i]=valB_2[i]=1;
for(int i=1;i<=n;i++)Solve_A();
for(int i=1;i<=n;i++)Solve_B();
sort(valA_1+1,valA_1+n+1);
sort(valA_2+1,valA_2+n+1);
sort(valB_1+1,valB_1+n+1);
sort(valB_2+1,valB_2+n+1);
int flag=1;
for(int i=1;i<=n;i++)if(valA_1[i]!=valB_1[i] || valA_2[i]!=valB_2[i]){puts("NO");flag=0;break;}
if(flag)puts("YES");
}

int main(){
freopen("1676.in","r",stdin);
freopen("1676.out","w",stdout);
scanf("%d",&test);
while(test--)Solve();
return 0;
}
Category: 其他OJ | Tags: OI 51nod | Read Count: 507
CBSE 12th Question P 说:
Aug 27, 2022 03:20:39 PM

Central Board of Secondary Education (CBSE) is Going to Conduct 11th or 12th Examination in the month of March to April 2023 Under Union Government of India. CBSE Published in the Class 12th Examination Model Question CBSE 12th Question Paper Paper 2023 Blueprint at Official Website Central Board of Secondary Education (CBSE) is Going to Conduct 11th or 12th Examination in the month of March to April 2023 Under Union Government of India. CBSE Published in the Class 12th Examination Model Question Paper 2023 Blueprint at Official Website


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com