Cod sursa(job #3192713)

Utilizator VanillaSoltan Marian Vanilla Data 13 ianuarie 2024 10:45:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
using namespace std;
string __fname = "disjoint"; ifstream in (__fname + ".in"); ofstream out (__fname + ".out"); 
#define cin in 
#define cout out
const int maxn = 1e5 + 1;
int p[maxn];

// int getdad (int x) {
//   if (x == p[x]) return x;
//   int rs = getdad(p[x]);
//   p[x] = rs;
//   return p[x];
// }

int getdad (int x) {
  return p[x] = (p[x] == x ? x: getdad(p[x]));
}

void combine (int x, int y) {
  x = getdad(x);
  y = getdad(y);
  p[x] = y;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++){
    p[i] = i;
  }
  while (m--) {
    int t,x,y;
    cin >> t >> x >> y;
    if (t == 1) {
      combine(x, y);
    }
    else {
      if (getdad(x) == getdad(y)) {
        cout << "DA\n";
      }
      else {
        cout << "NU\n";
      }
    }
  }

  return 0;
}