题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3926

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.
题意描述:判断两个图是否是同构图,同构图的定义:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所有的x,y∈V均有xy∈E等价于m(x)m(y)∈E1,则称G和G1是同构的,这样的一个映射m称之为一个同构,如果G=G1,则称他为一个自同构。
算法分析:由于是判断是否同构,我们就只需要判断一个集合里的节点数目是否相等和是否都是一个环就可以了。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int n,m,n2,m2;
int father[maxn],d[maxn],isCircle[maxn];
struct node
{
int num,isCircle;
friend bool operator < (node a,node b)
{
if (a.num!=b.num) return a.num>b.num;
return a.isCircle>b.isCircle;
}
}an[maxn],bn[maxn]; int findset(int x)
{
if (x==father[x]) return x;
return father[x]=findset(father[x]);
}
void Union(int x,int y)
{
x=findset(x) ;y=findset(y) ;
if (x==y) {isCircle[x]=;return;}
if (d[x]>d[y])
{
father[y]=x;
d[x] += d[y];
}
else
{
father[x]=y;
d[y] += d[x];
}
} int main()
{
int t,ncase=;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
memset(isCircle,,sizeof(isCircle));
for (int i= ;i<=n ;i++) father[i]=i,d[i]=;
int u,v;
for (int i= ;i<m ;i++)
{
scanf("%d%d",&u,&v);
Union(u,v);
}
int cnt=,cnt2=;
for (int i= ;i<=n ;i++) if (father[i]==i)
{
an[cnt].num=d[i] ;an[cnt].isCircle=isCircle[i];
cnt ++ ;
}
sort(an,an+cnt); scanf("%d%d",&n2,&m2);
memset(isCircle,,sizeof(isCircle));
for (int i= ;i<=n2 ;i++) father[i]=i,d[i]=;
for (int i= ;i<m2 ;i++)
{
scanf("%d%d",&u,&v);
Union(u,v);
}
for (int i= ;i<=n2 ;i++) if (father[i]==i)
{
bn[cnt2].num=d[i] ;bn[cnt2].isCircle=isCircle[i];
cnt2++;
}
sort(bn,bn+cnt2); printf("Case #%d: ",ncase++);
if (n!=n2 || m!=m2 || cnt!=cnt2) {printf("NO\n");continue; }
int flag=;
for (int i= ;i<cnt ;i++)
{
if (an[i].num != bn[i].num) {flag=;break; }
if (an[i].isCircle != bn[i].isCircle) {flag=;break; }
}
if (flag) printf("NO\n");
else printf("YES\n");
}
return ;
}

hdu 3926 Hand in Hand 同构图的更多相关文章

  1. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  2. HDU 3926 图的同构

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3926 题意:给定2个顶点度最大为2的无向图.问你这2个无向图是否同构. 思路: 1.最大度为2.说明这 ...

  3. hdu 3926 Hand in Hand

    http://acm.hdu.edu.cn/showproblem.php?pid=3926 这道题是判断两个图是不是同构相似.只要判断图中环的个数和链的个数,和每个环的节点数和链的节点数是否相等. ...

  4. hdu 3926 hands in hands

    https://vjudge.net/problem/HDU-3926 题意:有n个小朋友,他们之间手拉手,但是一只手只能拉一只手或者不拉,现在给出两个图,表示拉手关系,问这两个图是否同构.思路:一开 ...

  5. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  6. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  7. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  8. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  9. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. Zookeeper 脑裂

    转自 http://blog.csdn.net/u010185262/article/details/49910301 Zookeeper zookeeper是一个分布式应用程序的协调服务.它是一个为 ...

  2. jquery源码学习--iceDog

    /** * @FileName : iceDog * @Author : PheonixHkbxoic * @Mail : hkbxoic@gmail.com * @DateTime : 2016-0 ...

  3. 三、MongoDB的创建、更新和删除

    一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 概要 下面开始学习MongoDB最重要也是最基础的部分:C(创建)R(查询)U(更新)D( ...

  4. 配置pxe 自动化安装centos6.7

    dhcp服务器是pxe自动化安装的必要条件,因此先搞定dhcp服务器,yum -y install dhcp,  rpm -ql dhcp查看安装了哪些包,less /etc/dhcp/dhcpd.c ...

  5. php下使用phpmailer发送邮件

    由于默认虚拟空间不支持mail()函数,客户需要留言发送邮件,找到phpmailer发送不成功,调试成功后记录一下. 最新的下载地址在github,https://github.com/Synchro ...

  6. 全排列 (codevs 1294)题解

    [题目描述] 给出一个n, 请输出n的所有全排列(按字典序输出). [样例输入] 3 [样例输出] 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 [解题思路] 听说C++有作 ...

  7. 学习simple.data之进阶篇

    一.结果排序 -OrderBy(升序) -OrderByDescending(降序) db.Product.All().OrderByFactoryName(); db.Product.All().O ...

  8. Java程序员要注意的10个问题————————好东西就是要拿来分享

    [本文来自优优码:http://www.uucode.net/201406/ten-issue-for-java],好东西就是要拿来分享 1. Array 转为 ArrayList 很多人会这么写: ...

  9. Python数学运算

    python中的加减乘除比其他的语言简单,不需要对其赋值变量 (1)加减乘除 ) #加法 ) #减法 ) #乘法 ) #除法 5.0 ) #乘方 (2)判断 判断返回的是True或者False ) # ...

  10. spring与mysql整合数据源的配置

    需要解决两点,数据源的配置交给spring完成,事务管理交个spring来管理. <context:property-placeholder location="classpath:c ...