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. Hadoop--Hadoop的机架感知

    Hadoop的机架感知 Hadoop有一个“机架感知”特性.管理员可以手工定义每个slave数据节点的机架号.为什么要做这么麻烦的事情?有两个原因:防止数据丢失和提高网络性能.     为了防止数据丢 ...

  2. Java服务器热部署的实现原理

    转自:http://blog.csdn.net/chenjie19891104/article/details/42807959 在web应用开发或者游戏服务器开发的过程中,我们时时刻刻都在使用热部署 ...

  3. 【HDU 5532 Almost Sorted Array】水题,模拟

    给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...

  4. nodejs面试

    1. PM2相关 1. PM2的主要功能?*答案:在Node.js进程挂掉以后自动重启进程,并且能够方便的实现Node.js的集群模式* 2. 如何查看当前是否适合重启服务?*答案:pm2 monit ...

  5. flume-采集报错

    h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...

  6. magento产品导入时需要注意的事项

    (1) 必须保证csv文件是utf-8编码的.非utf-8的编码会导致产品导入失败 (2)产品图片 (a) 产品图片必须包含image,image_label,_media_image,_media_ ...

  7. Buns(dp+多重背包)

    C. Buns time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  8. 【UNIX网络编程(二)】基本TCP套接字编程函数

    基于TCP客户/server程序的套接字函数图例如以下: 运行网络I/O.一个进程必须做的第一件事就是调用socket函数.指定期望的通信协议类型. #include <sys/socket.h ...

  9. maven profile参数动态打入

    第一: 1,如果是resources目录下文件profile参数中动态打入,在pom.xml中的build标签中加入如下配置: <resources> <resource> & ...

  10. hdu3525

    题目大意:某个大学有个2个校区,此大学有n(1<=n<=10000)个运动员,这n个运动员在每个校区都挑选了m(1<=m<=10)个拉拉队.现在每个校区(A/B)中,这m*n个 ...