hdu 3926 Hand in Hand 同构图
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3926
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.
steps again to form another graph. Now Kid has a question for his kids: "Are the
two graph isomorphism?"
<= 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.
"YES" if the two graph are isomorphism or "NO" otherwise.
#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 同构图的更多相关文章
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 3926 图的同构
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3926 题意:给定2个顶点度最大为2的无向图.问你这2个无向图是否同构. 思路: 1.最大度为2.说明这 ...
- hdu 3926 Hand in Hand
http://acm.hdu.edu.cn/showproblem.php?pid=3926 这道题是判断两个图是不是同构相似.只要判断图中环的个数和链的个数,和每个环的节点数和链的节点数是否相等. ...
- hdu 3926 hands in hands
https://vjudge.net/problem/HDU-3926 题意:有n个小朋友,他们之间手拉手,但是一只手只能拉一只手或者不拉,现在给出两个图,表示拉手关系,问这两个图是否同构.思路:一开 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- http://www.shanghaihaocong.com-WORDPRESS开发的企业主题站
wordpress是世界上使用最多的php开源博客系统,功能强大,而且拥有众多的插件,可扩展性强. 最近,我也用它做了一个企业网站,欢迎浏览:http://www.shanghaihaocong.co ...
- js获取和设置DOM样式函数cssStyle(类似于jquery的$(elem).css())
如题,相信这个函数百度一搜一大推,但令人匪夷所思的是这些函数都写的“奇形怪状的”,例如http://www.cnblogs.com/windows7/archive/2010/03/30/170064 ...
- js获取数组中的最大值最小值
遍历方法: var tmp = [1,12,8,5]; var max = tmp[0]; for(var i=1;i<tmp.length;i++){ if(max<tmp[i])max ...
- php 安装pdo_mysql 扩展
如果pdo在linux下出现exception 'PDOException' with message 'could not find driver'则问题是php编译时少加--with-pdo-my ...
- Yii中使用PHPexcel获取excel中数据
1.view中代码如下: <form name="frmBatchSettle" id="" action="" method=&qu ...
- sql server备份还原数据时的问题记录
1.关于“因为数据库正在使用,所以无法获得对数据库的独占访问权”的最终解决方案 关键SQL语句: ALTER DATABASE [datebase] SET OFFLINE WITH ROLLBACK ...
- setTimeOut(),和setInterVal()调用函数加不加括号!!!
直接在ready中调用其他方法,会提示缺少对象的错误,解决方法如下: 方法1. 应用jQuery的扩展可以解决这个问题. $(document).ready(function(){ $.extend( ...
- C# 平时碰见的问题【5】
vs按F5启动调试,项目不会编译的解决办法 工具 -> 选项 -> 项目和解决方案 -> 运行时, 当项目过期(下拉框) -> 不要选[从不生成] 附英文版的:
- python笔记第二天
上节内容回顾和补充 编程语言 高级 低级 Python种类 JavaPython cPython ***** pypy 字节码 和 机器码 Python程序: 1. 终端: C:\python35\p ...
- Python多版本安装 Python2.7和Python3.5
声明:本文仅仅在win8.1测试通过! 1.下载 Python2.7,3.5 2.依次安装Python27(c:\Python27) Python35(c:\Python35) 3.c:\Pytho ...