70分TLE求助
查看原帖
70分TLE求助
984450
zhoujinhong楼主2023/9/22 14:22
#include<bits/stdc++.h>
using namespace std;
int s[10010];
int find(int t)
{
	if(t==s[t]) return t;
	else return find(s[t]);
}
void merge(int x,int y)
{
	x=find(x);
	y=find(y);
	if(x!=y) s[x]=s[y];
}
bool check(int x,int y)
{
	x=find(x);
	y=find(y);
	if(s[x]==s[y]) return 1;
	else return 0; 
}
int main()
{
	int n,m;
	int z,x,y;
	cin>>n>>m;
	for(int i=1;i<=n;i++) s[i]=i;
	while(m--)
	{
		cin>>z>>x>>y;
		if(z==1)
		{
			merge(x,y);
		}
		else
		{
			if(check(x,y)) cout<<"Y"<<'\n';
			else cout<<"N"<<'\n';
		}
	}
	return 0;
}
2023/9/22 14:22
加载中...