HDU 3926 Hand in Hand(判定同構造)

3365 ワード

Hand in Hand
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Others) Total Submission(s): 1880    Accepted Submission(s): 635
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 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?"
 
Input
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.
 
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
2011 Multi-University Training Contest 9 - Host by BJTU 

因为每个人小朋友只有两只手,所以每个点最多只有2度。图有可能是环、链,以及环和链构成的复杂图。  

  如何判断两幅图是否相似呢?判断相似是判断两幅图的圈的数量,以及构成圈的点数是否相同。还有判断链的数目和构成链的点数是否相同。

  具体实现:标记环(或者链),按照点的数目排序。如果点数相同,环排在前面。然后逐个判断,如果全部都相同,就是同构。

#include
#include
#include
#include
using namespace std;
const int N=10010;
struct node
{
    int cnt;
    bool iscc;
}arr1[N],arr2[N];
int f[N],r[N],iscir[N];
bool cmp(const node &x, const node &y)
{
    if(x.cnt!=y.cnt) return x.cnt