Cod sursa(job #2602919)

Utilizator avtobusAvtobus avtobus Data 18 aprilie 2020 09:27:51
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <stdio.h>
#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;

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

const int Nmax = 100555;
int a[Nmax];
int root(int x) {
  while(x != a[x]) {
    a[x] = a[a[x]];
    x = a[x];
  }
  return x;
}

int main(void) {
  // freopen("disjoint.in", "r", stdin);
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  int N, M;
  fin >> N >> M;
  rep(i, N+1) { a[i] = i; }
  int op, x, y;
  rep(i, M) {
    fin >> op >> x >> y;
    if (op == 1) {
      int rx = root(x);
      int ry = root(y);
      a[rx] = a[ry] = min(rx, ry);
    } else {
      fout << (root(x) == root(y) ? "DA\n" : "NU\n");
    }
  }

  return 0;
}