P3367 【模板】并查集 20分求助
  • 板块学术版
  • 楼主CompJIN
  • 当前回复4
  • 已保存回复4
  • 发布时间2021/7/22 11:37
  • 上次更新2023/11/4 13:51:50
查看原帖
P3367 【模板】并查集 20分求助
396838
CompJIN楼主2021/7/22 11:37

RT

#include<bits/stdc++.h>
using namespace std;
unsigned int n,m,father[10010],p1,p2,p3;
int find(int x){
	if(father[x]!=x) x=father[x];
	return father[x];
}
void unionn(int r1,int r2){
	father[r2]=r1;
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		father[i]=i;
	}
	for(int i=1;i<=m;i++){
		cin>>p1>>p2>>p3;
		if(p1==1)
			unionn(p2,p3);
		else{
			if(find(p2)==find(p3))
				cout<<"Y"<<endl;
			else
				cout<<"N"<<endl;
		}
	}
	return 0;
}
2021/7/22 11:37
加载中...