Cod sursa(job #1929141)

Utilizator LazarAndreiLazar Andrei Teodor LazarAndrei Data 17 martie 2017 10:06:22
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 100005;

int n, m, T[NMAX];

int root (int node) {
    if (T[node] != node) {
        T[node] = root (T[node]);
    }

    return T[node];
}

void Check (int x1, int x2) {
    int root1 = root(x1);
    int root2 = root(x2);

    if(root1 == root2) {
        printf ("DA\n");
    }
    else {
        printf ("NU\n");
    }
}

void Join (int x1, int x2) {
    T[x1] = x2;
}

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

    scanf ("%d%d", &n,&m);
    for (int i = 1; i <= n; ++ i) {
        T[i] = i;
    }

    for (int i = 1; i <= m; ++ i) {
        int query, x1, x2;
        scanf ("%d%d%d", &query, &x1, &x2);

        if (query == 1) {
            Join (x1, x2);
        }
        else {
            Check (x1, x2);
        }

    }

    return 0;
}