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.

并查集的扩展。

//Asimple
#include <iostream>
#include <cstdio> using namespace std;
const int maxn = 100010;
int fa[maxn];//存贮根 fa[a] 存 a 的根
int r[maxn];// 存贮 fa[a] 与 a 的关系
// 0 则不在一个 gang 里, 1 表示在一个 gang 里
int T, n, m, a, b;
char ch; void make_set(int n)// 保存根
{
for(int i=1; i<=n; i++)
{
fa[i] = i ;// i 的根是 fa[i]
r[i] = 1 ;// 在同一个 gang 里
}
} int find_set(int a)// 找根节点
{
if( a == fa[a] ) return a;
else
{
int temp = fa[a] ;
fa[a] = find_set(fa[a]);
r[a] = (r[temp] + r[a] + 1 ) % 2 ;
}
return fa[a] ;
} void union_set(int a, int b)
{
int faa = find_set(a);//找根节点
int fbb = find_set(b);
if( faa != fbb )//两个根节点不同,就将其联合起来
{
fa[faa] = fbb ;
r[faa] = ( r[a] + r[b] ) % 2 ;//更新状态
}
} int main()
{
scanf("%d",&T);
while( T -- )
{
scanf("%d%d",&n,&m);
make_set(n);
while( m-- )
{
getchar();
scanf("%c%d%d",&ch,&a,&b);
if( ch == 'A' )
{
if( find_set(a) == find_set(b) )
{
if((r[a]+r[b])%2==0) cout << "In the same gang." << endl ;
else cout << "In different gangs." << endl ;
}
else cout << "Not sure yet." << endl ;
}
else union_set(a,b);
}
} return 0;
}

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

  1. ACM题目————中缀表达式转后缀

    题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...

  2. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  3. ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]

    原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...

  4. 有一种acm题目叫做,奇葩!

    本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...

  5. ACM题目————STL练习之求次数

    题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...

  6. ACM题目————zoj问题

    题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20322 解决:3560 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. ...

  7. ACM题目————又见拦截导弹

    描述 大家对拦截导弹那个题目应该比较熟悉了,我再叙述一下题意:某国为了防御敌国的导弹袭击,新研制出来一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:它的第一发炮弹能够到达任意的高度,但是以后每一发炮 ...

  8. ACM题目————还是畅通工程

    Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...

  9. ACM题目————小A的计算器

    Description 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-25分别由a-z表示.  现在小A要在这个操作系统上实现一个计算器,这 ...

随机推荐

  1. centos6关闭ipv6

    Install packages for CentOS 6.0 Minimal cat <<EOF>>/etc/modprobe.d/disable_ipv6.conf ali ...

  2. ipmi使用

    1.安装ipmitool Linux: yum -y install OpenIPMI-tools 备注:Linux机器也可以安装ipmi  yum -y install OpenIPMI OpenI ...

  3. Java基础之访问文件与目录——列出目录内容(ListDirectoryContents)

    控制台程序,列出目录的全部内容并使用过滤器来选择特定的条目. import java.nio.file.*; import java.io.IOException; public class List ...

  4. mysql:sql行列转换

    今天一个同学遇到一个问题问我了,由于本人平时学习的mysql比较基础,确实没解决,后来google了一下,才知道是sql的一种技法[行列转换],话不多说先上图: 想得到下面的结果: +------+- ...

  5. IntelliJ IDEA 常用设置讲解2

    IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 如上图 Gif 所示, ...

  6. java io读书笔记(2)什么是stream

    什么是stream?stream就是一个长度不确定的有序字节序列. Input streams move bytes of data into a Java program from some gen ...

  7. 结合DDE指标来分析成本分布的重要作用

    筹码分布集中度90和70是什么意思? (2015-08-14 12:12:27) 转载▼ 标签: 股票 分类: 成交量能.筹码分析   那个集中度90,和集中度70,90和70是什么意思??集中度最大 ...

  8. nyist 626 intersection set

    http://acm.nyist.net/JudgeOnline/problem.php?pid=626 intersection set 时间限制:1000 ms  |  内存限制:65535 KB ...

  9. sql set xact_abort on 用例

    set xact_abort on 设置事务回滚的当为ON时,如果你存储中的某个地方出了问题,整个事务中的语句都会回滚为OFF时,只回滚错误的地方 例子 : ALTER proc [dbo].[BuC ...

  10. 2016-9-6 批量给文件名的前面加上“igeek_高薪就业” 2、 利用FileInputStream和FileOutputStream复制文件

    在此只列出典型题目,有的题目扫一眼就有代码的不去浪费时间了,想要完整题目的评论留邮箱,看到就发.持续更新中... 1.批量给文件名的前面加上“igeek_高薪就业” package com.work; ...