不知道哪错了

P3367 【模板】并查集

耗子i @ 2016-10-14 13:12:35

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct point
{
    int x,y;
}v[200005];
int n,m,i,a,c,b,p,fa[10005];
int find(int x)
{
    if(fa[x]!=x) fa[x]=find(fa[x]);
    return fa[x];
}
int un(int a,int b)
{
    if(find(a)!=find(b))
      fa[b]=a;
}
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++) fa[i]=i;
    for(i=1;i<=m;i++)
    {
        cin>>c>>a>>b;
        if(c==1)
        { 
            un(a,b);
        }
        if(c==2)
        {
        if(find(a)==find(b))
          cout<<"Y"<<endl;
        else 
          cout<<"N"<<endl;
        }
    }
    return 0;
}

by 青丝、暮成雪 @ 2016-10-14 14:44:28

合并那一步写错了,应该是:

int un(int a,int b)
{
    int aa=find(a);
    int bb=find(b);
    if(aa!=bb) return fa[aa]=bb;
}

by Ghost_lzy @ 2016-10-14 14:55:01

楼上正解


by 耗子i @ 2016-10-14 18:01:13

@ 青丝、暮成雪 谢谢


by 脱氧核苷酸 @ 2016-10-14 18:02:59

对哒


|