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.
 /*
Name: Find them, Catch them
Copyright:
Author:
Date: 09/08/17 09:27
Description: 给定两个集合,通过已给信息,判断两个元素是否同一集合。
如果是,则输出 "In the same gang."
如果不是,则输出"In different gangs."
如果不能从已给信息判断,则输出 "Not sure yet."
*/
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int N = ;
int parent[N],rank[N]; void init() /*初始化*/
{
for(int i = ; i < N; i++)
{
parent[i] = i;
rank[i] = -;
} }
int find(int n)
{
if(n !=parent[n])
parent[n] = find(parent[n]);
return parent[n];
}
/*
使用这个函数竟然给我TLE = =、
int find(int n)
{
if(n == parent[n])
return n;
else
return find(parent[n]);
}
*/
void merge(int a,int b)
{
a = find(a);
b = find(b);
if(a != b)
parent[a] = b;
}
int main()
{
int t,m,n,a,b;
char ch;
while(cin>>t)
{
while(t--)
{
int temp = ;
init();
scanf("%d %d",&n,&m);
for(int i = ; i < m; i++)
{
getchar();
scanf("%c %d %d",&ch,&a,&b);
//cin>>Node[i].ch>>Node[i].a>>Node[i].b; 在循环次数较多情况下使用scanf 使用cin容易TLE!
if(ch == 'D')
{
if(rank[a] != -) merge(rank[a],b);
if(rank[b] != -) merge(rank[b],a);
rank[a] = b;
rank[b] = a; }else{
if( find(a) == find(b))
printf("In the same gang.\n");
else if(find(rank[a]) == find(b))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
}
}
}
return ;
}

ACM Find them, Catch them的更多相关文章

  1. ACM题目————Find them, Catch them

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  2. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  3. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  4. 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)

    转载注明原地址:http://blog.csdn.net/nk_test/article/details/49497017 少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯 ...

  5. HDU 2717 Catch That Cow(BFS)

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  6. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  7. HDU 2717 Catch That Cow(常规bfs)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...

  8. ACM卡常数(各种玄学优化)

    首先声明,本博文部分内容仅仅适用于ACM竞赛,并不适用于NOIP与OI竞赛,违规使用可能会遭竞赛处理,请慎重使用!遭遇任何情况都与本人无关哈=7= 我也不想搞得那么严肃的,但真的有些函数在NOIP与O ...

  9. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

随机推荐

  1. Vue框架axios请求(类似于ajax请求)

    Vue框架axios get请求(类似于ajax请求) 首先介绍下,这个axios请求最明显的地方,通过这个请求进行提交的时候页面不会刷新 <!DOCTYPE html> <html ...

  2. SpringMVC(三):@RequestMapping中的URL中设定通配符,可以使用@PathVariable映射URL绑定的占位符

    1)带占位符的URL是Spring3.0新增的功能,该功能在SpringMVC向REST目标挺进发展过程中具有里程碑的意义. 2)通过@PathVariable可以将URL中占位符参数绑定到控制器处理 ...

  3. Django通过pycharm创建后,如何登录admin后台?

    问题背景: 使用pycharm创建完成django项目(项目名称为:mydjangopro,app名称为my_blog) , 本想登录后台直接输入地址:http://127.0.0.1:8000/ad ...

  4. POJ-2923 Relocation---01背包+状态压缩

    题目链接: https://vjudge.net/problem/POJ-2923 题目大意: 有n个货物,给出每个货物的重量,每次用容量为c1,c2的火车运输,问最少需要运送多少次可以将货物运完 思 ...

  5. [CVPR2018] Context-aware Deep Feature Compression for High-speed Visual Tracking

    基于内容感知深度特征压缩的高速视觉跟踪 论文下载:http://cn.arxiv.org/abs/1803.10537对于视频这种高维度数据,作者训练了多个自编码器AE来进行数据压缩,至于怎么选择具体 ...

  6. Angular CLI 安装

    安装Angular 官网的教程,因为国内网络环境原因,访问不了服务器,导致安装失败. 1.先安装NodeJs 安装教程:http://blog.csdn.net/zengmingen/article/ ...

  7. [测试题]wows

    Description 山山最近在玩一款游戏叫战舰世界(steam 游戏太少了),他被大舰巨炮的魅力折服,于是山山开了一局游戏,这次发现目标是一艘战列舰新墨西哥级,舰桥很高,原本应该打在目标身后的圆形 ...

  8. [SDOI2017]数字表格

    Description Doris刚刚学习了fibonacci数列.用f[i]表示数列的第i项,那么 f[0]=0 f[1]=1 f[n]=f[n-1]+f[n-2],n>=2 Doris用老师 ...

  9. ●BZOJ 4318 OSU!

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4318题解: 期望dp 如果我们能够得到以每个位置结尾形成的连续1的长度的相关期望,那么问题就 ...

  10. ●POJ 2794 Double Patience

    题链: http://poj.org/problem?id=2794题解: 状压DP,概率 9元组表示每一堆还剩几张牌.可以用5进制状压,共5^9=1953124个状态. 令P(S)表示S这个状态被取 ...