P1551 亲戚
思路
模板题目
参考代码:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7;
int fa[maxn];
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y)
{
x = find(x), y = find(y);
if (x != y) fa[x] = y;
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cout.tie(0);
std::cin.tie(0);
int n, m, p, x, y;
cin >> n >> m >> p;
for (int i = 1; i <= n; i++) fa[i] = i;
while (m--)
{
cin >> x >> y;
merge(x, y);
}
while (p--)
{
cin >> x >> y;
x = find(x), y = find(y);
if (x != y) cout << "No" << endl;
else cout << "Yes" << endl;
}
return 0;
}