MLE??

P3367 【模板】并查集

Surge_of_Force @ 2021-07-26 18:04:52

#include<bits/stdc++.h>
using namespace std;
int a[10010],zhi[10010];
int fd(int x)
{
    if(a[x]==x)
       return x;
    return a[x]=fd(x);
}
void hb(int x,int y)
{
    int r1=fd(x),r2=fd(y);
    if(x==y)
       return ;
    if(zhi[x]<zhi[y])
    {
        a[r1]=r2;
        zhi[y]+=zhi[x];
        return ;
    }
    a[r2]=r1;
    zhi[x]+=zhi[y];
    return ;
}
void judge(int x,int y)
{
    if(a[x]==a[y])
       cout<<"Y"<<endl;
    else cout<<"N"<<endl;
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        a[i]=i;
        zhi[i]=1;
    }
    for(int i=1;i<=m;i++)
    {
        int z,x,y;
        cin>>z>>x>>y;
        if(z==1)
           hb(x,y);
        else
           judge(x,y);
    }
    return 0;
}

by Surge_of_Force @ 2021-07-26 18:05:18

望大佬解答


by HanPi @ 2021-07-26 18:10:19

盲猜爆栈(


by Gokix @ 2021-07-26 18:13:53

@wapmhac

return a[x]=fd(x);

您确定不是 fd(a[x])


by HanPi @ 2021-07-26 18:14:28

fd函数写错了,

return a[x]=fd(x);
//             ^ 应该为 a[x]

by Surge_of_Force @ 2021-07-26 18:21:52

还是只有三十分啊

@Gokix


by naroanah @ 2021-07-26 18:36:48

@wapmhac 查询的时候不对吧,判断条件应为fd(x)==fd(y)


|