#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define N 50010
using namespace std;
int pre[N];
int findroot(int x)
{
int temp,son;
son=x;
while(x!=pre[x])
x=pre[x];//找到根
while(son!=pre[x])//更新路径
{
temp=pre[son];
pre[son]=x;
son=temp;
}
return x;//返回根
}
void join(int a,int b)
{
a=findroot(a);
b=findroot(b);
if(a!=b)//如果两个不同则进行合并
pre[a]=b;
}
int main()
{
int n,m,p,x,y;
cin>>n>>m>>p;
for(int i=1; i<=n; i++)
pre[i]=i;
for(int i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
join(x,y);
}
for(int i=1; i<=p; i++)
{
scanf("%d%d",&x,&y);
if(findroot(x)==findroot(y))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
并查集
最新推荐文章于 2023-05-12 16:29:23 发布