Find them, Catch them

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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.
 #include<stdio.h>
#include<set>
#include<map>
#include<algorithm>
#include<string.h>
const int M = 1e5 + ;
int T ;
int n , m ;
int f[M] ;
int mm[M] ;
int Union (int x)
{
return f[x] == x ? x : f[x] = Union (f[x]) ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &T) ;
while (T --) {
scanf ("%d%d" , &n , &m);
memset (mm , , sizeof(mm)) ;
for (int i = ; i <= n ; i ++) f[i] = i ;
char s[] ;
int _u , _v , u , v ;
int x , y ;
int _x , _y ;
while (m --) {
scanf ("%s" , s) ;
if (s[] == 'D') {
scanf ("%d%d" , &u , &v ) ;
if (mm[u] < mm[v]) std::swap (u , v) ;
if (mm[u] && mm[v]) {
_u = mm[u] ; _v = mm[v] ;
x = Union (_u) ; y = Union (v) ;
f[y] = x ;
x = Union (_v) ; y = Union (u) ;
f[y] = x ;
}
else if (mm[u] && mm[v] == ) {
_u = mm[u] ;
x = Union (_u) ; y = Union (v) ;
f[y] = x ;
mm[v] = u ;
}
else if (mm[u] == ) {
mm[u] = v ;
mm[v] = u ;
}
}
else if (s[] == 'A') {
scanf ("%d%d" , &u , &v) ;
if (mm[u] < mm[v]) std::swap (u , v) ;
if ( !mm[u] || !mm[v]) puts ("Not sure yet.") ;
else {
x = Union (u) ; y = Union (v) ;
if (x == y) puts ("In the same gang.") ;
else {
_x = Union (mm[u]) ;
if (_x != y) puts ("Not sure yet.") ;
else puts ("In different gangs.") ;
}
}
}
}
}
return ;
}

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 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

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

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

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

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

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

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

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

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

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

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

  8. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  9. POJ 1703 Find them, Catch them (数据结构-并查集)

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

随机推荐

  1. K-D Tree

    这篇随笔是对Wikipedia上k-d tree词条的摘录, 我认为解释得相当生动详细, 是一篇不可多得的好文. Overview A \(k\)-d tree (short for \(k\)-di ...

  2. Oracle 数据库对象

    数据库对象是数据库的组成部分,常常用CREATE命令进行创建,可以使用ALTER命令修改,用DROP执行删除操作.前面已经接触过的数据库对象有表.用户等. 今天将学习更多的Oracle数据库对象: 同 ...

  3. 导入.pch文件

    Xcode5中创建一个工程的时候,系统会自动创建一个以以工程名为名字的pch(Precompile Prefix Header)文件,开发的过程中可以将广泛使用的头文件以及宏包含在该文件下,编译器就会 ...

  4. hdu 1272 小希的迷宫

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. js中substring与substr的学习。

    今天在工作的过程中,看到js中两个双胞胎函数.分别是substring与substr.顿时被两个可恶的家伙给迷惑住了,不知道具体有什么作用.. 先来看看substring手册是怎么介绍的. 手册解释的 ...

  6. webView(简单的浏览器)

    #import "MJViewController.h" @interface MJViewController () <UISearchBarDelegate, UIWeb ...

  7. 【原】react+redux实战

    摘要:因为最近搞懂了redux的异步操作,所以觉得可以用react+redux来做一个小小的项目了,以此来加深一下印象.切记,是小小的项目,所以项目肯定是比较简单的啦,哈哈. 项目效果图如图所示:(因 ...

  8. Latent Semantic Analysis (LSA) Tutorial 潜语义分析LSA介绍 一

    Latent Semantic Analysis (LSA) Tutorial 译:http://www.puffinwarellc.com/index.php/news-and-articles/a ...

  9. HTML学习笔记——块级标签、行级标签、图片标签

    1>块级标签.行级标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  10. 使用wp_editor函数实现可视化编辑器

    在最近的wp项目中遇到了需要使用可视化编辑器来接收用户的输入,正好就研究了一下wp_editor这个函数的用法,利用这个函数能很方便的把textarea文本域变成可视化编辑器. Wp_editor函数 ...