模板题:
https://www.luogu.com.cn/problem/P1551
题解:
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int fa[5050];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int query(int x, int y) {
int fx = find(x), fy = find(y);
if(fx == fy) return 1;
return 0;
}
void merge(int x, int y) {
int fx = find(x), fy = find(y);
if(fx == fy) return ;
fa[fx] = fy;
}
int main() {
cin >> n >> m >> p;
for(int i = 1; i <= n; i ++) {
fa[i] = i;
}
while(m --) {
int x, y;
cin >> x >> y;
merge(x, y);
}
while(p --) {
int x, y;
cin >> x >> y;
if(query(x, y)) {
cout << "Yes" << endl;
}
else cout << "No" << endl;
}
return 0;
}
标签:return,fa,int,fy,查集,fx,find
From: https://www.cnblogs.com/yishujia/p/18077080