Title Link: 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 has more than, hands get together so one hand grabs at the 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 have a question for his kids: "Is the and the" graph isomorphism? "InputThe first line contains a single positive integer t (t <=), indicating the number of datasets.
there is and 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 has a integers u and V indicating kid U and V is "hand in hand".
You can assume all kid only have a hands.OutputFor each test case:output the case number as shown and "YES" if the both graph is isomorphism or "NO" otherwise.Test Instructions Description: Determine whether the two figures are the same composition, the definition of the same composition: assuming g= (v,e) and g1= (V1,E1) is two graphs, if there is a double-shot m:v→v1, so that all x,y∈v have xy∈e equivalentin M (x) m (y) ∈e1, the G and G1 are isomorphic, so that a map m is called an isomorphism, and if G=G1, it is called a self-isomorphic. algorithm Analysis: As the judgment is isomorphic, we just need to determine whether the number of nodes in a set is equal and whether it is a ring.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 #defineINF 0x7fffffff8 using namespacestd;9 Const intmaxn=10000+Ten;Ten One intn,m,n2,m2; A intFATHER[MAXN],D[MAXN],ISCIRCLE[MAXN]; - structnode - { the intnum,iscircle; -FriendBOOL operator<(node A,node b) - { - if(A.num!=b.num)returnA.num>B.num; + returnA.iscircle>b.iscircle; - } + }AN[MAXN],BN[MAXN]; A at intFindset (intx) - { - if(X==father[x])returnx; - returnfather[x]=Findset (father[x]); - } - voidUnion (intXinty) in { -X=findset (x); y=Findset (y); to if(x==y) {iscircle[x]=1;return;} + if(d[x]>D[y]) - { thefather[y]=x; *D[X] + =D[y]; $ }Panax Notoginseng Else - { thefather[x]=y; +D[y] + =D[x]; A } the } + - intMain () $ { $ intT,ncase=1; -scanf"%d",&t); - while(t--) the { -scanf"%d%d",&n,&m);Wuyimemset (Iscircle,0,sizeof(iscircle)); the for(intI=1; i<=n; i++) father[i]=i,d[i]=1; - intu,v; Wu for(intI=0; i<m; i++.) - { Aboutscanf"%d%d",&u,&v); $ Union (u,v); - } - intCnt=0, cnt2=0; - for(intI=1; i<=n; i++)if(father[i]==i) A { +An[cnt].num=d[i]; an[cnt].iscircle=Iscircle[i]; theCNT + + ; - } $Sort (an,an+CNT); the thescanf"%d%d",&n2,&m2); thememset (Iscircle,0,sizeof(iscircle)); the for(intI=1; i<=n2; i++) father[i]=i,d[i]=1; - for(intI=0; i<m2; i++.) in { thescanf"%d%d",&u,&v); the Union (u,v); About } the for(intI=1; i<=n2; i++)if(father[i]==i) the { theBn[cnt2].num=d[i]; bn[cnt2].iscircle=Iscircle[i]; +cnt2++; - } theSort (bn,bn+cnt2);Bayi theprintf"Case #%d:", ncase++); the if(n!=n2 | | m!=m2 | | cnt!=cnt2) {printf ("no\n");Continue; } - intflag=0; - for(intI=0; i<cnt; i++.) the { the if(An[i].num! = bn[i].num) {flag=1; Break; } the if(an[i].iscircle! = bn[i].iscircle) {flag=1; Break; } the } - if(flag) printf ("no\n"); the Elseprintf"yes\n"); the } the return 0;94}
Hdu 3926 Hand in Hand with composition