Cod sursa(job #2611933)

Utilizator matthriscuMatt . matthriscu Data 7 mai 2020 20:44:22
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <cstdio>

int t[505], rang[505];

int root(int x) {
    if(t[x] == 0)
        return x;
    else {
        int temp = root(t[x]);
        t[x] = temp;
        return temp;
    }
}

void join(int x, int y) {
    int rootX = root(x), rootY = root(y);
    if(rang[x] > rang[y])
        t[rootY] = rootX;
    else {
        t[rootX] = rootY;
        if(rang[x] == rang[y])
            ++rang[y];
    }    
}

int main() {
    int n, m, i, c, x, y;
    scanf("%d%d", &n, &m);
    for(i = 1; i <= m; ++i) {
        scanf("%d%d%d", &c, &x, &y);
        if(c == 1)
            join(x, y);
        else
            if(root(x) == root(y))
                printf("DA\n");
            else
                printf("NU\n");            
    }
}