题目链接: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. zabbix介绍

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix组件主要分两个: zabbix-server和zabbix-agent.支持的监控协议有ICM ...

  2. jQuery在IE7和8下setInterval失效的问题

    原因不在于setInterval,而是IE的缓存造成ajax请求页没有更新的问题. 在请求的url中加入一个随机数参数即可. var CheckPaied = function (transactio ...

  3. 《安全参考》HACKCTO-201312-12

    小编的话 “忽如一夜春风来,千树万树梨花开.” 小伙伴们,不要只为了“千树万树的梨花”而惊喜,陶醉! 与此同时,您最爱的整合型信息安全技术期刊<安全参考>第12期也如约而至啦! 这一期&l ...

  4. char引发的血案

    char cc = 'j';cc = (char)(cc -32); //注意下,自动转型了System.out.println(cc);

  5. 带有×的EditText

    代码: EditTextWithDel.java(直接复制): package com.sunday.customs; import com.example.customs.R; import and ...

  6. IS上部署MVC网站,打开后ExtensionlessUrlHandler-

    以管理员运行下面的命令注册: 32位机器: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 64位机器: C:\W ...

  7. Oracle并行事务回滚相关参数及视图

    /******相关参数****/fast_start_parallel_rollback1.取值有3种:false,low,high2.各值含义:false  ---禁用并行回滚功能          ...

  8. Ztack学习笔记(6)-广播组播点播

    Zigbee网络中进行数据通信主要有三种类型:单播.组播.广播.那这三种方式如何设置呢,在哪里设置呢, 一. 广播 当应用程序需要将数据包发送给网络的每一个设备时,使用这种模式.广播的短地址有三种 0 ...

  9. lldb

    所有命令选择与input 值用  -- 区分 1 p/x 16 转16进制 https://sourceware.org/gdb/onlinedocs/gdb/Output-Formats.html ...

  10. matlab封装DLL混合编程总结

    最近做了个项目要用到matlab做些算法处理,然后用.net项目调用这个类,我把这个matlab封装dll总结了下如下: matlab是商业数学软件,优势是在算法开发上面有很强的功能,提供了很多数学算 ...