Cod sursa(job #624294)

Utilizator TeodoraTanaseTeodora Tanase TeodoraTanase Data 22 octombrie 2011 10:30:06
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <cstdio>
#define N 100003

using namespace std;

int n, m, a, x, y, tata[N];

int reunim(int nod)
{
    if (tata[nod] == nod)
        return nod;
    return tata[nod] = reunim (tata[nod]);
}

bool verif(int x, int y)
{
    if (tata[y] == y && x != y)
        return false;
    if (tata[y] == tata[x])
        return true;
    return verif (x, tata[y]);
}

void pune()
{
    for (int i = 1; i <= n; ++ i)
        tata[i] = i;
}

int main()
{
    freopen ("disjoint.in", "r", stdin);
    freopen ("disjoint.out", "w", stdout);
    scanf ("%d %d ", &n, &m);
    pune();
    for (int i = 1; i <= m; ++ i)
    {
        scanf ("%d %d %d ", &a, &x, &y);
        if (a == 1)
            tata[y] = reunim (x);
        else
        {
            if (verif(x, y))
                printf ("DA\n");
            else
                printf ("NU\n");
        }
    }
    return 0;
}