题目链接
直接上代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int s[5200];
int f(int x) {
if (x != s[x]) s[x] = f(s[x]);
return s[x];
}
void mm(int x, int y) {
int xx = f(x);
int yy = f(y);
if (xx != yy) s[xx] = s[yy];
}
void solve() {
for (int i = 1; i <= 5199; i++) s[i] = i;
int n, m, p;
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
mm(x, y);
}
while (p--) {
int aa, bb;
cin >> aa >> bb;
if (f(aa) == f(bb)) cout << "Yes" << "\n";
else cout << "No" << "\n";
}
}
int main() {
std::cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}