Cod sursa(job #2491796)

Utilizator Cosmin2004_InfoMoldoveanu Cosmin Cosmin2004_Info Data 13 noiembrie 2019 09:51:21
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <cstdio>

using namespace std;
int t[100001], h[100001];

int FindFirstAncestor(int x) {
    while(x != t[x])
        x = t[x];
    return x;
}

void Operation2(int x, int y) {
    bool c = FindFirstAncestor(x) == FindFirstAncestor(y);
    if(c)
        printf("DA\n");
    else
        printf("NU\n");
}

void Operation1(int x, int y) {
    if(h[x] == h[y]) {
        t[y] = x;
        h[x]++;
    }
    else if(h[x] > h[y]) {
        t[y] = x;
    }
    else
        t[x] = y;
}

int main() {
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w", stdout);
    int i, n, m, op_type, x, y, tx, ty;
    scanf("%d%d", &n, &m);
    for(i = 1; i <= n; i++)
    {
        t[i] = i;
        h[i] = 1;
    }
    for(i = 1; i <= m; i++)
    {
        scanf("%d%d%d", &op_type, &x, &y);
        if(op_type == 2)
            Operation2(x, y);
        else
            Operation1(x, y);
    }
    return 0;
}