Cod sursa(job #625029)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 23 octombrie 2011 16:55:46
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <cstdio>

#define NMax 100005

using namespace std;

int N, Father[NMax];

int Find (int X)
{
    int Root=X;
    for (; Father[Root]!=0; Root=Father[Root]);
    while (Father[X]!=0)
    {
        int Y=Father[X];
        Father[X]=Root;
        X=Y;
    }
    return Root;
}

void Unite (int X, int Y)
{
    Father[Find (Y)]=Find (X);
}

int main()
{
    freopen ("disjoint.in", "r", stdin);
    freopen ("disjoint.out", "w", stdout);
    int NQ;
    scanf ("%d %d", &N, &NQ);
    for (; NQ>0; --NQ)
    {
        int Type, X, Y;
        scanf ("%d %d %d", &Type, &X, &Y);
        if (Type==1)
        {
            Unite (X, Y);
        }
        else
        {
            if (Find (X)==Find (Y))
            {
                printf ("DA\n");
            }
            else
            {
                printf ("NU\n");
            }
        }
    }
    return 0;
}