poj1703--Find them, Catch them(并查集应用)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32073 | Accepted: 9890 |
Description
The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
as described above.
Output
Sample Input
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
Sample Output
Not sure yet.
In different gangs.
In the same gang.
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 110000
int c[maxn] , d[maxn] ;
int find1(int x)
{
if( c[x] != x )
{
c[x] = find1(c[x]) ;
d[x] = d[ c[x] ] ;
}
return c[x] ;
}
int main()
{
int t , n , m , i , j ;
char str[10] ;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &n, &m);
for(i = 1 ; i <= n ; i++)
c[i] = i ;
memset(d,-1,sizeof(d));
while(m--)
{
int a , b , x , y , xx , yy ;
scanf("%s %d %d", str, &a, &b);
x = find1(a) ;
y = find1(b) ;
if( str[0] == 'D' )
{
if(d[x] == -1 && d[y] == -1)
{
d[a] = b ; d[b] = a ;
}
else
{
if( d[x] != -1 )
{
if( d[y] != -1 )
{
xx = d[y] ;
xx = find1(xx) ;
c[xx] = x ;
d[xx] = d[x] ;
}
c[y] = d[x] ;
d[y] = x ; }
else
{
if( d[x] != -1 )
{
yy = d[x] ;
yy = find1(yy) ;
c[yy] = y ;
d[yy] = d[y] ;
}
c[x] = d[y] ;
d[x] = y ;
}
}
}
else
{
if( x == y )
printf("In the same gang.\n");
else if( d[x] == -1 || d[y] == -1 || d[x] != y || d[y] != x )
printf("Not sure yet.\n");
else if( d[x] == y || d[y] != x )
printf("In different gangs.\n"); }
}
}
return 0;
}
poj1703--Find them, Catch them(并查集应用)的更多相关文章
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- POJ-1703 Find them, Catch them(并查集&数组记录状态)
题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ1703-Find them, Catch them 并查集构造
Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
随机推荐
- 寒哥细谈之AutoLayout全解
文/南栀倾寒(简书作者)原文链接:http://www.jianshu.com/p/683fbcbfb705著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 看到群中好多朋友还停留在Fr ...
- eclipse 404以及tomcat failed to start错误
eclipse中的servlet项目有时会不编译,不编译可能就会出现404错误,因为在build path的输出目录并没有class文件,然而如果在输出目录引入之前编译的class文件,就可能出现cl ...
- URLScan安装及配置(转)
安装 URLScan 要安装 URLScan,请访问下面的 Microsoft Developer Network (MSDN) 网站: http://msdn2.microsoft.com/en-u ...
- Temporary ASP.NET Files 文件夹中保存的是什么内容?[转]
转自:http://www.cnblogs.com/suiqirui19872005/archive/2007/05/14/746320.html ASP.NET 页面请求的处理过程需要使用一些临时文 ...
- Jquery:强大的选择器<一>
今天回家之后,学习的是Jquery的选择器.选择器作为Jquery的优势之一,确实让我感觉到了它的强大.Jquery选择器分为基本选择器.层次选择器.过滤选择器和表单选择器,下面我一一介绍这四种选择器 ...
- 《第一行代码》学习笔记16-碎片Fragment(1)
1.碎片( Fragment):一种可以嵌入在活动当中的UI片段,能让程序更加合理和充分地利用大屏幕的空间,在平板上的应用广泛. 2.碎片同样包括布局,有自己的生命周期,甚至可理解成一个迷你型的活动. ...
- 《转载》深入理解 cocos2d-x 坐标系
原文地址:http://www.cnblogs.com/lyout/p/3292702.html. 首先我们添加两个测试精灵(宽:27,高:40)到场景里面: CCSprite *sprite1 = ...
- 不同频率下的pwm配置
200k //PWM1 PWMPERDL1=0xb3; PWMPERDH1= 0x00; PWMCCNTL1=0x6B; PWMCCNTH1= ; PWMDBDY1=0x2B; //死区延时计时器 / ...
- 记“debug alipay”一事
背景:客户支付成功,无法返回支付结果 ===================================== 查找原因,追踪代码: verified = AlipayNotify.verify(p ...
- ecshop中404错误页面设置
在ecshop系统当中,比如你随意将商品详细页面的地址中的ID修改为一个不存在的商品ID,ecshop会自动跳转到首页.ecshop在这方面做得非常的差,甚至导致了很多的站不被搜索引擎收录.最模板提供 ...