【POJ - 1703】Find them, Catch them(带权并查集之--种类并查集 权为与父节点关系)
題干:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Find them, Catch them
| Time Limit:?1000MS | ? | Memory Limit:?10000K |
| Total Submissions:?36176 | ? | Accepted:?11090 |
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.題目大意:
是一個城市有兩個幫派,A 1 2是詢問1和2這兩個人是不是一個幫派,D 1 2是認為這兩個人不在一個幫派是true的(即認為他倆不在一個幫派) 每一次A就輸出一次 思路: 帶權并查集,加一個數組rank[]表示子節點和其父節點的關系,是一個門派就是0,不是就是1
(ps:用G++語言提交,不讓用rank做數組名,,也是很不友好的)
解題報告:
? ? ?這題是帶權并查集的一個變種,叫做種類并查集,上一次訓練的題有一個蟲子談戀愛的,判斷是不是同性戀(POJ - 2492),還有最經典的種類并查集食物鏈,都是種類并查集,權均代表與祖先節點之間的關系。使用的一種向量思維把各種給定的情況用權值分開表示。比如此題rank[0]代表與祖先節點是同一幫派,rank[1]代表與祖先節點是不同幫派。
AC代碼1:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm>using namespace std; const int MAX = 1e5 + 5; int n,m; bool rank[MAX]; int f[MAX]; int getf(int v) {if(f[v] == v) return v;int tmp=f[v];f[v] = getf(f[v] );rank[v] = (rank[v] + rank[tmp] ) %2;return f[v]; } void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);if(t1!=t2) {f[t2] = t1;rank[t2] = (rank[u]+rank[v]+1)%2;//放到if外面可以嗎 應該不行吧 你讓老大和自己的關系 通過u和v這兩個小嘍啰來改變?應該不行吧,,要改變這個老大的rank 只能讓更老大的巨擘來當這個老大的頭頭。然后rank跟著巨擘來改變。 } } void init() {for(int i = 1; i<=n; i++) {f[i]=i;rank[i]=0;} } int main() {int t;char op[5];cin>>t;while(t--) {int u,v; scanf("%d %d",&n,&m);init();while(m--) {scanf("%s",op);scanf("%d %d",&u,&v);if(op[0] == 'D') {merge(u,v); }else {if(getf(u) != getf(v) ) printf("Not sure yet.\n");else {if(rank[u]==rank[v]) printf("In the same gang.\n");else printf("In different gangs.\n");} }} } return 0 ;}總結:
? ? 1. emmm 別忘了輸出最后的句點哈、、、、
?
總結
以上是生活随笔為你收集整理的【POJ - 1703】Find them, Catch them(带权并查集之--种类并查集 权为与父节点关系)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经常"咔咔"掰手指 会不会得关节炎?有人
- 下一篇: 【CodeForces - 266B 】