제출 #1167808

#제출 시각아이디문제언어결과실행 시간메모리
1167808avighnaExperimental Charges (NOI19_charges)C++20
100 / 100
14 ms1420 KiB
#include <bits/stdc++.h>

class UFDS {
public:
  std::vector<int> par;

  UFDS(int n) : par(n + 1) { std::iota(par.begin(), par.end(), 0); }

  int root(int x) { return x == par[x] ? x : par[x] = root(par[x]); }

  void merge(int x, int y) {
    x = root(x), y = root(y);
    if (x == y) {
      return;
    }
    par[y] = x;
  }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, q;
  std::cin >> n >> q;
  UFDS dsu(2 * n);
  while (q--) {
    char c;
    std::cin >> c;
    int a, b;
    std::cin >> a >> b;
    if (c == 'A') {
      dsu.merge(a, b + n);
      dsu.merge(a + n, b);
    } else if (c == 'R') {
      dsu.merge(a, b);
      dsu.merge(a + n, b + n);

    } else {
      if (dsu.root(a) == dsu.root(b)) {
        std::cout << "R\n";
      } else if (dsu.root(a) == dsu.root(b + n)) {
        std::cout << "A\n";
      } else {
        std::cout << "?\n";
      }
    }
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...