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的更多相关文章

  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 In order to get rid of Conan, Kaitou KID disguis ...

  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. HDOJ 2111. Saving HDU 贪心 结构体排序

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

  9. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

随机推荐

  1. 迁移笔记:php缓存技术memcached

    1)memcached 的几个指令: -p监听的端口 -l连接的IP地址, 默认是本机 -d start启动memcached服务 -d restart重起memcached服务 -d stop|sh ...

  2. HDU 2457 DNA repair (AC自动机+DP)

    题意:给N个串,一个大串,要求在最小的改变代价下,得到一个不含上述n个串的大串. 思路:dp,f[i][j]代表大串中第i位,AC自动机上第j位的最小代价. #include<algorithm ...

  3. 【转】图文并茂 Ubuntu使用Thunderbird方法指南

    原文网址:http://os.51cto.com/art/201101/243445.htm Ubuntu 是一个启动速度超快.界面友好.安全性好的操作系统,它适用于桌面电脑.笔记本电脑.服务器以及上 ...

  4. c、c++知识点

    一. (1)在linux下类似uint8_t这样的文件定义在头文件<stdint.h>里面   (2)截取了stdint.h头文件里的一些常用部分 二.c++中c_str()用法 函数返回 ...

  5. poj 1759 Garland (二分搜索之其他)

    Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...

  6. hdu 5312 Sequence(数学推导+线性探查(两数相加版))

    Problem Description Today, Soda has learned a sequence whose n-th (n≥) item )+. Now he wants to know ...

  7. 表单javascript checkbox全选 反选 全不选

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. SPRING源码分析:IOC容器

    在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 - 不管怎么着,作为IOC容器,这些接口你必须要满足应用程序的最基本要求: ...

  9. java BingInteger生成2进制String循环移位时长度自动缩减

    最近在做文本处理,使用MD5 生成一段文字的MD5哈希长度为32位也即128个0-1序列. 由于需要对这个MD5值进行循环移位,显然普通的  int 是不行的,所以使用 BigInteger.但是在使 ...

  10. [转]轻量级 Java Web 框架架构设计

    工作闲暇之余,我想设计并开发一款轻量级 Java Web 框架,看看能否取代目前最为流行的而又越来越重的 Spring.Hibernate 等框架.请原谅在下的大胆行为与不自量力,本人不是为了重造轮子 ...