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. PHP 补零操作

    str_pad(string,length,pad_string,pad_type)//参数 描述string //必需.规定要填充的字符串.length //必需.规定新的字符串长度.如果该值小于字 ...

  2. monggoDB添加到windows服务

    ----------------mongoDB安装------------------------------- 1.下载mongoDB安装包安装完毕后,配置环境变量 D:\Program Files ...

  3. Charles学习(一)之macOS Charles 4.x版本的安装、激活、使用以及软件功能了解

    前言 Charles是mac上一款比较好用的抓包工具,那么我们什么情况下需要用到抓包工具呢?比如我想查看一个接口请求的参数.返回值,还有移动设备上的http/https请求. Charles是一个HT ...

  4. springboot(十六)-swagger2

    SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API ...

  5. echarts属性的设置

    // 全图默认背景  // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87cefa','#da70d6','#32cd ...

  6. window.location.href 与 window.location.href 的区别

  7. mybatis原理解析

    本文是结合spring-mybatis整合进行的分析 1.先看看依赖的jar包: <dependency> <groupId>org.mybatis</groupId&g ...

  8. js带有遮罩的弹窗

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. maven学习之路二(2)

    上次我介绍到mybaties generator 这款mybaties的插件.这次我简单介绍下这款插件: <plugin> <groupId>org.mybatis.gener ...

  10. Linux环境测试机器端口连通性

    生产中,有很大一部分的问题都是由于不同机器间网络不同导致的,那么如何判断两台机器之间的连通性?本文介绍几种常见的方式: telnet方法wget方法ssh方法curl方法1. telnet方法格式:t ...