菜鸟刚学并查集懵逼了RE+WA求助qaq

P3367 【模板】并查集

SIXIANG32 @ 2020-05-08 21:09:37

#include<iostream>
using namespace std;
int fa[100000];
int Find(int x)
{
    if(fa[x]==x)return x;
    else fa[x]=Find(fa[x]);
} 
void Union(int u,int v)
{
    int a=Find(u),b=Find(v);
    if(a!=b)fa[a]=b;
}
int main()
{
    int n,m,a,b,a1,a2,a3;
    cin>>n>>m;
    cout<<n<<' '<<m<<endl;
    for(int p=1;p<=n;p++)fa[p]=p;
    for(int p=1;p<=m;p++)
    {
        cin>>a1>>a2>>a3;
        if(a1==1)
            Union(a2,a3);
        if(a1==2)
            if(Find(a2)==Find(a3))cout<<"Y"<<endl;
            else cout<<"N"<<endl;
    }
    return 0;
}

菜鸟才学并查集,感觉也没有会RE的地方啊,思路也不是很难懂为什么WA了呢?肯定是我太菜了,根据书本打的,求助大佬QAQ


by SIXIANG32 @ 2020-05-08 21:10:24

cout<<n<<' '<<m<<endl;

这句话不要在意,我觉得我很有可能输入都错了……


by lndjy @ 2020-05-08 21:11:51

Find没return啊


by lndjy @ 2020-05-08 21:11:55

@SIXIANG


by FZzzz @ 2020-05-08 21:12:38

@SIXIANG 你 Find 里第二种情况没 return……


by SIXIANG32 @ 2020-05-08 21:12:38

@水题淹死的鱼 果然是我太菜了,谢谢


by SIXIANG32 @ 2020-05-08 21:12:53

@FZzzz 嗯……我真菜谢谢


by Happy_Orca @ 2020-05-08 21:14:07

@FZzzz 是我瞎了


by SIXIANG32 @ 2020-05-08 21:15:10

AC啦!感谢各位大佬的帮助!


by lzqy_ @ 2020-05-08 21:20:45

@SIXIANG 就在几个月前我出过同样的错误发过同样的帖


by SIXIANG32 @ 2020-05-08 21:24:29

@栾竹清影 握手


|