fetaler @ 2020-01-26 17:22:41
#include <iostream>
using namespace std;
int home[10001];
int n,m;
int find(int i);
void search(int num);
void init();
int main()
{
init();
search(0);
}
void init()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
home[i]=i;
}
void search(int num)
{
int x,y,z;
if(num==m) return;
num++;
cin>>z>>x>>y;
if(z==1)
{
home[find(x)]=find(y);
}
else
{
if(find(x)==find(y)) cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
search(num);
}
int find(int i)
{
while(home[i]!=i)
{
i=home[i];
}
return i;
}
by chenxinyang2006 @ 2020-01-26 17:24:33
加上路径压缩或者启发式合并
你这样复杂度不对
by zztqwq @ 2020-01-26 17:25:29
加一个路径压缩
by critnos @ 2020-01-26 17:26:56
@鎏玥™ 把find函数改成
int find(int k)
{
while(k!=home[k]) k=home[k]=home[home[k]];
return k;
}
就过了
by tuzhewen @ 2020-01-26 17:31:07
路径压缩啊