求大神帮查问题,MLE,多次无语

P3367 【模板】并查集

Wenchih @ 2018-10-30 21:39:39

#include<bits/stdc++.h>

using namespace std;

int N,M;

int ys[100001];
int zi,xi,yi;

void gb(int xx,int yy)// 合并
{
    if(ys[xx] == ys[yy])
    {
        return;
    }
    for (int i = ys[yy];;i = ys[i])
    {
        if(i == ys[i])
        {
            ys[i] = ys[xx];
            break;
        }
        ys[i] = ys[xx];
    }
}

int find(int xx)
{
    if(xx == ys[xx])
    {
        return xx;
    }
    else 
    {
        return ys[xx] = find(ys[xx]);
    }
}

int main()
{
    cin.sync_with_stdio(false);
    cout.tie(0);
    freopen("test.in","r",stdin);

    cin >> N >> M;
    for (int i = 1;i <= N; i ++)
    {
        ys[i] = i;
    }

    for (int i = 1;i <= M; i++)
    {
        cin >> zi;
        cin >> xi >> yi;
        if(zi == 1)
        {
            gb(xi,yi);
        }
        else if(zi == 2)
        {
            if(find(xi) == find(yi))
            {
                cout << 'Y' << '\n';
            }
            else cout << 'N' << '\n';

        }
    }

    return 0;
}

by Wenchih @ 2018-10-30 22:01:28

@雄英天下第一 已解决


上一页 |