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. [转]Geoserver全面了解

    From:http://www.cnblogs.com/ada-zheng/archive/2013/07/16/3192921.html Geoserver是一个功能齐全,遵循OGC开放标准的开源W ...

  2. JIRA官方:JIRA亮点介绍

    操作超级简单 简单不意味着要以牺牲功能作为代价.JIRA提供了友好.直观的可配置的Web界面,并支持大量的快捷键操作. 跟踪任何事务 跟踪问题.任务.需求,当然还有软件缺陷.定义你自己的事务类型来使之 ...

  3. OpenStack high-level Functionsenabled

  4. MapReduce工作机制

    MapReduce是什么? MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,MapReduce程序本质上是并行运行的,因此可以解决海量数据的计算问题. MapReduce ...

  5. 依赖注入及AOP简述(二)——工厂和ServiceLocator .

    2.2.    工厂模式 基于手工构建组件的诸多弱点,1995年“大师4人组”(GoF)在其经典著作<DesignPatterns>一书中提出了“工厂模式”,这种模式在一定程度上有效的解决 ...

  6. 新发现。css3控制浏览器滚动条的样式

    &::-webkit-scrollbar-track { background-color: #7e7e7e; } &::-webkit-scrollbar { width: 14px ...

  7. RDLC报表系列(二) 行分组

    接上一篇文章的内容,今天来说的是行分组.还是打开demo1.rdlc界面,拖入一个文本框和表 1.在表中随便选择一个字段,不然在添加行组的时候不会自动提示.我这里是选择的Dept 2.在下面的行组中右 ...

  8. Linux 08

    1按照视频里提出的几点要求完善使ls命令模仿windows下dir命令输出的脚本 {printf $6" "$7" \t";if (substr($1,1,1) ...

  9. [Linked List]Reverse Linked List,Reverse Linked List II

    一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...

  10. (转)OpenGL中位图的操作(glReadPixels,glDrawPixels和glCopyPixels应用举例)

    (一)BMP文件格式简单介绍 BMP文件是一种像素文件,它保存了一幅图象中所有的像素.这种文件格式可以保存单色位图.16色或256色索引模式像素图.24位真彩色图象,每种模式种单一像素的大小分别为1/ ...