70分求调

P3367 【模板】并查集

FRANXX_ @ 2023-01-17 16:35:32

#include<bits/stdc++.h>
using namespace std;
int f[100009],n,m,x[100009],y[100009],z[100009];
int find(int x)
{
    if(f[x]==x) return x;
    else f[x]=find(f[x]);
    return f[x];
}
void work1(int x,int y)
{
    f[find(y)]=find(x);
}
void work2(int x,int y)
{
    if(find(x)==find(y)) cout<<"Y\n";
    else cout<<"N\n";
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i)
    {
        scanf("%d",&z[i]);
        scanf("%d%d",&x[i],&y[i]);
        f[i]=i;
    }
    for(int i=1;i<=m;++i)
    {
        if(z[i]==1) work1(x[i],y[i]);
        else work2(x[i],y[i]);
    }
    return 0;
}

by yizhiming @ 2023-01-17 16:46:00

@FRANXX_ 两个问题

数组开小了,询问 2\times 10^5,而且也没必要存下来

f 数组要从 1n 赋值,特殊构造一下可以卡掉


by yizhiming @ 2023-01-17 16:59:57

@FRANXX_ 你这数组大小不是没改吗???


by yizhiming @ 2023-01-17 17:00:34

您为什么把为一个跟询问没有关系的数组开大了啊(


by FRANXX_ @ 2023-01-17 17:08:18

@yizhiming 已经更改了,谢谢大佬解惑,感谢


|