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
2 3 2
1 2
2 3
3 2
3 2
2 1 3 3
1 2
2 3
3 1
3 1
1 2
 
Sample Output
Case #1: YES
Case #2: NO
 
Source
 搜索:
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std; const int N = 10100;
struct node
{
int sum,cyc;
}; node sta1[N],sta2[N];
int vist[N];
vector<int>tmap[N]; bool cmp(const node &g1, const node &g2)
{
if(g1.sum < g2.sum)
return true;
else if(g1.sum == g2.sum && g1.cyc < g2.cyc)
return true;
else
return false;
} void addedg(int a,int b)
{
int flag=0;
for(int i=0;i<tmap[a].size();i++)
if(b==tmap[a][i])
{
flag=1; break;
}
if(flag==0)
tmap[a].push_back(b),tmap[b].push_back(a);
}
void init(int n)
{
for(int i=0;i<=n;i++)
{
vist[i]=0; tmap[i].clear();
}
}
void DFS(int x,int fath,node &ss)
{
ss.sum++; vist[x]=1;
for(int i=0;i<tmap[x].size();i++)
{
if(fath==tmap[x][i])continue;
if(vist[tmap[x][i]])
{
ss.cyc=1; continue;
}
DFS(tmap[x][i],x,ss);
}
} int main()
{
int t,a,b,n[2],m[2],c=0,flag;
scanf("%d",&t);
while(t--)
{
c++; flag=1;
scanf("%d%d",&n[0],&m[0]); init(n[0]);
for(int i=1;i<=m[0];i++)
{
scanf("%d%d",&a,&b);
addedg(a,b);
} int k1=0;
for(int i=1;i<=n[0];i++)
if(vist[i]==0)
{
sta1[k1].cyc=sta1[k1].sum=0;
DFS(i,-1,sta1[k1]);
k1++;
} scanf("%d%d",&n[1],&m[1]);
if(n[1]!=n[0])
flag=0;
init(n[1]);
for(int i=1;i<=m[1];i++)
{
scanf("%d%d",&a,&b);
if(flag)
addedg(a,b);
}
if(flag)
{
int k2=0;
for(int i=1;i<=n[1];i++)
if(vist[i]==0)
{
sta2[k2].cyc=sta2[k2].sum=0;
DFS(i,-1,sta2[k2]); k2++;
} if(k1!=k2)
flag=0; sort(sta1,sta1+k1,cmp);
sort(sta2,sta2+k2,cmp);
for(int i=0;i<k1;i++)
if(sta1[i].sum!=sta2[i].sum||sta1[i].cyc!=sta2[i].cyc)
{
flag=0; break;
}
}
if(flag)
printf("Case #%d: YES\n",c);
else
printf("Case #%d: NO\n",c);
}
}

并查集:
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std; const int N = 10100;
struct node
{
int sum,cyc;
}; node sta1[N],sta2[N];
int fath[N],cyc[N],sum[N]; bool cmp(const node &g1, const node &g2)
{
if(g1.sum < g2.sum)
return true;
else if(g1.sum == g2.sum && g1.cyc < g2.cyc)
return true;
else
return false;
} int findfath(int x)
{
if(x==fath[x])
return fath[x];
fath[x]=findfath(fath[x]);
return fath[x];
}
void setfath(int x,int y)
{
x=findfath(x);
y=findfath(y);
if(x==y)
cyc[x]=1;
else
fath[x]=y,sum[y]+=sum[x];
}
void init(int n)
{
for(int i=0;i<=n;i++)
{
cyc[i]=0;
sum[i]=1; fath[i]=i;
}
} int main()
{
int t,a,b,n[2],m[2],c=0,flag;
scanf("%d",&t);
while(t--)
{
c++; flag=1;
scanf("%d%d",&n[0],&m[0]); init(n[0]);
for(int i=1;i<=m[0];i++)
{
scanf("%d%d",&a,&b);
setfath(a,b);
} int k1=0;
for(int i=1;i<=n[0];i++)
if(fath[i]==i)
{
k1++;
sta1[k1].sum=sum[i];
sta1[k1].cyc=cyc[i];
} scanf("%d%d",&n[1],&m[1]);
if(n[1]!=n[0])
flag=0;
init(n[1]);
for(int i=1;i<=m[1];i++)
{
scanf("%d%d",&a,&b);
if(flag)
setfath(a,b);
}
if(flag)
{
int k2=0;
for(int i=1;i<=n[1];i++)
if(fath[i]==i)
{
k2++;
sta2[k2].sum=sum[i];
sta2[k2].cyc=cyc[i];
} if(k1!=k2)
flag=0; sort(sta1+1,sta1+k1+1,cmp);
sort(sta2+1,sta2+k2+1,cmp);
for(int i=1;i<=k1;i++)
if(sta1[i].sum!=sta2[i].sum||sta1[i].cyc!=sta2[i].cyc)
{
flag=0; break;
}
} printf("Case #%d: %s\n",c,flag>0?"YES":"NO");
}
}

HDU3926Hand in Hand(搜索 或 并查集)的更多相关文章

  1. HNUSTOJ-1674 水果消除(搜索或并查集)

    1674: 水果消除 时间限制: 2 Sec  内存限制: 128 MB提交: 335  解决: 164[提交][状态][讨论版] 题目描述 “水果消除”是一款手机游戏,相信大家都玩过或玩过类似的游戏 ...

  2. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  3. hdu 5652(并查集)

    题意:很久之前,在中国和印度之间有通路,通路可以简化为一个n*m的字符串,0表示能通过,1表示障碍,每过一年就有一个坐标变成1,问你什么时候,通路彻底无法通过: 解题思路:无向图的连通性,一般直接搜索 ...

  4. HDU1213:How Many Tables(并查集入门)

    -----------刷点水题练习java------------- 题意:给定N点,M边的无向图,问有多少个连通块. 思路:可以搜索;  可以并查集.这里用并查集练习java的数组使用,ans=N, ...

  5. Stanford Local 2016 E "Election of Evil"(搜索(正解)或并查集(划掉))

    传送门 题意: 给出集合U,V,集合U有n个元素,集合V有m个元素: 有 m 个操作,mi : s1 s2 有一条s1指向s2的边(s1,s2可能属于第三个集合,暂且称之为K集合): 指向边具有传递性 ...

  6. 2018.09.24 bzoj1016: [JSOI2008]最小生成树计数(并查集+搜索)

    传送门 正解是并查集+矩阵树定理. 但由于数据范围小搜索也可以过. 我们需要知道最小生成树的两个性质: 不同的最小生成树中,每种权值的边出现的个数是确定的 不同的生成树中,某一种权值的边连接完成后,形 ...

  7. 【NOIP模拟_54测试】【并查集】【二进制】【搜索】【区间序列类】

    第一题 Mushroom的序列 大意: 给一个序列,求一段连续最长区间满足:最多改变一个数,使得区间是严格的上升子序列. 解: 直接扫描一遍,记一个最长上升子序列编号.然后从每一个编号为1 的点来判断 ...

  8. zoj 3761(并查集+搜索)

    题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...

  9. hust 1385 islands 并查集+搜索

    islands Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 Descri ...

随机推荐

  1. axios 里面 then 默认写的function里面没有this,改成箭头函数后就可以用this了

    ,methods:{ loadJson:function(){ //this.jsonTest = "jjj" this.$http.get('http://localhost:3 ...

  2. CPP-STL:list容器

    本文以List容器为例子,介绍了STL的基本内容,从容器到迭代器,再到普通函数,而且例子丰富,通俗易懂.不失为STL的入门文章,新手不容错过! 目录 1 定义一个list 2 使用list的成员函数p ...

  3. HTML习题附答案

    第一章 1.HTML指的是(   A   ). A超文本标记语言(Hyper Text Markup Language) B家庭工具标记语言(Home Tool Markup Language) C超 ...

  4. tensorflow-gpu install check

    https://gist.github.com/mrry/ee5dbcfdd045fa48a27d56664411d41c#file-tensorflow_self_check-py-L16

  5. j数组对象去重

    var Arrlist = [ {name:"张三",age:25,time:"2018-07-30 17:45:13"}, {name:"赵六&qu ...

  6. SQL语句新建数据库

    CREATE DATABASE 语句. CREATE DATABASE Epiphany ON ( NAME = Epiphany, FILENAME = 'E:\SQL SERVER 2008\Ep ...

  7. 如何用纯 CSS 创作文本滑动特效的 UI 界面

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/QrxxaW 可交互视频教 ...

  8. java线程总结--synchronized关键字,原理以及相关的锁

    在多线程编程中,synchronized关键字非常常见,当我们需要进行“同步”操作时,我们很多时候需要该该关键字对代码块或者方法进行锁定.被synchronized锁定的代码块,只能同时有一条线程访问 ...

  9. python基础 : 1.计算机基础 2.注释 3.变量 4.标识符 5.输出 6.格式化输出 7.输入 8.算数运算符 9.字符串操作

  10. 04--activiti demo

    核心API1:ProcessEngine说明:1) 在Activiti中最核心的类,其他的类都是由他而来.2) 产生方式: ProcessEngine processEngine = ProcessE ...