Link:
http://acm.hdu.edu.cn/showproblem.php?pid=3926
Original title:
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 Description
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 the than two hands get together so one hand grabs at most one. After kids stop moving they form a graph.
Everybody takes a look in the graph and repeat the above steps again to form another graph. Now Kid has a question to his kids: "Are the two graph isomorphism?"
Input
The contains a single positive integer t (t <=), indicating the number of datasets.
There are two graphs in and for each graph:
The 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.
Output
For each test case:output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise.
Sample Input
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
Sample Output
Case #1: YES case
#2: NO
Source
Multi-university training Contest 9-host by Bjtu
Analysis and Summary:
Since each person has only two hands, each hand cannot be connected with more than one hand, that is, each node can be only 2 degrees, eventually the students may be divided into groups, each group is a circular circle, or a chain.
The topic is to judge whether the two already connected graphs are "identical compositions", where the so-called same composition refers to the different circles of the two graphs, the chains, each of which correspond to equal numbers.
Then we just need to use and search the set to combine the same, if found that there is a ring, to identify. The two graphs are then sorted according to the number of nodes per tree, and if the numbers are the same, then the rings are placed in front. Then, as long as you compare two sorted arrays, if you find that one is different, it's not the same composition.