Cod sursa(job #1522988)

Utilizator fluture.Gafton Mihnea Alexandru fluture. Data 12 noiembrie 2015 12:10:22
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <cstdio>

#define in "disjoint.in"
#define out "disjoint.out"
#define NMAX 100007

using namespace std;
int n, m, cod, a, b, tata[NMAX];

int root(const int &key)
{
    int rkey = key, aux;
    while(tata[rkey] > 0)
        rkey = tata[rkey];
    aux = key;
    while(tata[aux] > 0)
    {
        int tmp = aux;
        aux = tata[aux];
        tata[tmp] = rkey;
    }
    return rkey;
}

void unit(const int &x, const int &y)
{
    int rx = root(x), ry = root(y);
    if(tata[rx] < tata[ry])
    {
        tata[rx] += tata[ry];
        tata[ry] = rx;
    }
    else
    {
        tata[ry] += tata[rx];
        tata[rx] = ry;
    }
}
int query(const int &x, const int &y)
{
    int rx = root(x), ry = root(y);
    return (rx == ry);
}

int main()
{
    freopen(in , "r", stdin);
    freopen(out, "w", stdout);
    scanf("%d %d", &n, &m);
    for(int i = 1; i<= n; ++i) tata[i] = -1;
    for(; m; --m)
    {
        scanf("%d %d %d", &cod, &a, &b);
        if(cod == 1) unit(a, b);
        if(cod == 2)
        {
            if(query(a, b)) printf("DA\n");
            else printf("NU\n");
        }
    }
    return 0;
}