Cod sursa(job #2028934)

Utilizator rangal3Tudor Anastasiei rangal3 Data 28 septembrie 2017 20:54:24
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <cstdio>
#define in "disjoint.in"
#define out "disjoint.out"
#define N 100003
using namespace std;

int n,m,p,x,y,t[N];

inline void unite(int X,int Y)
{
    t[X] = Y;
}

inline int root(int nod)
{
    int rad = nod;

    while(t[rad] > 0)
        rad = t[rad];

    return rad;

}

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

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

    int rx,ry;

    while(m--)
    {
        scanf("%d%d%d",&p,&x,&y);

        rx = root(x);
        ry = root(y);

        if(p == 1)
        {
            if(rx != ry)
                unite(rx,ry);
        }
        else
        {
            if(rx != ry) printf("NU\n");
                else printf("DA\n");
        }
    }

    fclose(stdin); fclose(stdout);
    return 0;
}