做题地址:https://www.luogu.com.cn/problem/P1551
这一题主要考察并查集,是一道模板题,可以直接上手,无坑
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, m, q;
int f[N];
int find(int x){
if (x != f[x]) f[x] = find(f[x]);
return f[x];
}
int main(){
cin >> n >> m >> q;
for (int i = 1; i <= n; i ++ ) f[i] = i;
while (m -- ){
int a, b; cin >> a >> b;
f[find(a)] = find(b);
}
while (q -- ){
int a, b; cin >> a >> b;
if (find(a) == find(b)) puts("Yes");
else puts("No");
}
return 0;
}