Cod sursa(job #1906042)

Utilizator cyg_vladioanBirsan Vlad cyg_vladioan Data 6 martie 2017 12:09:04
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <cstdio>
using namespace std;
int t[1005] , h[1005];
void unionMD(int tx , int ty)
{
    if(h[tx] == h[ty])
    {
        h[tx] ++;
        t[ty] = tx;
    }
    else if(h[tx] > h[ty])
        t[ty] = tx;
    else
        t[tx] = ty;
}
int findMD(int x)
{
    while(x != t[x])
        x = t[x];
    return x;
}
int main()
{
    freopen("disjoint.in" , "r" , stdin);
    freopen("disjoint.out" , "w" , stdout);
    int x , y , n , nr , i , tx , ty , m , cnt;
    scanf("%d%d" , &n , &m);
    for(i = 1 ; i <= n ; i ++)
        t[i] = i , h[i] = 1;
    while(scanf("%d%d%d" , &cnt , &x , &y) != EOF)
    {
        tx = findMD(x);
        ty = findMD(y);
        if(cnt == 1)
            unionMD(tx , ty);
        else
        {
            if(tx == ty)
                printf("DA\n");
            else
                printf("NU\n");
        }
    }
    return 0;
}