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. vs2010的资源视图中,对话框显示数字的解决方法之一

    以上是不正常显示. 我这次遇到该问题的原因是资源名IDD_DLG_INTENSITY重复定义导致的, 所以在resource.h文件中去除重复定义就好了. 正常应该显示DD_XXX,如下图所示

  2. MFC模态框关闭时出现断言报错!

    我尝试一个老的主对话框上创建另一个新的模态对话框并结束对话框,然后包含创建模态对话框的函数体执行结束时,我出现了这个断言失败! 原因:使用PostQuitMessage(1),导致出现上述问题:或者别 ...

  3. 最短路 || Codeforces 938D Buy a Ticket

    题意:从城市u到v(双向)要花w钱,每个城市看演唱会要花不同的门票钱,求每个城市的人要看一场演唱会花费最少多少(可以在这个城市看,也可以坐车到别的城市看,然后再坐车回来) 思路:本来以为是多源..实际 ...

  4. RPM Package Manager

    本文大部分内容来自鸟哥的Linux私房菜,并且由作者根据自己的学习情况做了一些更改,鸟哥原文链接 1. 程序的安装方式 源代码安装:利用厂商释出的Tarball 来进行软件的安装,每次安装程序都需要检 ...

  5. js实现复制粘贴功能

    在项目中使用到复制粘贴功能,虽然网上有很多大牛封装了很多的插件,但是还是想不去使用插件,就像自己来实现这个功能. 另一篇是禁止复制粘贴 前端er怎样操作剪切复制以及禁止复制+破解等 初步想法: 1. ...

  6. idea Error:(65, 27) java: 未结束的字符串文字

    今天在使用IDEA的时候,出现了这个错误,原因项目文件编码不一致导致的,解决方法是: 将项目的文件编码全改成一致(UTF-8),如下图所示:

  7. PADS规则设计-对某一网络/元件单独设置规则

    转载请注明出处,并附带本文网址https://www.cnblogs.com/brianblog/p/9894867.html, 在PADS规则设计中可能会遇到某个走线与另一个走线之间的间距,普通规则 ...

  8. ASP.NET MVC如何在页面加载完成后ajax异步刷新

    背景:之前已写过两篇有关Ajax的随笔,这一篇是单独针对在页面加载完成的Ajax操作.比如说打开学生列表页面,先加载页面,然后以Ajax的方式,从数据库中检索相应的学生信息,给浏览者更好的体验. 简单 ...

  9. luogu2633 Count on a tree

    主席树放到树上而已 #include <algorithm> #include <iostream> #include <cstdio> using namespa ...

  10. css position是前端的你了解多少?

    此文根据Steven Bradley的<How Well Do You Understand CSS Positioning?>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之处 ...