hdu 3926 Hand in Hand 判断两个度最多是2的无向图是否同构

本文介绍了一种通过分析孩子们在游戏中形成的“手拉手”连接来判断两组连接是否构成图同构的问题。利用图论中的概念,文章详细阐述了如何通过遍历和比较两个图的结构来确定它们是否等价。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

//因为度最多是2,所以每一块是链或者是一个环。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100000;
struct edge
{
    int v;
    int next;
};
int V,E;
int p[maxn];
edge G[maxn];
int l;
void init()
{
    l=0;
    memset(p,-1,sizeof(p));
}
void addedge_double(int u,int v)
{
    G[l].v=v;
    G[l].next=p[u];
    p[u]=l++;
    G[l].v=u;
    G[l].next=p[v];
    p[v]=l++;
}
int a[maxn],b[maxn];
int la,lb;
void input()
{
    init();
    scanf("%d%d",&V,&E);
    for(int i=0;i<E;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        addedge_double(u,v);
    }
}
int vis[maxn];
int cnt;
int circle;
void dfs(int u,int fath)
{
    vis[u]=1;cnt++;
    for(int i=p[u];i!=-1;i=G[i].next)
    {
        int t=G[i].v;
        if(t==fath) continue;
        if(vis[t]) circle=1;
        else dfs(t,u);
    }
}
void solve(int a[maxn],int &l)
{
    l=0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=V;i++)
    {
        if(!vis[i])
        {
            cnt=0;
            circle=0;
            dfs(i,-1);
            if(circle) cnt=-cnt;
            a[l++]=cnt;
        }
    }
}
int same()
{
    sort(a,a+la);
    sort(b,b+lb);
    if(la!=lb) return 0;
    for(int i=0;i<la;i++) if(a[i]!=b[i]) return 0;
    return 1;
}
int main()
{
    int ci,pl=1;scanf("%d",&ci);
    while(ci--)
    {
        input();
        solve(a,la);
        input();
        solve(b,lb);
        if(same()) printf("Case #%d: YES\n",pl++);
        else printf("Case #%d: NO\n",pl++);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值