hdu 3926 Hand in Hand
http://acm.hdu.edu.cn/showproblem.php?pid=3926
这道题是判断两个图是不是同构相似。只要判断图中环的个数和链的个数,和每个环的节点数和链的节点数是否相等。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 30000
using namespace std; int t,n1,m1,n2,m2;
int f[maxn],num[maxn];
int cicl[maxn];
struct node
{
int lis;
int cicl;
bool operator <(const node &a)const
{
return ((lis<a.lis)||(lis==a.lis&&cicl<a.cicl));
}
}p1[maxn],p2[maxn]; int find1(int x)
{
if(x==f[x]) return x;
return f[x]=find1(f[x]);
} void merge1(int a,int b)
{
int fa=find1(a);
int fb=find1(b);
if(fa==fb)
{
cicl[fa]=;
return;
}
num[fa]+=num[fb];
f[fb]=fa;
} void inti(int n)
{
for(int i=; i<=n; i++)
{
f[i]=i;
num[i]=;
}
memset(cicl,,sizeof(cicl));
} int main()
{
scanf("%d",&t);
for(int ca=; ca<=t; ca++)
{
scanf("%d%d",&n1,&m1);
inti(n1);
for(int i=; i<m1; i++)
{
int a,b;
scanf("%d%d",&a,&b);
merge1(a,b);
}
int cnt1=;
for(int i=; i<=n1; i++)
{
if(f[i]==i)
{
p1[cnt1].lis=num[i];
p1[cnt1++].cicl=cicl[i];
}
}
scanf("%d%d",&n2,&m2);
inti(n2);
int cnt2=;
for(int i=; i<m2; i++)
{
int a1,b1;
scanf("%d%d",&a1,&b1);
merge1(a1,b1);
}
for(int i=; i<=n2; i++)
{
if(f[i]==i)
{
p2[cnt2].lis=num[i];
p2[cnt2++].cicl=cicl[i];
}
}
//printf("....%d %d\n",cnt1,cnt2);
printf("Case #%d: ",ca);
if(n1!=n2)
{
printf("NO\n");
continue;
}
if(cnt1!=cnt2)
{
printf("NO\n");
continue;
}
bool flag=true;
sort(p1,p1+cnt1);
sort(p2,p2+cnt2);
for(int i=; i<cnt1; i++)
{
if(p1[i].lis!=p2[i].lis||(p1[i].cicl!=p2[i].cicl))
{
flag=false;
printf("NO\n");
break;
}
}
if(flag)
{
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 In order to get rid of Conan, Kaitou KID disguis ...
- 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 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
随机推荐
- MVC之实体框架(数据持久化框架)EntityFrameWork(EF)
EF - EntityFrameWork 中文名:实体框架(数据持久化框架) 1.使用EF查询(Linq to EF) 1.1使用标准查询运算符来查询 OumindBlogEntities db = ...
- MySQL流程控制函数
官方文档:Control Flow Functions Name Description CASE Case operator IF() If/else construct IFNULL() Null ...
- 【转】adb push&pull bug --- Permission denied----不错
原文网址:http://blog.csdn.net/hengkong_horse/article/details/8708076 问题背景: adb push E:\\rrr.txt /data/ ...
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...
- POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)
题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream& ...
- js中json的转换
//aa='{"id":0,"appId":"app***********Id","appSecret":"a ...
- DotDensityRenderer
关键之处在于获取每个点所代表的的值 这里使用geodatabase类库中idatastatistic接口进行统计字段,再将结果传递给esrisysytem.istatisticsResult进行. 需 ...
- CSS彻底研究(3) - 浮动,定位
Github pages 博文 CSS彻底研究(3)-浮动,定位 一 . 浮动float I . 定义及规则 float默认为none,对应标准流的情况.当float : left;时,元素就会向其父 ...
- 黑马程序员—— Java SE(2)
----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训 ...
- Ext的labelWidth默认会给100
Ext的textfield控件的labelWidth属性,如果没有设置这个属性,那么默认会给100,导致左侧有100px的留白