20分,没道理啊
查看原帖
20分,没道理啊
528151
shiyiou楼主2021/9/16 20:30
#include <iostream>

using namespace std;

int fa[100005];
int n,m;
struct graph{
	int x,y,z;
}a[100005];

void init(){
	cin>>n>>m;
	for(int i=1;i<=n;++i){
		fa[i]=i;
	}
	for(int i=1;i<=m;++i){
		cin>>a[i].z>>a[i].x>>a[i].y;
	}
}

int find(int x){
	if(x=fa[x]) return x;
	return fa[x]=find(fa[x]);
}

void join(int r1,int r2){
	int f1=find(r1),f2=find(r2);
	if(f1!=f2){
		fa[f1]=f2;
	}
}

bool judge(int x,int y){
	int f1=find(x),f2=find(y);
	if(f1==f2) return true;
	return false;
}

void solve(){
	for(int i=1;i<=m;++i){
		if(a[i].z==1){
			join(a[i].x,a[i].y);
		}else{
			if(judge(a[i].x,a[i].y)){
				cout<<"Y";
			}else{
				cout<<"N";
			}cout<<'\n';
		}
	}
}

int main(){
	init();
	solve();
	return 0;
}

#我蒙了

2021/9/16 20:30
加载中...