Cod sursa(job #1414387)

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

const int Nmax = 100000 + 10;

int tata[Nmax];

int getRoot(int a)
{
    if (a == tata[a])
        return a;
    else
        return getRoot(tata[a]);
}

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[b] = a;
        else
             if (getRoot(a) == getRoot(b))
                puts("DA");
            else
                puts("NU");
    }

}