求助! T 了三个点

P3367 【模板】并查集

hulne @ 2020-01-05 07:50:56

蒟蒻表示不知道哪里错了,求巨佬指点

#include<bits/stdc++.h>
using namespace std;

int fa[10010], n, m, x, y, ox, oy, ins;

int find(int o){
    if(o == fa[o]) return o;
    else return find(fa[o]); 
}

int main(){
    scanf("%d%d", &n, &m);
    for(int i=1; i<=n; i++) fa[i] = i;
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d%d", &ins, &x, &y);
        ox = find(x);
        oy = find(y);
        if(ins == 1) fa[ox] = oy;
          else{
            if(ox == oy) cout<<"Y"<<"\n";
            else cout<<"N"<<"\n";
          }
    }
    return 0;
}

by hht2005 @ 2020-01-05 07:59:46

@char_yin 路径压缩


by hulne @ 2020-01-05 08:04:48

@hht2005 谢谢大佬,改过来了


by 谷民 @ 2020-01-05 08:46:18

按秩合并


by Happy_Orca @ 2020-01-05 08:55:51

改成

int find(int o){
    if(o == fa[o]) return o;
    else return fa[o]=find(fa[o]); 
}

|