Cod sursa(job #2422977)

Utilizator catu_bogdan_99Catu Bogdan catu_bogdan_99 Data 20 mai 2019 15:18:53
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <cstdio>
using namespace std;

#define UNION 1
#define CHECK 2

const int NMAX = 100001;
int DS[ NMAX ];

int ROOT( int x ) {

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

    return DS[ x ] = ROOT( DS[ x ] );

}

int main () {

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

    int n, m, x, y, rx, ry, op;

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

    for ( int i = 1; i <= n; ++i ) {
        DS[ i ] = i;
    }

    while ( m-- ) {
        scanf( "%d%d%d", &op, &x, &y );
        switch ( op ) {
        case UNION :

                rx = ROOT( x );
                ry = ROOT( y );
                DS[ rx ] = ry;

            break;
        case CHECK :

                rx = ROOT( x );
                ry = ROOT( y );
                printf( "%s\n", ( rx == ry ) ? "DA" : "NU" );

            break;
        default : break;
        }
    }

    return 0;

}