ykily @ 2021-04-16 22:05:37
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
ll fa[210000];
ll n,m;
inline ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
void f()
{
for(ll i=1;i<=n;++i)
fa[i]=i;
return;
}
ll find(ll m)
{
while(fa[m]!=m)
m=fa[m];
return m;
}//循环的方式
ll find(int m){
return fa[m]==m?m:fa[m]=find(fa[m]);
}//递归的方式
void concern(ll x,ll y)
{
ll baba1,baba2;
baba1=find(x);
baba2=find(y);
if(baba1!=baba2)
fa[baba1]=baba2;
return;
}
int main()
{
n=read();m=read();
ll z,x,y,sum=1;
f();
char save[210000];
for(ll i=1;i<=m;++i)
{
z=read();x=read();y=read();
if(z==1)
{
concern(x,y);
}
else
{
if(find(x)==find(y))
save[sum]='Y';
else
save[sum]='N';
++sum;
}
}
for(ll i=1;i<sum;++i)
printf("%c\n",save[i]);
return 0;
}
by ez_lcw @ 2021-04-16 22:07:51
递归有路径压缩
by _caiji_ @ 2021-04-16 22:09:15
你这个没有压缩
by iMya_nlgau @ 2021-04-16 22:20:07
循环你可以搞个栈把经过的点压到根要不复杂度不对
by _Emiria_ @ 2021-04-16 22:24:41
您这样没有路径压缩,我一般这样写
m = fa[m] = fa[fa[m]];
by Aw顿顿 @ 2021-04-16 22:24:59
递归有路径压缩,你写的这种循环做不到
by lcyxds @ 2021-04-16 23:21:34
@蒟蒻且网抑fks 这复杂度也不太对吧
by _Emiria_ @ 2021-04-16 23:31:30
@lcyxds 非递归 递归
by hly1204 @ 2021-04-17 00:37:01
循环用路径对折,复杂度一样的,看Tarjan论文