Cod sursa(job #2251716)

Utilizator catu_bogdan_99Catu Bogdan catu_bogdan_99 Data 1 octombrie 2018 21:20:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <cstdio>
using namespace std;

#define NMAX 100001
int P[ NMAX ];

int Root ( int x ) {

    if ( P[ x ] == P[ P[ x ] ] )
        return P[ x ];

    return P[ x ] = Root( P[ x ] );

}

int main () {


    freopen( "disjoint.in", "r", stdin );
    freopen( "disjoint.out", "w", stdout );
    int n, m, i, j, x, y, k,rx, ry;

    scanf( "%d%d",&n,&m );
    for ( i = 1; i <= n; ++i ) P[ i ] = i;

    while ( m-- ) {
        scanf( "%d%d%d",&k,&x,&y );
        rx = Root( x );
        ry = Root( y );
        if ( k == 1 ) P[ rx ] = ry;
        else {
                if ( rx == ry )printf( "DA\n" );
                else printf( "NU\n" );
        }
    }


    return 0;
}