并查集模版题
#include <iostream>
using namespace std;
const int N = 20010;
int p[N], n, m, q, u, v;
int find(int x) {
return p[x] ^ x ? p[x] = find(p[x]) : x;
}
int main(void) {
for (int i = 0; i < N; ++i) p[i] = i;
scanf("%d %d", &n, &m);
while (m--) {
scanf("%d %d", &u, &v);
p[find(u)] = p[find(v)];
}
scanf("%d", &q);
while (q--) {
scanf("%d %d", &u, &v);
puts(find(u) == find(v) ? "Yes" : "No");
}
return 0;
}
请讲解,谢谢!