Cod sursa(job #532643)

Utilizator jupanubv92Popescu Marius jupanubv92 Data 12 februarie 2011 10:03:54
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <cstdio>

using namespace std;

int n, m, tata[100001];

int rad(int x)
{
    int L = x;
    while(tata[L] != L)
        L = tata[L];

    while(tata[x] != x)
    {
        int aux = tata[x];
        tata[x] = L;
        x = aux;
    }
    return L;
}

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

    scanf("%d%d",&n,&m);
    int tip,x,y;

    for(int i = 1; i <= n; ++i)
        tata[i] = i;
    for(int i = 1; i <= m; ++i)
    {
        scanf("%d %d %d", &tip, &x, &y);
        if( tip == 1)
            if( rad(x) != rad(y))
                tata[rad(x)] = tata[rad(y)];
        if(tip == 2)
            if(rad(x) == rad(y))
                printf("DA\n");
            else printf("NU\n");
    }
    return 0;
}