Cod sursa(job #2848210)

Utilizator vladburacBurac Vlad vladburac Data 12 februarie 2022 11:13:26
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
using namespace std;
const int MAXN = 1e5;

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

int father[MAXN+1];

int findd( int x );
void unite( int x, int y ) {
  int xx, yy;
  xx = findd(x);
  yy = findd(y);
  if( xx != yy )
    father[x] = y;
}

int findd( int x ) {
  if( x == father[x] )
    return x;
  return father[x] = findd( father[x] );
}

int main() {
  int n, m, i, op, x, y;
  fin >> n >> m;
  for( i = 1; i <= n; i++ )
    father[i] = i;
  for( i = 0; i < m; i++ ) {
    fin >> op >> x >> y;
    if( op == 1 )
      unite( x, y );
    else {
      if( findd(x) == findd(y) )
        fout << "DA";
      else
        fout << "NU";
      fout << "\n";
    }
  }
  return 0;
}