###哪里错了?

P3367 【模板】并查集

yingzifan @ 2016-12-09 19:44:19

#include<cstdio>
int n,m,father[100001];
int add(int a,int b){father[a] = b;}
int find(int a){
    if(father[a] != a)
        father[a] = find(father[a]);
    return father[a];
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i = 1;i <= n;i++)
        father[i] = i;
    int a,b,c;
    for(int i = 1;i <= m;i++){
        scanf("%d %d %d",&a,&b,&c);
        int b1 = find(b),c1 = find(c);
        if(a == 1)
            if(b1 != c1)
                add(b,c);
        if(a == 2){
            if(b1 == c1)printf("Y\n");
            else printf("N\n");
        }
    }
    return 0;
}
- 求大神!

by yingzifan @ 2016-12-09 20:26:03

突然发现应该是:

         if(a == 1)
            if(b1 != c1)
                add(find(b),find(c));

|