我对大佬不屑

P3367 【模板】并查集

Comintern @ 2021-08-10 21:28:07

来看我这个30分的代码表示难过(;′⌒`)

#include<bits/stdc++.h>
using namespace std;
long long fa[200001];
int findfa(int a){
    if(fa[a]==a)return a;
    fa[a]=findfa(fa[a]);
    return fa[a]; 
}
void bing(int a,int b){
    if(a<b)swap(a,b);
    if(findfa(a)==findfa(b))return ;
    fa[b]=a;
}
int main(){

    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)fa[i]=i;
    for(int i=1;i<=m;i++){
        int x,y,z;
        cin>>x>>y>>z;
        if(x==1){
            bing(y,z);
        }
        if(x==2){
            if(findfa(y)==findfa(z))printf("Y\n");
            else printf("N\n");
        }
    }
    return 0;
}

by RedLycoris @ 2021-08-10 21:29:58


by rui_er @ 2021-08-10 21:30:06

fa[b]=a;fa[findfa(b)]=findfa(a);


by Comintern @ 2021-08-10 21:30:17

@蒟蒻小白orz 希望有巨佬看看


by Wu_while @ 2021-08-10 21:31:16

fa[findfa(b)]=findfa(a);


by Wu_while @ 2021-08-10 21:31:56

@蒟蒻小白orz


by RedreamMer @ 2021-08-10 21:33:05


by 听取MLE声一片 @ 2021-08-10 21:33:59

什么玩意


by Nopain @ 2021-08-10 21:34:35

你合并炸了,应该是把a的树根并到b去,你这样写只并了以fa[b]为根的子树,再往上都没并


by Comintern @ 2021-08-10 21:34:41

@rui_er 谢谢(^∀^●)ノシ


by BotDand @ 2021-08-10 21:34:52


| 下一页