Cod sursa(job #2284244)

Utilizator CristyXtremeSimion Cristian CristyXtreme Data 17 noiembrie 2018 01:36:59
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <stdio.h>

using namespace std;

int find_set(int *tata, int ind_element) {
    if (tata[ind_element] != ind_element)
        tata[ind_element] = find_set(tata, tata[ind_element]);
    return tata[ind_element];
}

void uniune(int *tata, int *rang, int ind_x, int ind_y) {
    int parinte_x = find_set(tata, ind_x), parinte_y = find_set(tata, ind_y);
    if (rang[parinte_x] < rang[parinte_y])
        tata[parinte_x] = parinte_y;
    else
        tata[parinte_y] = parinte_x;
    if (rang[parinte_x] == rang[parinte_y])
        rang[parinte_x]++;
}

int main()
{
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    int n, m, tata[100000], rang[100000] = {0}, x, y, cod;
    scanf("%i %i", &n, &m);
    for (int i = 0; i < n; i++)
        tata[i] = i;
    for (int i = 0; i < m; i++) {
        scanf("%i %i %i", &cod, &x, &y);
        if (cod == 1)
            uniune(tata, rang, x - 1, y - 1);
        else
            if (find_set(tata, x - 1) == find_set(tata, y - 1))
                printf("DA\n");
            else
                printf("NU\n");
    }
    return 0;
}