题目链接: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. 设置trace SQL

    Select PeopleTools, Utilities, Debug, Trace SQL to access the Trace SQL page. You use this page to c ...

  2. Java编程性能优化

    1尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: 第一,控制资源的使用,通过线程同步来控制资 ...

  3. JSON.stringify 语法实例讲解

    语法:  JSON.stringify(value [, replacer] [, space]) value:是必选字段.就是你输入的对象,比如数组,类等. replacer:这个是可选的.它又分为 ...

  4. 实现QQ机器人报警

    如题,废话不说,直接上代码.首先是登录QQ的小脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  5. web.xml中常见配置解读

    文章转自:http://blog.csdn.net/sdyy321/article/details/5838791 有一般XML都必须有的版本.编码.DTD <web-app>下子元素&l ...

  6. List集合实战总结

    //构造被分隔的集合 List<object> list = new List<object>(); for (int i = 0; i <= 100; i++) { l ...

  7. 关于datagridview的一些操作

    1.绑定datatable时,会显示出不需要显示的列可以加datagridview.AutoGenerateColumns = false; 2.如果datagridview的某列是数值型的,有小数, ...

  8. Visual Studio 2013中添加mimeType

    事例: cd C:\Program files\IIS Expressappcmd set config /section:staticContent /+[fileExtension='.json' ...

  9. 使用checked关键字处理“溢出”错误

    在进行算术运算时,可以使用checked关键字有效处理溢出错误,使用checked关键字可能对程序的性能会有一点点的影响,这是一种以牺牲性能换取健康的做法. private void button1_ ...

  10. 学长们的求职血泪史(C/C++/JAVA)

    以下分三个方向讲解,每个方向都是一个学长独自撰稿. (一)  C语言篇 C语言求职血泪史 华为(实习):机试.一面.性格测试被鄙视.优招被鄙视.普招被鄙视 锐捷:笔试.面试莫名其妙被鄙视 创新工场:笔 ...