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. JavaSE-27 JDBC

    学习要点 JDBC 查询数据 添加数据 修改数据 删除数据 JDBC 1  JDBC的定义 JDBC是Java数据库连接技术的简称,提供连接和操作各种常用数据库的能力. 2  JDBC工作原理 3  ...

  2. packet capture

    1.下载地址:https://www.coolapk.com/apk/app.greyshirts.sslcapture

  3. TWaver可视化编辑器的前世今生(三)Doodle编辑器

    插播一则广告(长期有效)TWaver需要在武汉招JavaScript工程师若干要求:对前端技术(JavasScript.HTML.CSS),对可视化技术(Canvas.WebGL)有浓厚的兴趣基础不好 ...

  4. Node.js快速生成26个字母

    const buf1 = Buffer.allocUnsafe(26); for (let i = 0; i < 26; i++) { // 97 是 'a' 的十进制 ASCII 值. buf ...

  5. 牛客OI赛制测试赛2 D 星光晚餐

    链接:https://www.nowcoder.com/acm/contest/185/D来源:牛客网 题目描述 Johnson和Nancy要在星光下吃晚餐.这是一件很浪漫的事情. 为了增加星光晚餐那 ...

  6. POJ-2442-Sequence(二叉堆)

    POJ-2442 Description Given m sequences, each contains n non-negative integer. Now we may select one ...

  7. python UDP-数据报协议

    基于udp协议通信的套接字 服务端 from socket import * server = socket(AF_INET, SOCK_DGRAM) # SOCK_DGRAM=>数据报协议 s ...

  8. docker:安装tomcat

    文章来源:http://www.cnblogs.com/hello-tl/p/8929879.html 0.下载镜像 # docker pull tomcat:8.5 1.复制tomcat配置 先启动 ...

  9. C语言学习10

    判断三角形的类型 根据输入的三角形的三条边判断三角形的类型,并输出它的面积. #include <stdio.h> #include <math.h> void judge_1 ...

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

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