20分,没道理啊

P3367 【模板】并查集

shiyiou @ 2021-09-16 20:30:10

#include <iostream>

using namespace std;

int fa[100005];
int n,m;
struct graph{
    int x,y,z;
}a[100005];

void init(){
    cin>>n>>m;
    for(int i=1;i<=n;++i){
        fa[i]=i;
    }
    for(int i=1;i<=m;++i){
        cin>>a[i].z>>a[i].x>>a[i].y;
    }
}

int find(int x){
    if(x=fa[x]) return x;
    return fa[x]=find(fa[x]);
}

void join(int r1,int r2){
    int f1=find(r1),f2=find(r2);
    if(f1!=f2){
        fa[f1]=f2;
    }
}

bool judge(int x,int y){
    int f1=find(x),f2=find(y);
    if(f1==f2) return true;
    return false;
}

void solve(){
    for(int i=1;i<=m;++i){
        if(a[i].z==1){
            join(a[i].x,a[i].y);
        }else{
            if(judge(a[i].x,a[i].y)){
                cout<<"Y";
            }else{
                cout<<"N";
            }cout<<'\n';
        }
    }
}

int main(){
    init();
    solve();
    return 0;
}

我蒙了


by 镜音连 @ 2021-09-16 20:31:58

@shiyiou

    if(x=fa[x]) return x;

这里错了 , 然后数组开小了


by shiyiou @ 2021-09-16 20:37:10

@镜音连 已改正,谢谢


|