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 ...
随机推荐
- php中对MYSQL操作之批量运行,与获取批量结果
<?php //批量运行,与获取结果 //创建一个mysqli对象 $mysqli = new MySQLi("主机名","mysqlusername". ...
- Learning Lua Programming (3) iMac下搭建Lua脚本最好的编码环境(代码补全,编译运行)
这篇文章参考自http://blog.sina.com.cn/s/blog_991afe570101rdgf.html,十分感谢原作者的伟大创造,本人亲测可行. 这篇文章记录一下如何在MAC系统环境下 ...
- Grizzly开发Echoserver实战
Grizzly开发Echoserver实战 作者:chszs,转载需注明. 博客主页:http://blog.csdn.net/chszs 用Java编写可伸缩的server应用是有难度的.用Java ...
- C#控制台吹泡泡算法
代码如下: static void Main(string[] args) { Bubbling(100, 100, "O", 1000); Console.ReadLine(); ...
- Python进阶之路---1.4python数据类型-数字
python入门基础 声明:以后python代码未注明情况下,默认使用python3.x版本 1.python代码基础:print print('hello,python') 1.1pyt ...
- Stm32高级定时器(二)
Stm32高级定时器(二) 1 主从模式:主?从? 谈论主从,可知至少有两个以上的触发或者驱动信号,stm32内部有多个定时器,可以相互之间驱动或者控制. 主模式:定时器使能只受驱动时钟控制或者输出控 ...
- 《第一行代码》学习笔记21-Git
Git(1) 1.Git是一个开源的分布式版本控制工具,其开发者是Linux操作系统的作者Linus Torvalds. 2.仓库(Repository)是用于保存版本管理所需要信息的地方,所有本地提 ...
- 如何修改UIButton按下后默认的蓝色效果
其实有两个简单方法:1.修改xib属性检查器Highlight Tint的值: 2.通过代码修改:btn.tintColor=[UIColor grayColor];或者[btn setTintCol ...
- iOS图片拉伸技巧—— resizableImageWithCapInsets
http://blog.csdn.net/chaoyuan899/article/details/19811889
- js中数组的检测方法
在js中可以使用Object.prototype.toString.call()的来检测一个对象是否为一个数组 //检测数组 var a = [1, 2]; console.log(typeof a) ...