Cod sursa(job #1182379)

Utilizator dropsdrop source drops Data 6 mai 2014 11:27:37
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

int rad[100010], n, m, op, x, y;

int tata(int x) {
    if (x == rad[x]) return x; else {
        rad[x] = tata(rad[x]);
        return rad[x];
    }
}

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

    return 0;
}