求助:超过内存限制

P3367 【模板】并查集

CSP_Sept @ 2020-01-22 13:31:57

//P3367
#include <bits/stdc++.h>
using namespace std;
int f[10010];
int find(int a){
    if(f[a]==a) return a;
    f[a]=find(a);
    return f[a];
}
int main(){
    int n,m;
    cin>>n>>m;
    int x,y,z;
    for(int i=0;i<=n;i++) f[i]=i;
    for(int i=0;i<m;i++){
        cin>>z>>x>>y;
        if(z==1){
            f[find(x)]=find(y);
        }
        else{
            if(find(x)==find(y)) cout<<'Y'<<endl;
            else cout<<'N'<<endl;
        }
    }
    return 0;
}

by Aehnuwx @ 2020-01-22 13:33:49

@CSP_Sept 您的路径压缩呢...


by cnyzz @ 2020-01-22 13:34:31

 f[a]=find(a);

楼主写了个假路径压缩


by WYXkk @ 2020-01-22 13:34:52

@CSP_Sept 您find写错了,是 f[a]=find(f[a])


by CSP_Sept @ 2020-01-22 13:36:53

@Aehnuwx @菜鸟一只 @WYXkk 已解决,感谢!


by command_block @ 2020-01-22 14:37:30

写作MLE读作RE


|