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. mybatis-spring_缓存

    学习之前需要先了解一下什么是mybatis一级缓存? LZ推荐:https://blog.csdn.net/niunai112/article/details/80601793#%E4%B8%80%E ...

  2. android问题

    http://www.cnblogs.com/tianjian/category/330793.html

  3. POJ-1190-生日蛋糕(深搜,剪枝)

    生日蛋糕 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23049 Accepted: 8215 Description 7月1 ...

  4. C语言int *a 和int* a的写法

  5. [LUOGU] P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈

    https://www.luogu.org/problemnew/show/P3611 二分答案+优先队列 二分O(logn) 判一次正确性O(nlogn) 总体O(nlognlogn) 为了让pri ...

  6. tornado框架基础08-sqlalchemy表关系和简单登录注册

    01 一对一表关系 Module 需要先创建对应的 Module ,这里采用之前建立好的 User 和 UserDetails relationship from sqlalchemy.orm imp ...

  7. 教你轻松在React Native中集成统计(umeng)的功能(最新版)

    关于在react-native中快速集成umeng统计,网上的文章或者教程基本来自----贾鹏辉老师的文章http://www.devio.org/2017/09/03/React-Native-In ...

  8. Solr5.0.0定时更新索引

    由于通过配置的方式定时更新不生效,故通过代码执行定时任务更新 package com.thinkgem.jeesite.modules.meeting.task; import java.io.IOE ...

  9. 【模板】CDQ分治

    __stdcall大佬的讲解 这里贴的代码写的是点修改.区间查询的题. #include<cstdio> #include<cstring> #include<algor ...

  10. tcpcopy简单用法

    这篇文章介绍下网易开源的流量重放(replay)工具TCPCopy,说是简单介绍,绝对不是谦虚,因为自己了解的确实也不多.为什么不甚了解呢,大家可以到TCPCopy的官方仓库看看,https://gi ...