1. 程式人生 > >HDU 3926 Hand in Hand (圖的同構)

HDU 3926 Hand in Hand (圖的同構)

題目連結

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

PS:這是一個圖的判斷的題,首先這個圖的最大度是2,就說明圖中可能有鏈,環。現在可以遍歷每個連通分量,看節點數以及該連通分量是環還是鏈。判斷之前先將兩個圖按照節點數排序,若節點數一樣,則按照鏈在前環在後的順序排序。然後遍歷比較就可以了,細節看程式碼。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e4+10;
const int mod=1e9+7;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
int roota[maxn],rootb[maxn];
int na,ma,nb,mb;
struct node
{
    int cnt;
    int type;
    node(int a=1,int b=0):cnt(a),type(b){}
    bool friend operator<(node a,node b)//首先按照節點數目排序,若節點數目一樣,則按照鏈在前,環在後的順序排列。
    {
        if(a.cnt==b.cnt)
            return a.type<b.type;
        return a.cnt<b.cnt;
    }
}ta[maxn],tb[maxn];//ta存圖A,tb存圖B
void inct()//初始化
{
    for(int i=0;i<maxn;i++)
    {
        roota[i]=rootb[i]=i;
        ta[i].cnt=tb[i].cnt=1;//每個節點初始化節點數目為一個
        ta[i].type=tb[i].type=0;//0代表鏈,1代表環。
    }
}
int Find(int x,int *Fa)//找根節點
{
    return x==Fa[x]?x:Fa[x]=Find(Fa[x],Fa);
}
void unite(int x,int y,int *Fa,node *a)//合併
{
    int x1=Find(x,Fa);
    int y1=Find(y,Fa);
    if(x1==y1)//若根節點一樣,說明形成環,將type賦值為1.
        a[x1].type=1;
    else//根節點不一樣,說明是鏈,將節點少的鏈連線到節點多的鏈上。
    {
        if(a[x1].cnt>a[y1].cnt)
        {
            a[x1].cnt+=a[y1].cnt;
            Fa[y1]=x1;
        }
        else
        {
            a[y1].cnt+=a[x1].cnt;
            Fa[x1]=y1;
        }
    }
}
bool check()//比較函式
{
    sort(ta,ta+na+1);
    sort(tb,tb+nb+1);
    for(int i=0;i<=na;i++)
        if(ta[i].cnt!=tb[i].cnt||ta[i].type!=tb[i].type)
            return 0;
    return 1;
}
int main()
{
    int t,Case=1;
    scanf("%d",&t);
    while(t--)
    {
        inct();
        scanf("%d%d",&na,&ma);
        while(ma--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            unite(u,v,roota,ta);
        }
        scanf("%d%d",&nb,&mb);
        while(mb--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            unite(u,v,rootb,tb);
        }
        printf("Case #%d: ",Case++);
        if(ma!=mb||na!=nb)//當節點數不一樣,或者手拉手的數目不一樣,圖肯定不一樣。
            printf("NO\n");
        else if(check())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}