日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 1703】Find them, Catch them(带权并查集之--种类并查集 权为与父节点关系)

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 4

Sample 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(带权并查集之--种类并查集 权为与父节点关系)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。