【NOIP模板】 并查集
程序员文章站
2022-05-22 13:36:15
...
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m, p, r1, r2, fa[50010];
int find(int x) {
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int main() {
scanf("%d %d %d", &n, &m, &p);
for(int i = 1; i <= n; i ++) fa[i] = i;
for(int i = 1; i <= m; i ++) {
scanf("%d %d", &r1, &r2);
int p1 = find(r1);
int p2 = find(r2);
fa[p1] = p2;
}
for(int i = 1; i <= p; i ++) {
scanf("%d %d", &r1, &r2);
int p1 = find(r1);
int p2 = find(r2);
if(p1 == p2) printf("Yes\n");
else printf("No\n");
}
return 0;
}