全部MLE求助

P3367 【模板】并查集

VictorChen @ 2021-02-07 20:34:17

//P3367 【模板】并查集  

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int n,m;
int op,x,y;
int p[10010];

int Find(int i)
{
    if(p[i] < 0) return i;
    p[i] = Find(p[i]);
    return p[i];
}

void Union(int i,int j)
{
    i = Find(i);
    j = Find(j);
    if(p[i] < p[j]) swap(i,j);
    p[j] += p[i];
    p[i] = j;
    return;
}

int main()
{
    memset(p,-1,sizeof(p));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&op,&x,&y);
        if(op == 1)
            Union(x,y);
        else if(op == 2)
            if(Find(x) == Find(y)) printf("Y\n");
            else printf("N\n");
    }
    return 0;
}

10个点全部MLE了……本地上验证代码没有问题


by kebingyi @ 2021-02-07 20:37:51

memset(p,-1,sizeof(p));

或许应该是:

for(int i=1;i<=n;i++) p[i]=i;

by Dovuq @ 2021-02-15 14:45:37

铜球


|