失去智商 #12

P3367 【模板】并查集

middle_set @ 2017-11-09 10:27:57

写了如下的代码

#include <cstdio>
int fa[100001];
int n,m,z,x,y;
inline void init(){for(int i=1;i<=n;++i)fa[i]=i;}
int find(int x){
    return (x==fa[x])?x:(fa[x]=find(fa[x]));
}
inline void merge(int x,int y){
    x=find(x),y=find(y);
    fa[x]=y;
}
inline bool check(int x,int y){
    return find(x)==find(y);
}
int main(){
    init();
    scanf("%d%d",&n,&m);
    while(m--){
        scanf("%d%d%d",&z,&x,&y);
        if(z==1)merge(x,y);
        if(z==2)puts(check(x,y)?"Y":"N");
    }
}

却永远输出Y


by middle_set @ 2017-11-09 10:28:36

才发现,调用init()的地方写错了……应该在scanf()之后……


by LPA20020220 @ 2017-11-09 10:44:38

...


by 朝L夕 @ 2017-11-09 11:17:12

所以。。。


|