MLE,不明白

P3367 【模板】并查集

rjh1012 @ 2024-04-13 14:43:48

#include <bits/stdc++.h>
using namespace std;
int n,m,x,y,z,fa[100000];
int find(int x){
    if(x==fa[x]){
        return x;
    }
    else {
        return fa[x]=find(x);
    }
}
void join(int x,int y){
    if(find(x)!=find(y)){
        fa[find(x)]=find(y);
    }
} 
bool judge(int x,int y){
if(find(x)==find(y)){
    return 1;
}else{
    return 0;
}
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
    for(int i=1;i<=m;i++){
        cin>>z>>x>>y;
        if(z==1){
            join(x,y);
        }else{
            if(judge(x,y)){
                cout<<"Y"<<endl;
            }else{
                cout<<"N"<<endl;
            }
        }
    }
}

??? mle??? 为什么


by zjpwdyf @ 2024-04-13 14:48:02

return fa[x]=find(x); 改为 return fa[x]=find(fa[x]);


|