Cod sursa(job #226501)

Utilizator bogdan2412Bogdan-Cristian Tataroiu bogdan2412 Data 1 decembrie 2008 20:53:04
Problema Paduri de multimi disjuncte Scor Ascuns
Compilator cpp Status done
Runda Marime 0.99 kb
#include <cstdio>
#include <cstdlib>
#include <cassert>

using namespace std;

#define MAXN 100005

int N, M;
int p[MAXN];

inline int getSet(int x)
{
    if (x == p[x])
        return x;
    return p[x] = getSet(p[x]);
}

int main()
{
    freopen("disjoint.in", "rt", stdin);
    freopen("disjoint.out", "wt", stdout);
    assert(scanf("%d %d", &N, &M) == 2);
    assert(1 <= N && N <= 100000);
    assert(1 <= M && M <= 100000);
    for (int i = 1; i <= N; i++)
        p[i] = i;

    for (int i = 0; i < M; i++)
    {
        int type, x, y;
        assert(scanf("%d %d %d", &type, &x, &y) == 3);
        assert(type == 1 || type == 2);
        assert(1 <= x && x <= N);
        assert(1 <= y && y <= N);

        x = getSet(x);
        y = getSet(y);
        if (type == 1)
        {
            assert(x != y);
            if (rand() & 1)
                p[x] = y;
            else
                p[y] = x;
        }
        else
            printf("%s\n", (x == y) ? "DA" : "NU");
    }

    return 0;
}