什么鬼全WA 我明明按照板子写的

P3367 【模板】并查集

xdedmws @ 2020-08-19 13:56:29

#include<bits/stdc++.h>
using namespace std;
int n,m;
int fa[20010];
int find(int x){
    if(fa[x]!=x) fa[x]=find(fa[x]);
    return fa[x];
}
void unionn(int r1,int r2){
    fa[r2]=r1;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++){
        int z,x,y;
        scanf("%d",&z,&x,&y);
        if(z==1){
            int r1=find(x);
            int r2=find(y);
            if(r1!=r2) unionn(r1,r2);
        }
        if(z==2)
            if(find(x)==find(y)) printf("Y\n");
            else printf("N\n");
    }
    return 0;
}

哪位dalao帮我查查错……


by JK_LOVER @ 2020-08-19 13:59:16

 scanf("%d",&z,&x,&y);

by 向晚 @ 2020-08-19 13:59:56

int find(int x){
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
要路径压缩,是
int find(int x){
if(fa[x]!=x) return fa[x]=find(fa[x]);
return fa[x];
}


by xdedmws @ 2020-08-19 14:00:46

@JK_LOVER 过了,蟹蟹(日常眼瞎


by JK_LOVER @ 2020-08-19 14:01:18

@学渣想成犇犇 这不一样?


by xdedmws @ 2020-08-19 14:01:21

@学渣想成犇犇 OK,咱去优化


by 向晚 @ 2020-08-19 14:03:10

@JK_LOVER 会快一点 反正我打的有路径压缩


by QwQcOrZ @ 2020-08-19 14:04:52

@学渣想成犇犇 楼主已经路径压缩了……


by JK_LOVER @ 2020-08-19 14:05:16

@学渣想成犇犇 看来是我要重新学习路径压缩了/kk/kel


by 向晚 @ 2020-08-19 14:06:29

@QwQcOrZ 噢 好吧 是我错了


|