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

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

主站蜘蛛池模板: 色老头av| 日本女优在线看 | 怨女1988国语版在线观看高清 | 国内精品卡一卡二卡三 | 青青操影院 | 免费黄网在线看 | aaa一区二区| 亚洲最大看欧美片网站 | www.五月婷婷.com | 极品久久| 九九精品久久 | 永久免费在线 | 日韩精品一区二区三区 | 黄色视屏免费 | 在线免费观看 | 白浆网站| 亚洲视频你懂的 | 国产小视频在线免费观看 | 久久国产香蕉视频 | 国产不卡在线视频 | 神马影院一区二区三区 | 性av在线 | 亚洲欧美日本一区 | 成人激情av| www九九热 | 国产精品香蕉在线 | 美女视频一区二区三区 | 免费无码国产v片在线观看 三级全黄做爰在线观看 | 成年人小视频 | 日本aⅴ在线观看 | xiuxiuavnet | 奇米成人影视 | 高清亚洲| 成人夜色视频 | 久久亚洲精品国产精品黑人v | jizz国产视频 | 午夜剧场福利社 | 91精品国产闺蜜国产在线闺蜜 | 9久9久9久女女女九九九一九 | 人日人视频 | 极品少妇xxxx精品少妇 | 久热这里| 村姑电影在线播放免费观看 | 久久人人爽人人爽人人av | 国产精品一区二区av | 日韩欧美高清片 | 99精品人妻无码专区在线视频区 | 国产亚洲精品成人av在线 | 另类一区二区 | 一本到在线视频 | 中文字字幕码一二三区 | 国产在线观看99 | 中文字幕国产亚洲 | 久久乐国产精品 | 亚洲五码在线 | 嫩草影院在线观看视频 | 色婷婷av一区二区三区在线观看 | 久久爱一区 | 国产乱了高清露脸对白 | 91人人澡人人爽 | 色七七在线 | 欧美寡妇性猛交 | 久久久69 | 日本公妇乱淫免费视频一区三区 | 老牛影视av一区二区在线观看 | 夏晴子在线 | 亚洲最新av网址 | 免费成人看片 | 精品毛片在线观看 | 一本在线免费视频 | 2020亚洲男人天堂 | 亚洲国产aⅴ精品一区二区的游戏 | 国产日 | 日批视频在线播放 | 亚洲精品一区二区三区在线 | 午夜精品一区二区三区在线 | 久久亚洲国产精品 | 欧美乱人伦| 日韩激情中文字幕 | 久久成人免费网站 | 天海翼中文字幕 | 91久久人澡人人添人人爽欧美 | 久操网在线 | 国产精品一卡二卡 | 女同激情久久av久久 | 国产一区二区三区18 | 欧美不卡影院 | 射死你天天日 | 在线观看亚洲a | 男人的天堂你懂的 | 无码人妻aⅴ一区二区三区日本 | 99国产精品人妻噜啊噜 | 无码内射中文字幕岛国片 | 福利视频免费看 | 午夜激情电影院 | 久久久久久久久久网 | 亚洲欧美另类中文字幕 | 高清在线一区二区三区 | 国产美女视频免费观看下载软件 |