Cod sursa(job #1411620)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 31 martie 2015 20:44:53
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <cstdio>

#define Nmax 100010

using namespace std;

int i , n , m , tip , x , y;
int T[Nmax] , rang[Nmax];

int multime(int nod)
{
    if (T[nod] != nod) T[nod] = multime(T[nod]);

    return T[nod];
}

void reuneste(int x, int y)
{
    x = multime(x); y = multime(y);

    if (x == y) return;

    if (rang[x] < rang[y]) T[x] = y;
    else T[y] = x;

    if (rang[x] == rang[y]) rang[x]++;
}


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

    scanf("%d %d", &n, &m);

    for (i = 1; i <= n; T[i] = (i++));

    for ( ; m ; --m)
    {
        scanf("%d %d %d", &tip, &x, &y);

        if (tip & 1) reuneste(x , y);
        else (multime(x) == multime(y)) ? printf("DA\n") : printf("NU\n");
    }

    return 0;
}