30pts求调
  • 板块P1551 亲戚
  • 楼主fungoo
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/6 17:44
  • 上次更新2025/2/6 20:58:04
查看原帖
30pts求调
1449313
fungoo楼主2025/2/6 17:44

30pts求调

#include<bits/stdc++.h>
using namespace std;

const int MAXN=5010;
int father[MAXN];

inline int pFind(int x)
{
	if(father[x]==x) return x;
	return father[x]=pFind(father[x]);
}

void uunion(int x,int y)
{
	if(pFind(x)!=pFind(y)) father[x]=pFind(father[y]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n,m,p;
	cin>>n>>m>>p;
	
	for(int i=1;i<=n;i++)
	{
		father[i]=i;
	}
	
	for(int i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		uunion(a,b);
	}
	
	for(int i=0;i<p;i++)
	{
		int a,b;
		cin>>a>>b;
		
		if(pFind(a)==pFind(b))
		{
			cout<<"Yes\n";
		}		
		else cout<<"No\n";
	}

	return 0;
}
2025/2/6 17:44
加载中...