Cod sursa(job #1846081)

Utilizator Walrus21andrei Walrus21 Data 12 ianuarie 2017 09:38:37
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <stdio.h>
#include <time.h>
#include <algorithm>
#define N 100001

using namespace std;

int i, n, m, c, x, y, T[N], H[N];

int main()
{
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(i = 0; i < m; i++)
    {
        scanf("%d%d%d", &c, &x, &y);
        int tx = x, ty = y, t;
        while(T[tx])
            tx = T[tx];
        while(T[x])
        {
            t = T[x];
            T[x] = tx;
            x = t;
        }
        while(T[ty])
            ty = T[ty];
        while(T[y])
        {
            t = T[y];
            T[y] = ty;
            y = t;
        }
        if(c == 2)
        {
            if(x == y) printf("DA\n");
            else printf("NU\n");
        }
        else
        {
            if(H[x] == H[y])
            {
                T[y] == x;
                H[x] == H[y] + 1;
            }
            if(H[x] < H[y])
                T[x] = y;
            else
                T[y] = x;
        }
    }
    return 0;
}