Cod sursa(job #973097)

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

int t[100002],h[100002];

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

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(t[x],t[y]);
        else
        {
            if(tata(t[x])==tata(t[y]))
                printf("DA\n");
            else
                printf("NU\n");
        }
    }
    return 0;
}