Cod sursa(job #1267157)

Utilizator Alexa2001Alexa Tudose Alexa2001 Data 19 noiembrie 2014 16:51:50
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <cstdio>

using namespace std;

int x,y,n,m,r[100004],t[100004],i,op;

int find(int x)
{
    int z=x,y;

    while(z!=t[z])
        z=t[z];

    while(t[x]!=x)
    {
        y=x;
        x=t[x];
        t[y]=z;
    }

    return z;

}

void reuneste(int x,int y)
{
    if(r[x]<r[y])
    t[x]=y;
    else t[y]=x;

    if(r[x]==r[y]) r[x]++;

}

int main()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);


    scanf("%d%d",&n,&m);

    for(i=1;i<=n;++i)
    {
        r[i]=1;
        t[i]=i;

    }

    for(i=1;i<=m;++i)
    {
        scanf("%d%d%d",&op,&x,&y);

        if(op==1) reuneste(find(x),find(y));
        else
        {
           if(find(x)==find(y))
           printf("DA\n");
           else printf("NU\n");

        }


    }

    return 0;
}