Cod sursa(job #1039245)

Utilizator multislashRobert Morosanu multislash Data 22 noiembrie 2013 18:26:44
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include<cstdio>
using namespace std;
int n,m,cod,i,x,y,t[100001],h[100001];
int father(int x)
{
    while(t[x]!=x) x=t[x];
    return x;
}
void unire(int x,int y)
{
    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()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++) t[i]=i;
    for(i=1;i<=m;i++) h[i]=1;
    for(i=1;i<=m;i++)
    {
        scanf("%d%d%d",&cod,&x,&y);
        if(cod==1) unire(father(x),father(y));
        else if(father(x)==father(y)) printf("DA\n");
            else printf("NU\n");
    }
    return 0;
}