点击此处打开原题链接:https://vijos.org/p/1034
附AC代码:
#include<stdio.h>
#define M 50001
int father[M],a,b,m,n,p;
int find(int x){
if (father[x]!=x) father[x]=find(father[x]);
return father[x];
}
int main(){
int i;
scanf("%d%d%d",&n,&m,&p);
for (i=1;i<=n;i++) father[i] = i;
for (i=1;i<=m;i++){
scanf("%d%d",&a,&b);
a=find(a),b=find(b);father[a]=b;
}
for(i=1;i<=p;i++){
scanf("%d%d",&a,&b);
a=find(a);b=find(b);
if(a==b)printf("Yes\n");else printf("No\n");
}
return 0;
}