Cod sursa(job #1039251)

Utilizator ionutpop118Pop Ioan Cristian ionutpop118 Data 22 noiembrie 2013 18:32:15
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <cstdio>
using namespace std;
int t[100001],h[100001];
int findset(int x)
{
    while (t[x]!=x)
        x=t[x];
    return x;
}
void unionset(int x,int y)
{
    //x si y sunt radacini
    if (h[x]==h[y])
    {
        h[x]++;
        t[y]=x;
    }
    else
        if (h[x]<h[y])
            t[x]=y;
        else
            t[y]=x;

}
int main()
{
    int n,op,i,x,y,tip;
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    scanf("%d%d",&n,&op);
    for (i=1;i<=n;i++)
        t[i]=i;
    for (i=1;i<=op;i++)
    {
        scanf("%d%d%d",&tip,&x,&y);
        if (tip==1)
            unionset(x,y);
        else
            if (findset(x)==findset(y))
                printf("DA\n");
            else
                printf("NU\n");
    }
    return 0;
}