题目:

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.

题意:

给你N个英雄,这些英雄术语两个阵营。D a b表示知道a和b在对立阵营,A a b表示询问a和b是否属于同一阵营或者不确定关系。

分析:

敌人的敌人就是朋友,用一个数组记录关系:vis[a] = b表示a的敌人是b,然后并查集把对立的人和对立的人放到一个集合里,具体看代码实现。

#include<cstdio>
using namespace std;
const int maxn = 1e5+5;
int f[maxn],vis[maxn];
int t,n,m,a,b;
int get(int x){
return f[x] == x?x : f[x] = get(f[x]);
}
void merge(int x,int y){
f[get(x)] = get(y);
}
void init(){
for (int i = 1; i <= n; i++){
f[i] = i;
vis[i] = 0;
}
}
int main(){
char ch;
scanf("%d",&t);
while (t--){
scanf("%d%d",&n,&m);
init();
for (int i = 1; i <= m; i++){
getchar();
scanf("%c",&ch);
if (ch == 'D'){
scanf("%d%d",&a,&b);
if (vis[a]) merge(vis[a],b);
if (vis[b]) merge(vis[b],a);
vis[a] = b;vis[b] = a;
}
else{
scanf("%d%d",&a,&b);
if (get(a) == get(b)) printf("In the same gang.\n");
else if (get(vis[a]) == get(b)) printf("In different gangs.\n");
else printf("Not sure yet.\n");
}
}
}
return 0;
}

POJ-1703 Find them, Catch them(并查集&数组记录状态)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 1703 Find them, Catch them 并查集,还是有点不理解

    题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...

  7. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

  8. [并查集] POJ 1703 Find them, Catch them

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

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

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

随机推荐

  1. 八十、SAP中数据库操作之 (FOR ALL ENTRIES IN )用法,比较难明白

    一.代码如下 二.查询结果如下 三.我们把it_spfli的内表内容添加两个 四.结果如下,查询的是所有的JFK和SFO机场. *&------------------------------ ...

  2. 大数据之虚拟机配置和环境准备及hadoop集群搭建

    一.VMnet1和VMnet8路由器 VMware-workstation软件选择默认安装时,会自动创建VMnet1和VMnet8路由器设备.(安装失败使用CCleaner清理vm软件) VMnet1 ...

  3. spring学习第8天(advisor)

    1.关于之前的一个错误:aop的输出顺序,实际上官方文档上并没有说是否按照书写顺序输出的,有可能你1000次里面800次按顺序输出,200是随机输出的.<aop:aspect>有一个ord ...

  4. JavaSE--日志

    参考 https://www.cnblogs.com/hanszhao/p/9754419.html https://www.cnblogs.com/chenhongliang/p/5312517.h ...

  5. BGP联邦配置

    BGP联盟建立: ①:启用BGP进程. ②:关闭同步与自动汇总. ③:router-id ④:公布自己所属联盟.——confederation identifier ID ⑤:表达自己的与其他对等(p ...

  6. 爬虫(十八):Scrapy框架(五) Scrapy通用爬虫

    1. Scrapy通用爬虫 通过Scrapy,我们可以轻松地完成一个站点爬虫的编写.但如果抓取的站点量非常大,比如爬取各大媒体的新闻信息,多个Spider则可能包含很多重复代码. 如果我们将各个站点的 ...

  7. MySQL新增数据,存在就更新,不存在就添加

    1.插入一条数据,存在就更新,不存在就更新(必须现有唯一键)使用insert ignore语句: insert ignore into table(col1,col2) values ('a','b' ...

  8. HTML拖放

    <html><head><style>.droptarget {    float: left;     width: 100px;     height: 35p ...

  9. 利用京东云Serverless服务快速构建5G时代的IoT应用

    10月31日,在2019年中国国际信息通信展览会上,工信部宣布:5G商用正式启动.5G商用时代来了! 5G的商用,使得数据传输速度.响应速度.连接数据.数据传输量.传输可靠性等方面都有了显著的提升,这 ...

  10. UVA - 1631 Locker(密码锁)(dp---记忆化搜索)

    题意:有一个n(n<=1000)位密码锁,每位都是0~9,可以循环旋转.每次可以让1~3个相邻数字同时往上或者往下转一格.输入初始状态和终止状态(长度不超过1000),问最少要转几次. 分析: ...