POJ1703--Find them, Catch them(种类并查集)
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
题目链接: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(种类并查集)的更多相关文章
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- poj1703 Find them,Catch them 【并查集】
做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...
- poj1703 Find them, Catch them(并查集)
https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...
- poj1703 Find them, Catch them(并查集的应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32225 Accepte ...
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- poj1703 Find them, Catch them(种类并查集
题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...
- POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集
题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
- poj1703(种类并查集)
题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang. ...
随机推荐
- POJ - 1149 PIGS (建图思维+最大流)
(点击查看原题) 题目分析 (以下均为 Edelweiss 大佬的思路,博主承认自己写不了这么好,但是学习的心促使我记录下这个好题的写法,所以代码是我写的) [题目大意] 有 M 个猪圈,每个猪圈里初 ...
- 2019中山纪念中学夏令营-Day19 数论初步【GCD(最大公约数),素数相关】
关于GCD的一些定理或运用的学习: 1. 2.二进制算法求GCD 思想:使得最后的GCD没有2(提前把2提出来) 代码实现: #include <cstdio> #define int l ...
- 2019年8月23日 星期五(Workerman)
Workerman,高性能socket服务框架 Workerman是什么? Workerman是一款纯PHP开发的开源高性能的PHP socket 服务框架. Workerman不是重复造轮子,它不是 ...
- python-连接mysql实例
import pymysql # 创建连接 conn = pymysql.connect(host='192.168.71.140', port=3306, user='root', passwd=' ...
- Robot Framework(二)访问数据库
1.在Test suit中添加Library 直接输入库名,点击确定即可 DatabaseLibrary BuiltIn 2.在用例中,连接数据库,并执行sql Connect To Database ...
- Postman之简单使用
前提:已获得接口文档 / 抓包数据 1.启动Postman 直接在这个页面输入数据(不用管其他的地方!!!) 2.按照接口文档填入 注意蓝色框中的数据 请求方式:POST(几乎都是使用POST/GET ...
- Skills CodeForces - 613B (双指针)
大意: $n$门课, 第$i$门分数$a_i$, 可以增加共$m$分, 求$cnt_{mx}*cf+mi*cm$的最大值 $cnt_{mx}$为满分的科目数, $mi$为最低分, $cf$, $cm$ ...
- 解决mac下brew install报错
Error: Another active Homebrew update process is already in progress.Please wait for it to finish or ...
- golang(3):strings和strconv使用 & 时间和日期类型 & 指针类型 & 流程控制 & 函数
strings和strconv使用 . strings.HasPrefix(s string, prefix string) bool: // 判断字符串s是否以prefix开头 . . string ...
- debezium关于cdc的使用(下)
博文原址:debezium关于cdc的使用(下) 简介 debezium在debezium关于cdc的使用(上)中有做介绍.具体可以跳到上文查看.本篇主要讲述使用kafka connector方式来同 ...