Cod sursa(job #2521573)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 11 ianuarie 2020 10:32:57
Problema Paduri de multimi disjuncte Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <cstdio>
#define NMAX 100000

using namespace std;

int n, m;
int repr[NMAX + 5];

int get_repr(int x) {
  while(x != repr[x])
    x = repr[x];
  return x;
}

void set_repr(int x, int rx) {
  while(x != repr[x])
    x = repr[x];
  repr[x] = rx;
}

int main() {
  freopen("disjoint.in", "r", stdin);
  freopen("disjoint.out", "w", stdout);
  int cod, x, y;

  scanf("%d %d", &n, &m);
  for(int i = 1; i <= n; i++)
    repr[i] = i;

  for(int i = 1; i <= m; i++) {
    scanf("%d %d %d", &cod, &x, &y);
    if(cod == 1) {
      int rx = get_repr(x), ry = get_repr(y);

      if(rx != ry)
        set_repr(x, ry);
    }
    else {
      if(get_repr(x) == get_repr(y))
        printf("DA\n");
      else
        printf("NU\n");
    }
  }

  return 0;
}