Cod sursa(job #2922553)

Utilizator raresgherasaRares Gherasa raresgherasa Data 8 septembrie 2022 21:11:42
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NM = 1e5 + 5;

int t[NM], c[NM], n, m;

int root (int x){
  if (x == t[x]){
    return x;
  }
  return t[x] = root(t[x]);
}

void unite (int x, int y){
  x = root(x);
  y = root(y);
  if (c[x] > c[y]){
    swap(x, y);
  }
  t[y] = x;
  c[x] += c[y];
}

int main(){
  fin >> n >> m;
  for (int i = 1; i <= n; i++){
    t[i] = i;
    c[i] = 1;
  }
  while (m--){
    int o, x, y; fin >> o >> x >> y;
    if (o == 1){
      if (root(x) != root(y)){
        unite(x, y);
      }
    }
    else{
      fout << (root(x) == root(y) ? "DA" : "NU") << '\n';
    }
  }
}