Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32073   Accepted: 9890

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to.
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

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message
as described above.

Output

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

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

题目大意。给出n个人m个操作,A操作问两个人是不是在同一个集合里,D操作代表两个人不在一个集合里。
 
开一个数组d,d[i] = j,代表i所属的集合和j的集合对立。用并查集不断更新它就能够了
 
 
#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(并查集应用)的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. 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 ...

  3. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  4. POJ1703-Find them, Catch them 并查集构造

                                             Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...

  5. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  6. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  7. poj1703_Find them, Catch them_并查集

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42451   Accepted: ...

  8. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  9. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

随机推荐

  1. 网络测试工具netperf

    Netperf是一种网络性能的测量工具,主要针对基于TCP或UDP的传输.Netperf根据应用的不同,可以进行不同模式的网络性能测试,即批量数据传输(bulk data transfer)模式和请求 ...

  2. samba服务器详细配置(非域模式)

    组成Samba运行的有两个服务,一个是SMB,另一个是NMB:SMB是Samba 的核心启动服务,主要负责建立Samba服务器与Samba客户机之间的对话,验证用户身份并提供对文件和打印系统的访问,只 ...

  3. 小结: Async & Await

    新项目组用到Async & Await, 关于Await会不会新开不开线程,遇到什么情况会新开线程的问题网上查了很多资料都没看到直观的解释.现简单总结一下. 直接上代码: namespace ...

  4. GDI+(Graphics Device Interface)例子

    使用SolidBrush 单色画笔 Bitmap bitmap = new Bitmap(800, 600);            Graphics graphics = Graphics.From ...

  5. Android SDK离线安装

    Android SDK离线安装是本文要介绍的内容,主要是来了解并学习Android SDK安装的内容,具体关于Android SDK是如何离线安装的内容来看本文详解. Android开发环境,完整的说 ...

  6. Gradle 编译时选择不同的 google-services.json

    在做的安卓应用需要在 debug 和 release build中使用不同的谷歌服务账号,要用到不同的google-serivces.json ,手动替换的话太费时费力,好在万能的gradle可以完成 ...

  7. Oracle自动备份脚本

    set mydate=%date:~0,4%%date:~5,2%%date:~8,2%exp 用户名/密码@实例名 file=D:\mydata_%mydate%.dmp owner=用户名 log ...

  8. 图文混排——用表情代替"[文字]"

    1.简单设置带属性的字符串 定义一个NSMutableAttributedString带属性的字符串 NSMutableAttributedString *str = [[NSMutableAttri ...

  9. hdu Phone List

    Problem Description Given a list of phone numbers, determine if it is consistent in the sense that n ...

  10. hdu 1263 水果

    Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样J ...