_谦退_ @ 2018-12-09 20:42:47
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
int f[1001],a[1001],b[1001];
int findd(int x)
{
if(x==f[x]) return x;
return findd(f[x]);
}
void joinn(int x,int y)
{
x=findd(x);
y=findd(y);
f[x]=y;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=1;i<=m;i++)
{
cin>>k>>a[i]>>b[i];
if(k==1)
{
joinn(a[i],b[i]);
}
if(k==2)
{
if(findd(a[i])==findd(b[i])) cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}
return 0;
}
by Froggy @ 2018-12-09 20:48:59
数组开太小了
by RiverFun @ 2018-12-09 21:00:28
@谦退 数组开大
by RiverFun @ 2018-12-09 21:00:58
然后再加一个路径压缩