Cod sursa(job #973095)

Utilizator thewildnathNathan Wildenberg thewildnath Data 13 iulie 2013 14:00:35
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include<stdio.h>

int t[100002],h[100002];

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

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

int main()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    int n,m,i,c,x,y;
    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",&c,&x,&y);
        if(c==1)
            update(x,y);
        else
        {
            if(tata(x)==tata(y))
                printf("DA\n");
            else
                printf("NU\n");
        }
    }
    return 0;
}