RE求调

P3367 【模板】并查集

__Shine__ @ 2023-06-23 16:03:45

#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,z;
int f[10010],cnt[10010];
int Find(int x)
{
    if(x==f[x]) return x;
    else f[x]=Find(f[x]);
}
int Merge(int x,int y)
{
    int fx=Find(x),fy=Find(y);
    if(cnt[fx]<cnt[fy])
    {
        cnt[fy]+=cnt[fx];
        f[fx]=fy;
    }else{
        cnt[fx]+=cnt[fy];
        f[fy]=fx;
    }
}
void init(int n)
{
    for(int i=1;i<=n;i++)
        f[i]=i,cnt[i]=1;
}
int main()
{
    cin>>n>>m;
    init(n);
    while(m--)
    {
        cin>>z>>x>>y;
        if(z==1)
            Merge(x,y);
        else
            if(Find(x)==Find(y))
                cout<<"Y\n";
            else
                cout<<"N\n";
    }
    return 0;
}

by ForgotDream_CHN @ 2023-06-23 16:45:40

@Shine Find 少写了个 return


by ForgotDream_CHN @ 2023-06-23 16:50:25

而且 Merge 应该返回 void


by __Shine__ @ 2023-06-23 17:39:26

@ForgotDream_CHN thx 已关


|