Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 32909
Accepted: 10158

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.

Source

POJ Monthly--2004.07.18

题目链接:http://poj.org/problem?id=1703

思路:

种类并查集的裸题,

注意写法,很巧妙,有效地处理了更改一个节点的rank值,就必须更改此节点所在集合中所有节点的值的问题

  1 /*
2 * @FileName: D:\代码与算法\2017训练比赛\七月训练三\1005-pro.cpp
3 * @Author: Pic
4 * @Date: 2017-07-22 18:50:32
5 * @Last Modified time: 2017-07-22 21:38:49
6 */
7 #include<cstdio>
8
9 const int N = 100005;
10 int n, m, f[N], rank[N];
11
12 inline void init(){
13 for(int i=1; i<=n; ++i)
14 f[i]=i,rank[i]=0;
15 }
16
17 int find(int x){
18 if(x==f[x])return f[x];
19 int fa=f[x];
20 f[x] = find(f[x]);
21 rank[x] = (rank[x]+rank[fa])&1; //这一步的作用是将Line29做的更改传递下去
22 return f[x];
23 }
24
25 inline bool Union(int x,int y){
26 int a=find(x), b=find(y);
27 if(a==b) return false;
28 f[b] = a;
29 rank[b] = (rank[x]-rank[y]+1)&1; //rank[b]初始一定是0(初始化),若rank[x]与rank[y]一开始是相同的,则需要更改rank[b],否则不需要
30 }
31
32 int main(){
33 int T,a,b,fa,fb;
34 char ch;
35 scanf("%d",&T);
36 while(T--){
37 scanf("%d%d%*c",&n,&m);
38 init();
39 for(int i=0; i<m; ++i){
40 scanf("%c%d%d%*c",&ch,&a,&b);
41 if(ch=='D'){
42 Union(a,b);
43 }
44 else{
45 fa = find(a), fb=find(b);
46 if(fa==fb){
47 if(rank[a]==rank[b]) puts("In the same gang.");
48 else puts("In different gangs.");
49 }
50 else
51 puts("Not sure yet.");
52 }
53 }
54 }
55 return 0;
56 }
57 //
58 // _oo0oo_
59 // o8888888o
60 // 88" . "88
61 // (| -_- |)
62 // 0\ = /0
63 // ___/`---'\___
64 // .' \\| |// '.
65 // / \\||| : |||// \
66 // / _||||| -:- |||||- \
67 // | | \\\ - /// | |
68 // | \_| ''\---/'' |_/ |
69 // \ .-\__ '-' ___/-. /
70 // ___'. .' /--.--\ `. .'___
71 // ."" '< `.___\_<|>_/___.' >' "".
72 // | | : `- \`.;`\ _ /`;.`/ - ` : | |
73 // \ \ `_. \_ __\ /__ _/ .-` / /
74 // =====`-.____`.___ \_____/___.-`___.-'=====
75 // `=---='
76 //
77 //
78 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 //
80 // 佛祖保佑 永无BUG
81 //
82 //
83 //

POJ1703--Find them, Catch them(种类并查集)的更多相关文章

  1. [poj1703]Find them, Catch them(种类并查集)

    题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...

  2. POJ1703Find them, Catch them[种类并查集]

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

  3. poj1703 Find them,Catch them 【并查集】

    做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...

  4. poj1703 Find them, Catch them(并查集)

    https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...

  5. poj1703 Find them, Catch them(并查集的应用)

    Find them, Catch them   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32225   Accepte ...

  6. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

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

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  8. poj1703 Find them, Catch them(种类并查集

    题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...

  9. POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集

    题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...

  10. poj1703(种类并查集)

    题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang.   ...

随机推荐

  1. POJ - 3469 Dual Core CPU (最小割)

    (点击此处查看原题) 题意介绍 在一个由核A和核B组成的双核CPU上执行N个任务,任务i在核A上执行,花费Ai,在核B上执行,花费为Bi,而某两个任务之间可能需要进数据交互,如果两个任务在同一个核上执 ...

  2. C# 面向对象4 构造函数

    构造函数 1.构造函数用来创建对象,并且可以在构造函数中对对象进行初始化. (给对象的每个属性依次的赋值) 2.构造函数是用来创建对象的特殊方法: 1.方法名和类名一样. 2.没有返回值,连void都 ...

  3. Linux下如何查看CPU型号、个数、核数、逻辑CPU数、位数、发行版本、内核信息、内存、服务器生产厂家

    [原文链接]:http://blog.csdn.net/mdx20072419/article/details/7767809 http://blog.chinaunix.net/uid-224252 ...

  4. Ubuntu Linux 查看、编辑、比较二进制文件

    查看二进制有以下几种方法: 方法一:hexdump apt-get install libdata-hexdumper-perl 安装好之后就可以直接hexdump your_binary_file ...

  5. VIM简单操作

    ngg就跳转到第n行行首,G就到结尾 0光标移到当前行行首 $光标移到当前行行末 fx搜索当前行中下一个出现x的地方 yy复制当前行 nyy复制当前行到n-1行 dd删除当前行 ndd删除当前行到n- ...

  6. Arch Linux 安装rust

    Arch Linux 安装rust 0. 参考 Rust Toolchain 反向代理使用帮助 1. 安装 安装rustup和toolchain yaourt -S rustup rustup ins ...

  7. 脚本之SSH登录

    脚本之SSH登录 一)[python实现] 导入pxssh模块 常用的三个方法: Login() 建立ssh连接 Logout() 断开连接 Prompt() 等待系统提示符,用于等待命令执行结束 S ...

  8. 常用sql:按照表中的某一列对数据进行分组,统计数据条数

    select FROM_UNIXTIME(start_time,'%Y-%m-%d')as date,COUNT(*) FROM random_num GROUP BY FROM_UNIXTIME(s ...

  9. jenkins插件管理提示“update information obtained:不可用ago”

    jenkins插件管理遇到两个错误 (1)插件管理页面提示:There were errors checking the update sites:IOException:Unable to tunn ...

  10. datax二次开发

    从hive抽取数据,写入hbase 一.datax插件hbase12xwriter开发 查看datax源码,发现有hbase094xwriter和hbase11xwriter源码,说明datax支持h ...