Cod sursa(job #2700994)

Utilizator Victor2006Nicola Victor-Teodor Victor2006 Data 29 ianuarie 2021 15:35:37
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <cstdio>
#define N 100000

using namespace std;

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() {
    freopen( "disjoint.in", "r", stdin );
    freopen( "disjoint.out", "w", stdout );
    int q, op, x, y;

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

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