Cod sursa(job #1414389)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 2 aprilie 2015 16:16:24
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>

const int Nmax = 100000 + 10;

int tata[Nmax];

int getRoot(int a)
{
    int R = a;

    while (R != tata[R])
        R = tata[R];

    while (a != tata[a])
    {
        int y = tata[a];
        tata[a] = R;
        a = y;
    }

    return R;
}

int main() {

    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);

    int N, M;
    scanf("%d %d", &N, &M);

    for (int i = 1; i <= N; ++i)
        tata[i] = i;

    while (M--)
    {
        int tip, a, b;

        scanf("%d %d %d ", &tip, &a, &b);

        if (tip == 1)
            tata[ getRoot(a) ] = getRoot(b);
        else
             if (getRoot(a) == getRoot(b))
                puts("DA");
            else
                puts("NU");
    }

}