Cod sursa(job #2075043)

Utilizator AndreiSorin26012001Cirpici Andrei Sorin AndreiSorin26012001 Data 25 noiembrie 2017 10:57:38
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>
#define DIM 100005

using namespace std;

ifstream in("disjoint.in");
ofstream out("disjoint.out");

int n, m, t, x, y, rx, ry;
int T[DIM];

int rad( int x )
{
    while( T[x] > 0 )
        x = T[x];
    return x;
}

int main()
{
    for(int i = 1; i <= DIM; i++)
        T[i] = 0;

    in>>n>>m;
    for(int i = 1; i <= m; i++){
        in>>t>>x>>y;

        if( t == 1 ){

            rx = rad( x );
            ry = rad( y );
            T[rx] = ry;
        } else {

            rx = rad( x );
            ry = rad( y );
            if( rx == ry )
                out<<"DA\n";
            else
                out<<"NU\n";
        }
    }

    return 0;
}