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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值