20MLE+WA求助

P3367 【模板】并查集

警策看取 @ 2021-02-05 17:53:39

#include<bits/stdc++.h>
using namespace std;
#define MAXN 10010
//#define MAXM 0

int father[MAXN];
int n,m,x,y;
int op;

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

void unionn(int a,int b){
    father[find(b)]=find(a);
}

void judge(int a,int b){
    int ansa=find(a),ansb=find(b);
    if(ansa==ansb)cout<<"Y";
    else cout<<"N";
    cout<<endl;
}

int main(){
    cin>>n>>m;
    for(int i=1;i<=n;++i){
        father[i]=i;
    }
    for(int i=1;i<=m;++i){
        cin>>op>>x>>y;
        if(op==1)unionn(x,y);
        else judge(x,y);
    }
    return 0;
}

by metaphysis @ 2021-06-11 09:01:06

@警策看取

没有错误,提交可以通过。


|