30分MLE求助

P3367 【模板】并查集

__why @ 2023-07-20 19:38:47

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int f[maxn];
int find(int x){return f[x] == x ? x : f[x] = find(f[x]);}
signed main(){
    int n,m;
    cin>>n>>m;
    for(int i = 1;i<=n;i++){
        f[i] = i;
    }
    for(int i = 1;i<=m;i++){
        int x,y,z;
        cin>>x>>y>>z;
        if(x%2==0){
            if(find(y)==find(z)){
                cout<<"Y"<<endl;
            }else{
                cout<<"N"<<endl;
            }
        }else{
            f[find(y)] = f[z];
        }
    }
}

蒟蒻第一次发帖,大佬求捞


by ATZdhjeb @ 2023-07-20 19:47:20

数据范围里面有 1 \le N \le 10 ^ 4,而你的数组只开了 10 ^ 3,当然会错


by IGJHL @ 2023-07-20 19:48:15

  1. 数组开小了
  2. else{
            f[find(y)] = f[z];
    }

    应改为 f[find(y)] = find[find()z]。


by __why @ 2023-07-20 19:49:01

谢谢大佬,但为啥还是MLE


by IGJHL @ 2023-07-20 19:49:44

@wuhaoyu15 我这里过了


by __why @ 2023-07-20 19:50:57

@IGJHL 谢谢,AC了


|