【HDU 3926】Hand in Hand(并查集+同构图)

Hand in Hand
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 122768/62768 K (Java/Others)
Total Submission(s): 2757 Accepted Submission(s): 943

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

扫描二维码关注公众号,回复: 3106911 查看本文章

Case #1: YES
Case #2: NO

Source
2011 Multi-University Training Contest 9 - Host by BJTU

题意:给你两个图,判断这两个图是否相似。
思路:对于两个图,我们找出它们的环的数量,它们的链的数量以及链的大小
,排序后比较即可。
在判环的时候,我们可以利用并查集判环。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100500;
struct gra{
    int ran;//深度(链)
    int par;//父节点
    int ring;//判断是否成环
}s1[maxn],s2[maxn];
int n1,m1;
int n2,m2;
void init(){
    for(int i=1;i<maxn-1;i++){
        s1[i].par=i;
        s1[i].ring=0;
        s1[i].ran=1;
        s2[i].par=i;
        s2[i].ring=0;
        s2[i].ran=1;
    }
}
bool cmp(gra a,gra b){
    if(a.ran==b.ran) return a.ring<b.ring;
    return a.ran<b.ran;
}
int find1(int x){
    return x==s1[x].par?x:find1(s1[x].par);
}
int find2(int x){
    return x==s2[x].par?x:find2(s2[x].par);
}
void unite1(int a,int b){
    a=find1(a);
    b=find1(b);
    if(a==b){//判环
        s1[a].ring=1;
        return ;
    }
    if(s1[a].ran>s1[b].ran){
        s1[a].ran+=s1[b].ran;
        s1[b].par=a;
    }
    else{
        s1[b].ran+=s1[a].ran;
        s1[a].par=b;
    }
}
void unite2(int a,int b){
    a=find2(a);
    b=find2(b);
    if(a==b){
        s2[a].ring=1;
        return ;
    }
    if(s2[a].ran>s2[b].ran){
        s2[a].ran+=s2[b].ran;
        s2[b].par=a;
    }
    else{
        s2[b].ran+=s2[a].ran;
        s2[a].par=b;
    }
}
int main(){
    int t;
    scanf("%d",&t);
    int cnt=1;
    while(t--){
        init();
        bool flag=1;
        scanf("%d %d",&n1,&m1);
        int a,b;
        for(int i=1;i<=m1;i++){
            scanf("%d %d",&a,&b);
            unite1(a,b);
        }
        scanf("%d %d",&n2,&m2);
        for(int i=1;i<=m2;i++){
            scanf("%d %d",&a,&b);
            if(!flag) continue;
             unite2(a,b);
        }
        if(n1!=n2||m1!=m2) {
            flag=0;
        }
        printf("Case #%d: ",cnt++);
        sort(s1+1,s1+n1+1,cmp);
        sort(s2+1,s2+n2+1,cmp);
        for(int i=1;i<=n1;i++){
            if(s1[i].ring!=s2[i].ring||s1[i].ran!=s2[i].ran){
                flag=0;
                break;
            }
        }
        if(flag) puts("YES");
        else puts("NO");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/duanghaha/article/details/82354044