拿下面一道入門並查集的題作爲例子
重點在於father數組、getFather函數、union函數
這篇博文的目的是記錄下並查集的模板!
題目背景
若某個家族人員過於龐大,要判斷兩個是否是親戚,確實還很不容易,現在給出某個親戚關係圖,求任意給出的兩個人是否具有親戚關係。
題目描述
規定:x和y是親戚,y和z是親戚,那麼x和z也是親戚。如果x,y是親戚,那麼x的親戚都是y的親戚,y的親戚也都是x的親戚。
輸入格式
第一行:三個整數n,m,p,(n<=5000,m<=5000,p<=5000),分別表示有n個人,m個親戚關係,詢問p對親戚關係。
以下m行:每行兩個數Mi,Mj,1<=Mi,Mj<=N,表示Mi和Mj具有親戚關係。
接下來p行:每行兩個數Pi,Pj,詢問Pi和Pj是否具有親戚關係。
輸出格式
P行,每行一個’Yes’或’No’。表示第i個詢問的答案爲“具有”或“不具有”親戚關係。
輸入輸出樣例
輸入 #1複製
6 5 3 1 2 1 5 3 4 5 2 1 3 1 4 2 3 5 6
輸出 #1複製
Yes Yes No
說明/提示
非常簡單的並查集入門題哦!!!
直接上板子,詳解見註釋
#include<bits/stdc++.h>
using namespace std;
const int MAX = 5005;
int M,N,P;
int father[MAX];
int getFather(int x) {
if(x==father[x]) return x;
else{
//進行了路徑壓縮,遞歸更新每層的father,如果不需要壓縮路徑直接換成return getFather(father[x]);在此題中必須進行壓縮路徑,否則超時
return father[x]=getFather(father[x]);
}
}
void unionChild(int a,int b) {
int fatherA = getFather(a);
int fatherB = getFather(b);
if(fatherA!=fatherB) father[fatherA]=fatherB; //注意這裏是fatherA,而不是A,只需修改頂層的father元素,即可將兩個集合合併
}
int main(){
cin >> N >> M >> P;
for(int i=1;i<=N;i++) father[i]=i; //這裏非常重要,初始狀態每個元素的父親都是自己
while (M--) {
int m,n;
cin >> m >> n;
unionChild(m,n);
}
while (P--) {
int p,q;
cin >> p >> q;
//這裏必須用getfather,因爲在最後添加union時,修改的可能只是頂層的father元素!!!!
//不能直接用father數組!!!!
if(getFather(p)==getFather(q)) cout << "Yes" <<endl;
else cout << "No" << endl;
}
}