Link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3926
Original question:
Hand in hand
Time Limit: 2000/1000 MS (Java/others) memory limit: 122768/62768 K (Java/Others)
Total submission (s): 731 accepted submission (s): 294
Problem descriptionin 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? "
Inputthe 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.
Outputfor each test case: output the case number as shown and "yes" if the two graph are Isomorphism or "no" otherwise.
Sample Input
23 21 22 33 23 22 13 31 22 33 13 11 2
Sample output
Case #1: YESCase #2: NO
Source2011 multi-university training contest 9-host
By bjtu
Analysis and Summary:
Since each person has only two hands, each hand cannot be connected with multiple hands, that is to say, each node can only have two degrees. In the end, these students may be divided into multiple groups, each group is a circular circle or a chain.
The question is to determine whether two connected graphs are "same-picture". The so-called same-picture refers to the different circles and chains composed of two graphs, the numbers corresponding to each of them are equal.
Then we only need to use the query set to merge all of these connections. If we find a ring, we will identify it. Then, the Union query set of the two graphs is sorted by the number of nodes in each tree. If the number is the same, all nodes with loops are placed in front. Then, as long as we compare two sorted arrays one by one, as long as we find that one is different, it is not the same diagram.
Code:
#include<cstdio>#include<algorithm>#include<cstring>#define N 10010using namespace std;int f[N], rank[N], n1, m1, n2, m2, pos1, pos2, isCircle[N];struct Node{ int num; int isCircle; friend bool operator <(const Node&a,const Node&b){ if(a.num!=b.num) return a.num<b.num; if(a.isCircle<b.isCircle) return true; return false; }}arr1[N], arr2[N];void init(int n){ memset(isCircle, 0, sizeof(isCircle)); for(int i=0; i<=n; ++i) f[i]=i,rank[i]=1;}int find(int x){ int i, j=x; while(j!=f[j]) j=f[j]; while(x!=j){ i=f[x]; f[x]=j; x=i; } return j;}void Union(int x,int y){ int a=find(x), b=find(y); if(a==b){ isCircle[a] = 1; return; } rank[a] += rank[b]; f[b] = a;}void play(int n, int m){ int a,b; init(n); for(int i=0; i<m; ++i){ scanf("%d%d",&a,&b); Union(a, b); }}bool judge(){ if(pos1!=pos2)return false; sort(arr1, arr1+pos1); sort(arr2, arr2+pos2); for(int i=0; i<pos1; ++i){ if(arr1[i].num!=arr2[i].num)return false; if(arr1[i].isCircle!=arr2[i].isCircle)return false; } return true;}int main(){ int T,n1,cas=1; scanf("%d",&T); while(T--){ printf("Case #%d: ",cas++); scanf("%d%d",&n1,&m1); play(n1,m1); pos1 = 0; for(int i=1; i<=n1; ++i)if(i==find(i)){ arr1[pos1].num = rank[i]; arr1[pos1++].isCircle = isCircle[i]; } scanf("%d%d",&n2,&m2); play(n2, m2); if(n1!=n2 || m1!=m2){ puts("NO"); continue; } pos2 = 0; for(int i=1; i<=n2; ++i)if(i==find(i)){ arr2[pos2].num = rank[i]; arr2[pos2++].isCircle = isCircle[i]; } if(judge()) puts("YES"); else puts("NO"); } return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)