跳到正文

个人记录

代码

_Ascent_约 1 分钟
原文

这是 2026年3月28日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年3月14日
更新
2026年7月8日
篇幅
约 1 分钟

版本

  1. v6当前
  2. v5
  3. v4
  4. v3
更早的 2 个版本
  1. v2
  2. v1
C++
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,fa[200005],p;
void init(){
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
}
int find(int i){
	if(fa[i]==i){
		return i;
	}
	return fa[i]=find(fa[i]);
}
void unite(int x,int y){
	int fx=find(x),fy=find(y);
	if(fx==fy){
		return;
	}
	else{
		fa[fx]=fy;
	}
}
int main(){
	cin>>n>>m>>p;
	init();
	while(m--){
		cin>>x>>y;
		unite(x,y);
	}
	while(p--){
		cin>>x>>y;
		int fx=find(x),fy=find(y);
		if(fx==fy){
			cout<<"Yes\n";
		}
		else{
			cout<<"No\n";
		}
	}
	return 0;
}