Cod sursa(job #1207228)

Utilizator RathebaSerbanescu Andrei Victor Ratheba Data 12 iulie 2014 16:20:42
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>

using namespace std;
#define MAX 100005

int h, tata[MAX];
int goup(int nod);
int main()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    int n, m, cod, a, b, aa, bb, ha, hb, i;
    scanf("%d%d",&n,&m);
    for(i=1; i<=n; i++)
        tata[i] = i;
    for(i=1; i<=m; i++)
    {
        scanf("%d%d%d",&cod,&a,&b);
        if(cod==1)
        {
            h=0;
            bb=goup(b); hb=h;
            h=0;
            aa=goup(a); ha=h;
            if(ha >= hb) tata[bb] = aa;
            else tata[aa] = bb;
        }
        if(cod==2)
        {
            aa=goup(a);
            bb=goup(b);
            if(aa == bb)
                printf("DA\n");
            else printf("NU\n");
        }
    }
    return 0;
}
int goup(int nod)
{
    if(tata[nod] == nod){
        return nod;
    }
    h++;
    int t = goup(tata[nod]);
    return t;
}