题目:

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中创建一个LVC表格

    一.我们打开之前的程序,在函数里面创建一个名字为SHOW_DATA_LVCE的函数 二.点击编辑->模式 三.选择'REUSE_ALV_GRID_DISPLAY_LVC' 四.选择调用功能,点击 ...

  2. 020-PHP浏览目录

    <?php // 使用表格浏览目录的结构 print("<TABLE BORDER= '1'>"); // 创建表格的头 print("<TR&g ...

  3. 140-PHP类的抽象方法和继承

    <?php abstract class father{ //定义一个抽象类 abstract public function test(); //定义抽象方法 } class son exte ...

  4. jQuery判断输入法和非输入法输入

    需求背景: 页面需要输入完成后自动查询. 解决方案: $('input').on('input', function() { if ($(this).prop('comStart')) return; ...

  5. HDU 5480:Conturbatio 前缀和

    Conturbatio Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  6. ACM蒟蒻防bug专用 ( •̀ ω •́ )✧

    /*********************************************** * _ooOoo_ * * o8888888o * * 88" . "88 * * ...

  7. 归并排序(包含逆序数对的个数51Nod1019)

    归并排序是效率很好的排序方式,和快排效率一样高,但在稳定性上优于快排,下面我们来介绍归并排序. 归并排序运用递归将序列不断二分(其原理就是分治),就像一棵树不断向下分支,最后分到只剩一个元素,这样这个 ...

  8. 使用Docker构建基于centos7镜像的python环境

    Dcokerfile配置信息 ############################################## # 基于centos7构建python3运行环境 # 构建命令: 在Dock ...

  9. Comet OJ - Contest #15(B: 当我们同心在一起 )

    题目链接 题目描述 平面上有 nn 个坐标相异的点,请问当中有多少组非共线的三个点,这三个点的 外心 也在这 nn 个点之中? 输入描述 第一行有一个正整数 nn 代表平面上的点数. 接下来有 nn  ...

  10. UVA - 10285 Longest Run on a Snowboard(最长的滑雪路径)(dp---记忆化搜索)

    题意:在一个R*C(R, C<=100)的整数矩阵上找一条高度严格递减的最长路.起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩阵外.矩阵中的数均为0~100. 分析:dp[x ...