脚本宝典收集整理的这篇文章主要介绍了HDU 3926 图的同构,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
http://acm.hdu.edu.cn/showproblem.php?pid=3926
In order to get rid of Conan,Kaitou KID disguises himself as a teacher in the kindergarten. He kNows kids love games and works out a new game called "hand in hand".
InITially kids run on the playground randoMLy. When Kid says "stop",kids catch others‘ hands immediately. One hand can catch any other hand randomly. It‘s weird to have more than two hands get together so one hand grabs at most one other hand. After kids stop moving they form a graph.
Everybody takes a look at the graph and rePEat the above steps again to form another graph. Now Kid has a question for his kids: "Are the two graph isomorphism?"
The First line contains a single positive integer T( T <= 100 ),indicating the number of datasets.
There are two graphs in each case,for each graph:
? first line contains N( 1 <= N <= 10^4 ) and M indicating the number of kids and connections.
? the next M lines each have two integers u and v indicating kid u and v are "hand in hand".
? You can assume each kid only has two hands.
For each test case: output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise.
2 3 2 1 2 2 3 3 2 3 2 2 1 3 3 1 2 2 3 3 1 3 1 1 2
Case #1: YES Case #2: NO
因为每个点最多只有两个度,所以只会存在链或者环,这样就容易做了,搜索就行了。特别注意1—>2,2->1不能看成一个环,只是一条链,和单独的1->2是一样的,
#include<bits/stdc++.h> using namespace std; #define ll long long #define N 10050 #define M 100050 struct Edge{int x,y,s;}G1[M],G2[M]; int last1[N],last2[N]; template<typename T>void read(T&x) { ll k=0; char c=getchar(); x=0; while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar(); if (c==EOF)exit(0); while(isdigit(c))x=x*10+c-'0',c=getchar(); x=k?-x:x; } void read_char(char &c) {while(!isalpha(c=getchar())&&c!=EOF);} int DFs(int x,Edge*G,int *last,int *f) { f[x]=1; int ans=1; for(int i=last[x];i;i=G[i].s) { Edge &e=G[i]; if (f[e.y])continue; ans+=dfs(e.y,G,last,f); } return ans; } void init(int &n,int &m,int *a,int &num) { static int d[N],f[N]; int tot=0; num=0; read(n); read(m); memset(f,sizeof(int)*(n+1)); memset(d,sizeof(int)*(n+1)); memset(last,sizeof(int)*(n+1)); for(int i=1;i<=m;i++) { int x,y; read(x); read(y); G[++tot]=Edge{x,last[x]}; last[x]=tot; G[++tot]=Edge{y,x,last[y]}; last[y]=tot; d[x]++; d[y]++; } for(int i=1;i<=n;i++) if (d[i]==1&&!f[i])a[++num]=dfs(i,f); for(int i=1;i<=n;i++) if (!f[i]){a[++num]=dfs(i,f);if (a[num]>2)a[num]+=N;} } void work() { static int cas=0,n1,m1,n2,m2,a1[N],a2[N],num1,num2; init(n1,G1,last1,a1,num1); init(n2,G2,last2,a2,num2); sort(a1+1,a1+num1+1); sort(a2+1,a2+num2+1); for(int i=1;i<=num1;i++) if (a1[i]!=a2[i]||num1!=num2){printf("Case #%d: NO\n",++cas);return;} printf("Case #%d: YES\n",++cas); } int main() { #ifndef ONLINE_JUDGE freopen("aa.in","r",stdin); #endif int T; read(T); while(T--)work(); }
以上是脚本宝典为你收集整理的HDU 3926 图的同构全部内容,希望文章能够帮你解决HDU 3926 图的同构所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。