Cod sursa(job #1098610)

Utilizator gabrielvGabriel Vanca gabrielv Data 4 februarie 2014 22:20:18
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <cstdio>

using namespace std;

#define NMAX 100015

int N;

int Father[NMAX];
int Depth[NMAX];

int Die_Wurzel(int node)
{
    if(node == Father[node])
        return node;
    return Die_Wurzel(Father[node]);
}

void Vereinen(int x, int y)
{
    int fatherX = Die_Wurzel(x);
    int fatherY = Die_Wurzel(y);

    if(Depth[fatherX] >= Depth[fatherY])
        Father[fatherY] = fatherX;
    else
        Father[fatherX] = fatherY;

    if(Depth[fatherX] == Depth[fatherY])
        Depth[fatherX]++;
}

inline bool SindBruder(int x, int y)
{
    return Die_Wurzel(x) == Die_Wurzel(y);
}

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

    int M,op,x,y;
    scanf("%d%d",&N,&M);

    for(int i=1;i<=N;i++)
        Father[i] = i;

    while(M--)
    {
        scanf("%d%d%d",&op,&x,&y);
        if(op==1)
            Vereinen(x,y);
        else
            if(SindBruder(x,y))
                printf("DA\n");
            else
                printf("NU\n");
    }

    return 0;
}