Cod sursa(job #2916607)

Utilizator MR0L3eXMaracine Constantin Razvan MR0L3eX Data 30 iulie 2022 20:11:37
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
/**
 *    author:  R0L3eX
 *    created: 30.07.2022 19:32:03
 *    quote: Claustrophobic? Who would ever be afraid of Santa Clause?
**/

#include "bits/stdc++.h"

using namespace std;

#if defined(ONPC)
#include "bits/debug.h"
#endif

#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1e9 + 7;
const int mxN = 2e5;
const int INF = INT_MAX;
const char nl = '\n';

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

vector<int> sz;
vector<int> rep;

void init(int n) {
  sz = vector<int>(n);
  rep = vector<int>(n);

  for (int node = 0; node < n; ++node) {
    sz[node] = 1;
    rep[node] = node;
  }
}


int find(int a) {
  if (a == rep[a]) return a;
  return rep[a] = find(rep[a]);
}

bool same(int a, int b) {
  return find(a) == find(b);
}

bool join(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return false;
  if (sz[a] < sz[b]) swap(a, b);
  rep[b] = a;
  sz[a] += sz[b];
  return true;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);

  int n, Q;
  fin >> n >> Q;

  init(n);
  
  while (Q--) {
    int ty, a, b;
    fin >> ty >> a >> b;
    --a, --b;
    if (ty == 1) {
      join(a, b);
    } else {
      fout << (same(a, b) ? "DA" : "NU") << nl;
    }
  }
}