POJ1703 Find them, Catch them 并查集
生活随笔
收集整理的這篇文章主要介紹了
POJ1703 Find them, Catch them 并查集
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
點擊打開鏈接
Find them, Catch them
| Time Limit: 1000MS | ? | Memory Limit: 10000K |
| Total Submissions: 50757 | ? | Accepted: 15554 |
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 4Sample Output
Not sure yet. In different gangs. In the same gang.Source
POJ Monthly--2004.07.18題意:
一共有n個元素,這些元素要么屬于集合1,要么屬于集合2,
有m個操作,A? a ? b 為詢問,D a ? b 表示a,b屬于不同的集合
解法一:
利用2*n個元素建立并查集,每個元素x,有兩種表示:x和x+n
若a,b屬于不同集合,則 a和b+n屬于同一個集合,a+n和b屬于同一個集合
對于每次詢問:
若find(a)=find (b),則a和b屬于同一個集合
若find(a+n)==find(b)并且find(a)==find(b+n),則a和b屬于不同集合
#include<iostream> #include<cstdio> using namespace std; const int maxn=1e5+10; int p[maxn<<1]; int find(int x) {if(x==p[x]) return x;return p[x]=find(p[x]); } void mix(int x,int y) {int fx=find(x),fy=find(y);if(fx!=fy)p[fx]=fy; }int main() {int n,m,t;cin>>t;while(t--){//memset(vis,0,sizeof(vis));cin>>n>>m;for(int i=1;i<=2*n;i++){p[i]=i;}char op[2];int a,b;for(int i=0;i<m;i++){scanf("%s%d%d",op,&a,&b);if(op[0]=='A'){if (find(a) == find(b))printf("In the same gang.\n");else if (find(a)==find(b+n) && find(a+n)==find(b))printf("In different gangs.\n");elseprintf("Not sure yet.\n");}else{mix(a+n,b);mix(a,b+n);}}}return 0; }解法二:
帶權并查集
r數組記錄每個結點與其父親結點的關系
r[x]=0,代表x與其父親是在同一個集合
r[x]=1,代表x與其父親是在不在同一個集合
初始化r[x]=0,每個元素和自己在同一集合
1.find函數在尋找根節點時不斷更新r數組
根據子結點與父親結點的關系,以及父親結點與爺爺結點的關系,
可以知道子結點與爺爺結點的關系,具體為
若a和b的關系為rab,b和c的關系為rbc,則a和C的關系為rac=(rab+rbc)%2;
| (爺爺,父親) | (父親,兒子) | (爺爺,兒子) |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
2.union函數更新兩棵樹的關系:
定義:fx 為 x的根節點, fy 為 y 的根節點,聯合時,使得fa[ fy ] = fx;
同時也要尋找 fx 和 fy 的關系,其關系為(r[ x ]? + 1 - r[ y ]) % 2;
因為確定了 x 和 y 的關系是 1 ,因此 r[ fy ] = (r[ x ] + 1 - r[ y ]) % 2;
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 100005; int fa[maxn],r[maxn];int find(int x) {if (fa[x] == x) return fa[x];int tmp = fa[x];fa[x] = find(fa[x]);r[x] = (r[tmp] + r[x]) % 2;return fa[x]; }void unite(int x,int y) {int fx = find(x),fy = find(y);if (fx == fy) return;fa[fy] = fx;r[fy] = (r[x] + 1 - r[y]) % 2; }int main() {int T;scanf("%d",&T);while (T--){int N,M,x,y;char opt[5];scanf("%d%d",&N,&M);for (int i = 0;i <= N;i++) fa[i] = i,r[i] = 0;while (M--){scanf("%s %d %d",opt,&x,&y);if (opt[0] == 'A'){if (find(x) == find(y)){if (r[x] == r[y]) printf("In the same gang.\n");else printf("In different gangs.\n");}else printf("Not sure yet.\n");}else unite(x,y);}}return 0; }總結
以上是生活随笔為你收集整理的POJ1703 Find them, Catch them 并查集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计蒜客 百度无人车
- 下一篇: HDU2553 N皇后 回溯法+打表