POJ1703--Find them, Catch them(种类并查集)
Posted Pic
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(种类并查集)的主要内容,如果未能解决你的问题,请参考以下文章
poj-1703-Find them, Catch them
poj 1703 Find them, Catch them