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. 【计算机网络】-介质访问控制子层-无线LAN

    [计算机网络]-介质访问控制子层-无线LAN 802.11体系结构和协议栈 802.11网络使用模式: 有架构模式(Infrastructure mode) 无线客户端连接接入点AP,叫做有架构模式 ...

  2. LOJ526「LibreOJ β Round #4」子集

    题目 算是比较裸的题吧. 首先我们把符合要求的\((i,j)\)建一条边,那么我们要求的就是最大团. 转化为补图的最小独立集. 然后我们来证明补图是一个二分图. \((u,v)\)有边\(\Leftr ...

  3. [NAIPC2016]Jewel Thief(决策单调性+分治)

    [NAIPC2016]Jewel Thief(决策单调性+分治) 题面 原题提交地址(题目编号H) 原题面下载地址 有\(n\)个物品,每个物品有一个体积\(w_i\)和价值\(v_i\),现在要求对 ...

  4. AppCan IDE中有时格式化代码后,代码就运行不了了。

    AppCan IDE中有时格式化代码后,代码就运行不了了.

  5. golang 组装返回json数据,提供api接口

    model里 package model type Setting struct { Key string `gorm:"primary_key" json:"key&q ...

  6. SpringBoot-2-基本配置

    自定义启动配置 在resources下面新建一个banner.txt文件,里面写入自己想要的内容 /////////////////////////////////////////////////// ...

  7. JS正则之---HTML版

    话不多说  上代码 <html><head> <meta http-equiv="Content-Type" content="text/h ...

  8. 自动化测试_百度--糯米中--携程-出行<一>

    1:接下来我们看看思路  和相应的功能 使用python+selenium+unittest完成测试脚本 打开chrome浏览器,窗口最大化,设置等待时间10s 打开百度首页 鼠标移动到更多产品,点击 ...

  9. Robot Framework(二)访问数据库

    1.在Test suit中添加Library 直接输入库名,点击确定即可 DatabaseLibrary BuiltIn 2.在用例中,连接数据库,并执行sql Connect To Database ...

  10. windows下使用zookeeper

    windows下dos窗口操作:https://blog.csdn.net/a632189007/article/details/78085858