Hand in Hand

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=3926

Problem Description

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.

Sample Input

    23 21 22 33 23 22 13 31 22 33 13 11 2

Sample Output

    Case #1: YESCase #2: NO

题意

给你两个无向图,判断是结构是否相同,每个点最多两个度。

 题解

因为每个点最多只有两个度,所以只会存在链或者环,这样就容易做了,搜索就行了。特别注意1—>2 ,2->1不能看成一个环,只是一条链,和单独的1->2是一样的,

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 10050
#define M 100050
struct Edge{int x,y,s;}G1[M],G2[M];
int last1[N],last2[N];
template<typename T>void read(T&x)
{ll k=0; char c=getchar();x=0;while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();if (c==EOF)exit(0);while(isdigit(c))x=x*10+c-'0',c=getchar();x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int dfs(int x,Edge*G,int *last,int *f)
{f[x]=1;int ans=1;for(int i=last[x];i;i=G[i].s){Edge &e=G[i];if (f[e.y])continue;ans+=dfs(e.y,G,last,f);}return ans;
}
void init(int &n,int &m,Edge*G,int *last,int *a,int &num)
{static int d[N],f[N];int tot=0;num=0;read(n); read(m);memset(f,0,sizeof(int)*(n+1));memset(d,0,sizeof(int)*(n+1));memset(last,0,sizeof(int)*(n+1));for(int i=1;i<=m;i++){int x,y;read(x); read(y);G[++tot]=Edge{x,y,last[x]};last[x]=tot;G[++tot]=Edge{y,x,last[y]};last[y]=tot;d[x]++; d[y]++;}for(int i=1;i<=n;i++)if (d[i]==1&&!f[i])a[++num]=dfs(i,G,last,f);for(int i=1;i<=n;i++)if (!f[i]){a[++num]=dfs(i,G,last,f);if (a[num]>2)a[num]+=N;}
}
void work()
{static int cas=0,n1,m1,n2,m2,a1[N],a2[N],num1,num2;init(n1,m1,G1,last1,a1,num1);init(n2,m2,G2,last2,a2,num2);sort(a1+1,a1+num1+1);sort(a2+1,a2+num2+1);for(int i=1;i<=num1;i++)if (a1[i]!=a2[i]||num1!=num2){printf("Case #%d: NO\n",++cas);return;}printf("Case #%d: YES\n",++cas);}
int main()
{
#ifndef ONLINE_JUDGEfreopen("aa.in","r",stdin);
#endifint T;read(T);while(T--)work();
}

转载于:https://www.cnblogs.com/mmmqqdd/p/11183816.html

HDU 3926 图的同构相关推荐

  1. hdu 3926 Hand in Hand (图同构)

    这题就是判断图是否同构. 题意:有n个小朋友,他们之间手牵手形成了一张图.而且不会有超过三只手牵在一起. 简单说就算给你两张图,判断两个图是否同构. 思路:因为不会有超过三只手牵在一起,既每个节点的度 ...

  2. 杭电OJ分类题目(4)-Graph

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...

  3. 图论500题 慢慢写

    题目来源 https://blog.csdn.net/ffq5050139/article/details/7832991 这篇博客用来记录自己刷的图论题 先占个坑 所有题目都来自上面的链接 会慢慢更 ...

  4. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  5. HDU——1106排序(istringstream的使用、STLvector练习)

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  6. hdu 5438 Ponds 拓扑排序

    Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...

  7. HDU 1248 寒冰王座(全然背包:入门题)

    HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...

  8. hdu 1312 Red and Black 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 第二条深搜,题目并不难,但是做了我好久好久,由于一个细节,让我赌上了一个晚上的时间. 题目大意: ...

  9. HDU 1429 胜利大逃亡(续) (BFS+位压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)  ...

最新文章

  1. 探索.NET中事件机制(续)——虚事件和事件重写问题,微软的Bug?!
  2. RedHat 6配置DNS服务实现主从同步与正反向解析
  3. 【Android RTMP】音频数据采集编码 ( FAAC 编码器编码 AAC 音频采样数据 | 封装 RTMP 音频数据头 | 设置 AAC 音频数据类型 | 封装 RTMP 数据包 )
  4. CS中using的使用-以FileStream写入文件为例
  5. MongoDB 的 upsert
  6. Linux mount 修改文件系统的读写属性
  7. web安全学习-验证机制存在的问题
  8. leetcode之回溯backtracing专题4
  9. 1.struts1.x基本action的配置与使用
  10. 故障诊断:SLES12平台数据库启动失败ORA-27300
  11. 让jquery构造出类
  12. 夏至与北回归线的故事
  13. java后端分页查询_java后端分页方案
  14. Atitit  自动化gui 与 发帖机 技术
  15. js三种消息框总结-警告框、确认框、提示框
  16. DXP2004/Altium Desinger 自己画元器件和封装,及注意事项
  17. 回顾2022! 链上NFT精彩项目大盘点
  18. Viper快速配置与上手
  19. Redis队列实现Java版秒杀系统(无脚本、可用于生产)
  20. pyqt5之眨眼检测

热门文章

  1. Delphi APP 開發入門(五)GPS 定位功能
  2. Laravel插件推荐
  3. 星际2的一些技术特性
  4. 搭建Harbor企业级docker仓库
  5. OpenMP: OpenMP编程指南
  6. FPGA:下一代机器人感知处理器
  7. 使用国内源安装k8s
  8. Unity Shader-描边效果
  9. 查看Windows系统里的进程已运行的时间
  10. 使用hexo搭建个人博客