神奇的UKE

P3367 【模板】并查集

FlashHu @ 2017-11-09 14:53:13

#include<cstdio>
#define RG register
int fa[10001];
inline int in()
{
    RG char c;RG int z;
    while((c=getchar())<'-');
    z=c&15;
    while((c=getchar())>'-')
        z*=10,z+=c&15;
    return z;
}
int find(int x)
{
    if(x!=fa[x])return fa[x]=find(fa[x]);
    return x;
}
int main()
{
    RG int n=in(),m=in();
    for(;n;fa[n]=n,--n);
    while(m--)
        in()&1?fa[in()]=in():puts(find(in())==find(in())?"Y":"N");
    return 0;
}

by FlashHu @ 2017-11-09 14:54:04

只有10分QWQ

Invalid output from the executor.


by FlashHu @ 2017-11-09 14:55:08

dalao求救


by FlashHu @ 2017-11-09 15:09:01

哦噢,蒟蒻不会写并查集,改了一下就A了

int main()
{
    RG int n=in(),m=in();
    for(;n;fa[n]=n,--n);
    while(m--)
        in()&1?fa[find(in())]=find(in()):puts(find(in())==find(in())?"Y":"N");
    return 0;
}

|