HDU3926Hand 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?"
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.
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
Case #1: YES
Case #2: NO
#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(搜索 或 并查集)的更多相关文章
- HNUSTOJ-1674 水果消除(搜索或并查集)
1674: 水果消除 时间限制: 2 Sec 内存限制: 128 MB提交: 335 解决: 164[提交][状态][讨论版] 题目描述 “水果消除”是一款手机游戏,相信大家都玩过或玩过类似的游戏 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- hdu 5652(并查集)
题意:很久之前,在中国和印度之间有通路,通路可以简化为一个n*m的字符串,0表示能通过,1表示障碍,每过一年就有一个坐标变成1,问你什么时候,通路彻底无法通过: 解题思路:无向图的连通性,一般直接搜索 ...
- HDU1213:How Many Tables(并查集入门)
-----------刷点水题练习java------------- 题意:给定N点,M边的无向图,问有多少个连通块. 思路:可以搜索; 可以并查集.这里用并查集练习java的数组使用,ans=N, ...
- Stanford Local 2016 E "Election of Evil"(搜索(正解)或并查集(划掉))
传送门 题意: 给出集合U,V,集合U有n个元素,集合V有m个元素: 有 m 个操作,mi : s1 s2 有一条s1指向s2的边(s1,s2可能属于第三个集合,暂且称之为K集合): 指向边具有传递性 ...
- 2018.09.24 bzoj1016: [JSOI2008]最小生成树计数(并查集+搜索)
传送门 正解是并查集+矩阵树定理. 但由于数据范围小搜索也可以过. 我们需要知道最小生成树的两个性质: 不同的最小生成树中,每种权值的边出现的个数是确定的 不同的生成树中,某一种权值的边连接完成后,形 ...
- 【NOIP模拟_54测试】【并查集】【二进制】【搜索】【区间序列类】
第一题 Mushroom的序列 大意: 给一个序列,求一段连续最长区间满足:最多改变一个数,使得区间是严格的上升子序列. 解: 直接扫描一遍,记一个最长上升子序列编号.然后从每一个编号为1 的点来判断 ...
- zoj 3761(并查集+搜索)
题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...
- hust 1385 islands 并查集+搜索
islands Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 Descri ...
随机推荐
- DLL动态库多次加载问题
原因涉及DLL加载和运行机制,主要有两点:1)DLL动态链接库无法独立运行,必须由一个应用程序进程加载到进程空间后才能使用.加载DLL的进程称为宿主进程.被加载的DLL属于宿主进程,不属于宿主进程内某 ...
- QT_仅仅直接在构造函数中创建对象的不可行的原因
#include "mainwidget.h" #include <QApplication> int main(int argc, char *argv[]) { Q ...
- 在cmd 命令中 用cd E:\ 进入不了E盘 问题
cd 路径 只能在本盘符中切换路径需要切换到e盘 需要cd /d e:\
- (二十)python 3 匿名函数
匿名函数lambda Python使用lambda关键字创造匿名函数.所谓匿名,意即不再使用def语句这样标准的形式定义一个函数.这种语句的目的是由于性能的原因,在调用时绕过函数的栈分配.其语法是: ...
- idea 中使用 出现 svn: E155036
在idea中使用svn checkout时 svn出现如上错误. 原因本地的工作副本太旧.command line进入本地工作副本的根目录,执行svn upgrade后 重启idea就可以了.
- STM32F407 NVIC 中断优先级管理 个人笔记
内嵌向量中断控制器:Nested Vectored Interrupt Controller (NVIC) 设置中断向量的优先级并使能. 响应优先级& 抢占优先级 抢占优先级:一个中断A还在处 ...
- PTA 03-树1 树的同构 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构 (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...
- 关于srand()rand()的用法
转自:http://baike.baidu.com/link?url=bhos65ZKp8lEq_6chSsmQv29jHrqjN_IFGVMNod6BuicQ-3oCP5VsEn3RBjXBPvA7 ...
- bzoj 1702 贪心,前缀和
[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 807 Solved: ...
- [NOIP2001] 普及组
装箱问题 裸01背包,速刷过 #include<cstdio> #include<iostream> #include<cmath> using namespace ...