这么写为什么过不了,样例过了,提交就不行
查看原帖
这么写为什么过不了,样例过了,提交就不行
1124986
simple1first楼主2023/12/18 21:09
#include<bits/stdc++.h>
using namespace std;

const int v = 1e5+3;
int fa[v],n,m,x,y,z;

int root(int x){
	 return fa[x]==x ? x : fa[x] = root(fa[x]);
} 
int merge(int x,int y){
	fa[root(x)] = root(y);
}
bool con(int x,int y){
	return root(x)==root(y);
}
int init(int n){
	for(int i=0;i<n;i++){
		fa[i] = i;
	}
}

int main(){
	cin >> n >> m;
	init(n);
	while(m--){
		cin >> z >> x >> y;
		if(z==1)merge(x,y);
		else if(z==2)printf("%c\n",con(x,y) ? 'Y' : 'N');
	}
	
	return 0;
}
2023/12/18 21:09
加载中...