70RE

P3367 【模板】并查集

windfall_waterfall @ 2022-02-19 14:33:40

奇奇怪怪的RE

#include<iostream>
using namespace std;
int f[5005];
void into(){
    for(int i=1 ; i<=5005 ; i++) f[i]=i;
}
int find(int x){
    if(f[x]==x) return x;
    else return f[x]=find(f[x]);
}
void merge(int x,int y){
    f[find(x)]=find(y);
}
int main(){
    int n,m;
    cin>>n>>m;
    into();
    for(int i=1 ; i<=m ; i++){
        int x,y,z;
        cin>>z>>x>>y;
        if(z==1) merge(x,y);
        if(z==2){
            if(find(x)==find(y)) cout<<"Y"<<endl;
            else cout<<"N"<<endl;
        }
    }
    return 0;
}

by shyr @ 2022-02-19 14:34:50

1\le n \le 10^4

by windfall_waterfall @ 2022-02-19 14:37:34

奇奇怪怪的WA

#include<iostream>
using namespace std;
int f[50000];
void into(){
    for(int i=1 ; i<=5005 ; i++) f[i]=i;
}
int find(int x){
    if(f[x]==x) return x;
    else return f[x]=find(f[x]);
}
void merge(int x,int y){
    f[find(x)]=find(y);
}
int main(){
    int n,m;
    cin>>n>>m;
    into();
    for(int i=1 ; i<=m ; i++){
        int x,y,z;
        cin>>z>>x>>y;
        if(z==1) merge(x,y);
        if(z==2){
            if(find(x)==find(y)) cout<<"Y"<<endl;
            else cout<<"N"<<endl;
        }
    }
    return 0;
}

by windfall_waterfall @ 2022-02-19 14:38:06

@Respons_ 奇奇怪怪的WA

#include<iostream>
using namespace std;
int f[50000];
void into(){
    for(int i=1 ; i<=5005 ; i++) f[i]=i;
}
int find(int x){
    if(f[x]==x) return x;
    else return f[x]=find(f[x]);
}
void merge(int x,int y){
    f[find(x)]=find(y);
}
int main(){
    int n,m;
    cin>>n>>m;
    into();
    for(int i=1 ; i<=m ; i++){
        int x,y,z;
        cin>>z>>x>>y;
        if(z==1) merge(x,y);
        if(z==2){
            if(find(x)==find(y)) cout<<"Y"<<endl;
            else cout<<"N"<<endl;
        }
    }
    return 0;
}

by PPL_ @ 2022-02-19 14:39:12

@Wind_Fall 老哥你数组大小都改了,初始化不改吗?


by windfall_waterfall @ 2022-02-19 14:40:12

@PPL_ 大艹


|