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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

POJ1703 Find them, Catch them 并查集

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

Sample 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;

(爺爺,父親)(父親,兒子)(爺爺,兒子)
000
011
101
110

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 并查集的全部內容,希望文章能夠幫你解決所遇到的問題。

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