谢谢daolao!

P3367 【模板】并查集

ko_no_lzx_da @ 2021-07-22 09:45:31

求大佬看看

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int baba[9999];
int a,a1,a2,m,n;
int find(int x){
    if(baba[x]==x)return x;
    return baba[x]=find(baba[x]);
} 
int main(){
    cin >>m>>n;
    for(int i=1;i<=m;i++){
        baba[i]=i;
    }
    for(int i=1;i<=n;i++){
        cin >>a>>a1>>a2;
        if(a==1){
            baba[find(a1)]=baba[a2];
        }
        if(a==2){
            if(find(a1)==find(a2)){
                cout <<"Y"<<endl;
            }else{
                cout <<"N"<<endl;
            }
        }
    }
    return 0;
}

by _caiji_ @ 2021-07-22 09:46:54

数组开小了


by 垣根帝督 @ 2021-07-22 09:49:28

daolao...


by Textbook_blasphemy @ 2021-07-22 09:51:15

@ko_no_lzx_da

if(a==1){
     baba[find(a1)]=baba[a2];
}

应为:

if(a==1){
     baba[find(a1)]=find(a2);
}

by ko_no_lzx_da @ 2021-07-22 09:53:05

感谢大佬萌!!


by EthanC_ @ 2021-07-22 09:53:24

@ko_no_lzx_da 数组开成10001


by 清风雪月 @ 2021-09-03 17:39:06

大佬!=daolao\ daolao=到老


|