大神看看为什么MLE10个点

P3367 【模板】并查集

VictorChen @ 2019-01-22 16:42:36

#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,p[10010],xi,yi,zi;
int Find(int a)
{
    if(p[a]==-1) return a;
    else return Find(p[a]); 
} 
void Union(int x,int y)
{
    x=Find(x);
    y=Find(y);
    p[x]=y;
}
int main()
{
    memset(p,-1,sizeof(p));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&zi,&xi,&yi);
        if(zi==1) Union(xi,yi);
        else if(Find(xi)==Find(yi))                printf("Y\n");
        else printf("N\n");
    }
    return 0;
}

by runtime_error @ 2019-01-22 17:00:35

@VictorChen memset(p,0xff,sizeof(p))才是-1吧


by VictorChen @ 2019-01-22 17:04:48

@runtime_error 本机调试没错呀

但我查出错了


by zerrun @ 2019-01-25 11:49:44

@VictorChen

没有路径压缩

int Find(int a)
{
    if(p[a]==-1) return a;
    else return p[a]=Find(p[a]); 
} 

这样就好了


by VictorChen @ 2019-01-26 13:30:48

@Zhouzerong thanks~


|