HDU 3926 同构图

Hand in Hand

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Others)
Total Submission(s): 1005    Accepted Submission(s): 370


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

 

用并查集判断是链还是环,然后判断每一个链,每一个环节点个数是否相同。

代码:

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
const int maxn=10010;
int pre1[maxn],pre2[maxn];
int find1(int x)
{
        if(pre1[x]!=x)pre1[x]=find1(pre1[x]);
        return pre1[x];
}
int find2(int x)
{
        if(pre2[x]!=x)pre2[x]=find2(pre2[x]);
        return pre2[x];
}
struct node
{
        int son;
        bool ring;
}g1[maxn],g2[maxn];
bool cmp1(node a,node b)
{
        if(a.son!=b.son)return a.son<b.son;
        return a.ring<b.ring;
}
int main()
{
        int num1,link1,num2,link2,i,j,k,m,n,T,t;
        scanf("%d",&T);
        for(t=1;t<=T;t++)
        {
                bool flag=1;
                for(i=1;i<=10000;i++)
                {
                        pre1[i]=i;
                        pre2[i]=i;
                        g1[i].son=1;
                        g1[i].ring=0;
                        g2[i].son=1;
                        g2[i].ring=0;
                }
                scanf("%d%d",&num1,&link1);
               // cout<<"1111111    "<<num1<<" "<<link1<<endl;
                for(k=1;k<=link1;k++)
                {
                        scanf("%d%d",&i,&j);
                        i=find1(i);
                        j=find1(j);
                        if(i==j)g1[i].ring=1;
                        else
                        {
                                if(g1[i].son>=g1[j].son)
                                {
                                        pre1[j]=i;
                                        g1[i].son+=g1[j].son;
                                }
                                else
                                {
                                        pre1[i]=j;
                                        g1[j].son+=g1[i].son;
                                }
                        }
                }
                        scanf("%d%d",&num2,&link2);
                      //  cout<<"1222222222   "<<num1<<" "<<link1<<endl;
                        if(link1!=link2)flag=0;
                        for(k=1;k<=link2;k++)
                        {
                               scanf("%d%d",&i,&j);
                               if(flag==0)continue;
                                i=find2(i);
                                j=find2(j);
                                if(i==j)g2[i].ring=1;
                                else
                                {
                                       if(g2[i].son>=g2[j].son)
                                       {
                                             pre2[j]=i;
                                             g2[i].son+=g2[j].son;
                                       }
                                       else
                                       {
                                             pre2[i]=j;
                                             g2[j].son+=g2[i].son;
                                       }
                                }
                        }
                        printf("Case #%d: ",t);//cout<<flag<<endl;
                      
//  cout<<num1<<" "<<link1<<" "<<num2<<" "<<link2<<endl;
                        if(!flag)puts("NO");
                        else
                        {
                             sort(g1+1,g1+num1+1,cmp1);
                             sort(g2+1,g2+num2+1,cmp1);
                             for(i=1;i<=num1;i++)
                             {
                                     if((g1[i].son!=g2[i].son)||(g1[i].son==g2[i].son&&g1[i].ring!=g2[i].ring))flag=0;
                             }
                            // cout<<flag<<endl;
                             if(flag)puts("YES");
                             else puts("NO");
                        }
        }
}

 

posted @ 2013-09-13 13:47  线性无关  阅读(143)  评论(0)    收藏  举报