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. ...
随机推荐
- python使用Flask作为MockServer的方法
日常开发/测试过程中,需要对相关服务添加挡板--Mock 简单介绍一下使用python的Flask插件,对相关的服务进行Mock # coding:utf-8 import os from flask ...
- Ngnx工作原理(1)
Nginx 是一个轻量级的HTTP 服务程序,相比其他服务器程序如Apache,Nginx占用内存少,稳定性高,并发处理能力强.同时Nginx 还是一个反向代理服务程序,和邮件代理服务程序.Nginx ...
- vscod插件
Babel JavaScript Code Runner Debugger for Chrome ESLint HTML CSS Support HTML Snippets background J ...
- SpringBoot:spring boot使用Druid和监控配置
Druid是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能. Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource 业 ...
- redis性能指标
1.当内存使用达到设置的最大阀值时,需要选择一种key的回收策略,可在Redis.conf配置文件中修改“maxmemory-policy”属性值. 若是Redis数据集中的key都设置了过期时间,那 ...
- apacheTop
1.监控 httpd 请求数据,请求统计 apachetop -f /var/www/access_log 2. apachetop -H hits (Will display stats on th ...
- maven学习之路三
我们在写代码的时候,有些项目会有重复代码,或者是重复项目结构,这样我们就可以用maven 生成一个项目的基本骨架,就像我之前介绍的哪个logindemo一样继承了webApp-achetype一样.我 ...
- 01--springmvc分布式项目Web项目配置
springmvc的配置文件,放在resources目录下: 文件名:applicationContext-mvc.xml <?xml version="1.0" encod ...
- xhost + command not found
如下是一个示例: 原本我以为在没有联网的情况下.不能使用yum 的.可能是本地配置了yum 了吧也可以使用 1. [oracle@11GR2-test ~]$ export DISPLAY=192.1 ...
- lightinthebox 批量设置分类产品排列方式为List、Grid、Gallery
lightinthebox 批量设置分类产品排列方式为Grid categories_type = '1'表示List,2表示Grid,3表示Gallery方式 设置单个分类 ; ; ; 设置全部 ' ...