Cod sursa(job #2700998)

Utilizator Victor2006Nicola Victor-Teodor Victor2006 Data 29 ianuarie 2021 15:39:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#define N 100000

using namespace std;

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

int boss[N + 1];
int n;

int find( int n ) {
    if ( boss[n] == n )
        return n;
    return boss[n] = find( boss[n] );
}

void reuniune( int a, int b ) {
    int sef1, sef2;

    sef1 = find( a );
    sef2 = find( b );

    boss[sef2] = sef1;
}

int main() {
    int q, op, x, y;

    fin >> n >> q;
    for ( int i = 1; i <= n; i ++ )
        boss[i] = i;

    for ( int i = 1; i <= q; i ++ ) {
        fin >> op >> x >> y;
        if ( op == 1 )
            reuniune( x, y );
        else {
            if ( find( x ) == find( y ) )
                fout << "DA\n";
            else
                fout << "NU\n";
        }
    }
    return 0;
}