Cod sursa(job #2813472)

Utilizator Luca_Miscocilucainfoarena Luca_Miscoci Data 6 decembrie 2021 18:54:43
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>
#define NMAX 100000
using namespace std;

int parent[NMAX + 1];

int root (int child){
  if (child == parent[child])
    return child;
  return parent[child] = root (parent[child]);
}

int main(){

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

  int n, t, type, op1, op2;

  fin >> n >> t;

  for (int i = 0; i < n; i++){
    parent[i] = i;
  }

  while (t--){
    fin >> type >> op1 >> op2;
    if (type == 1)
      parent[root(op2)] = root (op1);

    else  {
      if (root (op1) == root(op2))
        fout << "DA";
      else
        fout << "NU" ;
      fout << "\n";
    }
  }
  return 0;
}