AcWing 1249. 亲戚
原题链接
简单
作者:
赐荒
,
2025-04-15 15:12:58
· 新疆
,
所有人可见
,
阅读 1
#include<iostream>
using namespace std;
const int N = 20010;
int p[N];
int n,m;
int find(int x)
{
if(p[x]!=x) return p[x]=find(p[x]);
else return p[x];
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i = 1;i<=n;i++)
{
p[i]=i;
}
while (m -- )
{
int a,b;
cin>>a>>b;
p[find(a)]=find(b);
}
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
if(find(a)==find(b)) puts("Yes");
else puts("No");
}
return 0;
}