题目:

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. 10.swoole学习笔记--进程队列通信

    <?php //进程仓库 $workers=[]; //最大进程数 $worker_num=; //批量创建进程 ;$i<$worker_num;$i++){ //创建子进程 $proce ...

  2. 026-PHP常用字符串函数(三)

    <?php //颠倒字串 print("abcdefg 颠倒 "); print(strrev("abcdefg")."<hr>&q ...

  3. jQuery原理系列-工具函数

    jquery源码中有很多精妙的实现,对于我们每天都在使用的东西,一定要知其原理,如果遇到不能使用jquery环境,也能自己封装原生的代码实现. 1.检测类型 众所周知typeof 不能用来检测数据,会 ...

  4. 【转】在C#中?,?:和??

    符号:?名称:可空类型修饰符.引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=null; 编译器就会报错.为了使值类型 ...

  5. web前端知识点

    一.CSS问题 1.flex布局 display:flex; 在父元素设置,子元素受弹性盒影响,默认排成一行,如果超出一行,按比例压缩 flex:1; 子元素设置,设置子元素如何分配父元素的空间,fl ...

  6. js(JavaScript)使用${pageContext.request.contextPath}报错

    前几天写程序在js文件中用到了${pageContext.request.contextPath}然后一直报错,没有办法post到服务器,原来js把这个当成字符串了,一直以为他是jquery的函数! ...

  7. CMD命令实现数字雨

    首先,我们在桌面上先创建一个“.bat”格式的文件,文件名为“数字雨.bat”,创建好了之后右键点击编辑. 在其中输入: @echo off title 数字雨 color 0a setlocal E ...

  8. 吴裕雄--天生自然 JAVASCRIPT开发学习:比较 和 逻辑运算符

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. hadoop搭建一:虚拟机网络配置和基础(未完成)

    基于VMware 15+CentOS 7+Hadoop 2.6,hadoop的搭建主要用于个人学习,水平有限. hadoop搭建一:虚拟机网络配置和基础 hadoop搭建二:hadoop全分布搭建 h ...

  10. INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决

    Target device: smartisan-yq601-3fa1a5dcInstalling APK: /Users/wangliang/workspace/emm-android/build/ ...