RT,代码如下
#include <bits/stdc++.h>
using namespace std;
int n, m, p, x, y, fa[5010];
int find (int x) {
if (x == fa[x])
return x;
return fa[x] = find(fa[x]);
}
void join(int c1, int c2) {
int f1 = find(c1), f2 = find(c2);
if (f1 != f2)
fa[f1] = f2;
}
int main() {
cin >> n >> m >> p;
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 0; i < m; i++) {
cin >> x >> y;
join(x, y);
}
for (int i = 0; i < p; i++) {
cin >> x >> y;
if (find(x) == find(y))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}