Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 31102   Accepted: 9583

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

题目大意:

T组測试数据,n个人。m组询问,D a b 表示 a,b 不在同一个gang(尽管不知道gang是什么意思?) ,A a b表示a和b的关系。

解题思路:

仅仅须要并查集,再增加一个enemy数组记录某人的一个敌人就可以。

解题代码:

#include <iostream>
#include <cstdio>
using namespace std; const int maxn=110000;
int father[maxn],enemy[maxn],n,m; int find(int x){
if(father[x]!=x){
father[x]=find(father[x]);
}
return father[x];
} void combine(int x,int y){
int a=find(x),b=find(y);
father[b]=a;
} void solve(){
char ch;
int a,b;
while(m-- >0){
getchar();
scanf("%c%d%d",&ch,&a,&b);
//cout<<ch<<"->"<<a<<"->"<<b<<endl;
if(ch=='D'){
if(enemy[a]!=-1) combine(enemy[a],b);
if(enemy[b]!=-1) combine(enemy[b],a);
enemy[a]=b;
enemy[b]=a;
}else{
if(enemy[a]==-1 || enemy[b]==-1 ) printf("Not sure yet.\n");
else{
if(find(a)==find(b)) printf("In the same gang.\n");
else if(find(enemy[a])==find(b) || find(a)==find(enemy[b]) ) printf("In different gangs.\n");
else printf("Not sure yet.\n");
}
}
}
} int main(){
int t;
scanf("%d",&t);
while(t-- >0){
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++){
father[i]=i;
enemy[i]=-1;
}
solve();
}
return 0;
}

POJ 1703 Find them, Catch them (数据结构-并查集)的更多相关文章

  1. poj 1703 Find them, Catch them 【并查集 新写法的思路】

    题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...

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

    题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...

  3. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

  4. POJ 1703 Find them, Catch them (并查集)

    题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...

  5. POJ 1703 Find them, Catch them(并查集拓展)

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  6. POJ 1703 Find them, Catch them(并查集,等价关系)

    DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...

  7. POJ 1703 Find them, Catch them(种类并查集)

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  8. POJ:1703-Find them, Catch them(并查集好题)(种类并查集)

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

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

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

  10. 算法手记 之 数据结构(并查集详解)(POJ1703)

    <ACM/ICPC算法训练教程>读书笔记-这一次补上并查集的部分.将对并查集的思想进行详细阐述,并附上本人AC掉POJ1703的Code. 在一些有N个元素的集合应用问题中,通常会将每个元 ...

随机推荐

  1. TCP 协议三次握手过程解析带实例

    TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...

  2. skynet-源码分析1:目录下的文件整理

    skynet是c和lua结合的一个开源游戏引擎,是云风所写,对我等屌丝来说,是很好的参考 先整理一下文件结构,然后再慢慢深入 主目录下有10个目录,105个文件, 具体包含的情况,我简单画了个图,明天 ...

  3. Cocos2d-x优化中多线程并发訪问

    多线程并发訪问在Cocos2d-x引擎中用的不是非常多,这主要是由于中整个结构设计没有採用多线程. 源自于Objective-C的Ref对象,须要使用AutoreleasePool进行内存管理,Aut ...

  4. iOS 三维变换

    1:平移 一个4*4的单位矩阵乘以一个P(x,y,z,1)的行向量,则表示此矩阵向x轴移动了x的单位,向Y轴移动了y个单位,向Z轴移动了z个单位,最后获得移动后的目标矩阵是 [ 1, 0, 0, 0  ...

  5. JS函数的四种调用模式

    函数在js中具有四种身份,分别为函数.方法.构造函数.apply或call调用 函数调用    函数调用模式中this指全局对象(window) var f1 = function() { alert ...

  6. jQuery的主要用法

    一.选择网页元素jQuery的基本设计和主要用法,就是"选择某个网页元素,然后对其进行某种操作".这是它区别于其他函数库的根本特点. 使用jQuery的第一步,往往就是将一个选择表 ...

  7. 标准C++的vector使用

    原文:http://blog.csdn.net/pandy1110/article/details/5963908 C++内置的数组支持容器的机制,但是它不支持容器抽象的语义.要解决此问题我们自己实现 ...

  8. android开发网络连接工具类(一)

    网络连接工具类整理: package com.gzcivil.utils; import java.io.IOException; import java.util.ArrayList; import ...

  9. 读书笔记一 Java程序员的基本修养(数组及其内存管理)

    1.1 数组初始化 1.1.1 java数组是静态的 java数组被初始化之后,该数组所占的内存空间.数组长度都是不可变的. java程序中的数组必须经过初始化才可使用. 数组的初始化有两种方式: 1 ...

  10. Android 开发中的View事件监听机制

    在开发过程中,我们常常根据实际的需要绘制自己的应用组件,那么定制自己的监听事件,及相应的处理方法是必要的.我们都知道Android中,事件的监听是基于回调机制的,比如常用的OnClick事件,你了解它 ...