답안 #486198

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486198 2021-11-10T21:02:24 Z evenvalue Experimental Charges (NOI19_charges) C++17
100 / 100
36 ms 1928 KB
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

class dsu {
  const int n;
  vector<int> e;

  int private_find(const int x) {
    return (e[x] < 0 ? x : (e[x] = private_find(e[x])));
  }

public:
  explicit dsu(const int n) : n(n), e(n, -1) {}

  int find(const int x) {
    assert(0 <= x and x < n);
    return private_find(x);
  }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return false;
    if (e[x] > e[y]) swap(x, y);
    e[x] += e[y];
    e[y] = x;
    return true;
  }

  int size(const int x) {
    return -e[find(x)];
  }

  bool same(const int x, const int y) {
    return (find(x) == find(y));
  }

  const int &operator[](const int i) {
    return e[i];
  }
};

class solution {
  using int64 = int64_t;
  using ld = long double;

  void main() {
    const int n = readInt();
    int q = readInt();
    dsu d(2 * n);
    vector<int> opp(2 * n);

    for (int i = 0; i < n; i++) {
      opp[i] = i + n;
      opp[i + n] = i;
    }

    auto unite_same = [&d, &opp](const int x, const int y) {
      const int ox = opp[x];
      const int oy = opp[y];
      d.unite(x, y);
      d.unite(ox, oy);
      opp[d.find(x)] = d.find(ox);
      opp[d.find(ox)] = d.find(x);
    };

    auto unite_diff = [&unite_same, &opp](const int x, const int y) {
      unite_same(x, opp[y]);
    };

    while (q--) {
      const char type = readChar();
      const int x = readInt() - 1, y = readInt() - 1;
      if (type == 'Q') {
        if (const bool same = d.same(x, y), diff = d.same(x, opp[y]); same or diff) {
          print(same ? 'R' : 'A'), println();
        } else {
          print('?'), println();
        }
      } else if (type == 'R') {
        unite_same(x, y);
      } else {
        unite_diff(x, y);
      }
    }
  }

public:
  explicit solution() {
    main();
  }

private:
  //inputting stuff (cin)
  [[maybe_unused]] static int readInt() {
    int x;
    cin >> x;
    return x;
  }
  [[maybe_unused]] static int64 readInt64() {
    int64 x;
    cin >> x;
    return x;
  }
  [[maybe_unused]] static char readChar() {
    char c;
    cin >> c;
    return c;
  }
  [[maybe_unused]] static string readString() {
    string s;
    cin >> s;
    return s;
  }
  [[maybe_unused]] static double readDouble() {
    return stod(readString());
  }
  [[maybe_unused]] static ld readLongDouble() {
    return stold(readString());
  }
  template<typename T1, typename T2>
  [[maybe_unused]] static pair<T1, T2> readPair() {
    pair<T1, T2> p;
    cin >> p.first >> p.second;
    return p;
  }
  template<typename T>
  [[maybe_unused]] static vector<T> readVec(const int sz) {
    vector<T> v(sz);
    for (T &x : v) {
      cin >> x;
    }
    return v;
  }
  template<typename T>
  [[maybe_unused]] static vector<vector<T>> readVecVec(const int n, const int m) {
    vector<vector<T>> a(n);
    for (vector<T> &v : a) {
      v = readVec<T>(m);
    }
    return a;
  }

  //outputting stuff (cout)
  [[maybe_unused]] static void println() { cout << '\n'; }
  [[maybe_unused]] static void printsp() { cout << ' '; }
  [[maybe_unused]] static void print(const int x) { cout << x; }
  [[maybe_unused]] static void print(const unsigned int x) { print(int(x)); }
  [[maybe_unused]] static void print(const int64 x) { cout << x; }
  [[maybe_unused]] static void print(const double d, const int precision = 10) { cout << fixed << setprecision(precision) << d; }
  [[maybe_unused]] static void print(const ld d, const int precision = 10) { cout << fixed << setprecision(precision) << d; }
  [[maybe_unused]] static void print(const char c) { cout << c; }
  [[maybe_unused]] static void print(const string &s) { cout << s; }
  template<typename T1, typename T2>
  [[maybe_unused]] static void print(const pair<T1, T2> &p) { print(p.first), printsp(), print(p.second); }
  template<typename T>
  [[maybe_unused]] static void print(const vector<T> &v) {
    for (const T &x : v) {
      print(x), printsp();
    }
    println();
  }
  template<typename T>
  [[maybe_unused]] static void print(const vector<vector<T>> &a) {
    for (const vector<T> &v : a) {
      print(v);
    }
  }

  //debugging stuff (cerr)
  [[maybe_unused]] static void _println() { cerr << '\n'; }
  [[maybe_unused]] static void _printsp() { cerr << ' '; }
  [[maybe_unused]] static void _print(const int x) { cerr << x; }
  [[maybe_unused]] static void _print(const int64 x) { cerr << x; }
  [[maybe_unused]] static void _print(const unsigned int x) { cerr << x; }
  [[maybe_unused]] static void _print(const double d, const int precision = 10) { cerr << fixed << setprecision(precision) << d; }
  [[maybe_unused]] static void _print(const ld d, const int precision = 10) { cerr << fixed << setprecision(precision) << d; }
  [[maybe_unused]] static void _print(const char c) { cerr << '\'' << c << '\''; }
  [[maybe_unused]] static void _print(const string &s) { cerr << '\"' << s << '\"'; }
  template<typename T1, typename T2>
  [[maybe_unused]] static void _print(const pair<T1, T2> &p) {
    cerr << '[';
    _print(p.first);
    cerr << ", ";
    _print(p.second);
    cerr << ']';
  }
  template<typename T>
  [[maybe_unused]] static void _print(const vector<T> &v) {
    const int n = v.size();
    cerr << '{';
    for (int i = 0; i < n; i++) {
      _print(v[i]);
      cerr << (i != n - 1 ? ", " : "");
    }
    cerr << '}';
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  const auto begin = std::chrono::high_resolution_clock::now();

  //  freopen(".in", "r", stdin);
  //  freopen(".out", "w", stdout);

  int t = 1;
  //cin >> t;
  for (int tc = 1; tc <= t; tc++) {
    solution();
  }

  const auto end = std::chrono::high_resolution_clock::now();
  const auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
  cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 1868 KB Output is correct
2 Correct 21 ms 1848 KB Output is correct
3 Correct 20 ms 1840 KB Output is correct
4 Correct 19 ms 1864 KB Output is correct
5 Correct 28 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 1912 KB Output is correct
2 Correct 20 ms 1868 KB Output is correct
3 Correct 22 ms 1876 KB Output is correct
4 Correct 22 ms 1868 KB Output is correct
5 Correct 23 ms 1928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 1840 KB Output is correct
2 Correct 22 ms 1800 KB Output is correct
3 Correct 22 ms 1804 KB Output is correct
4 Correct 26 ms 1852 KB Output is correct
5 Correct 23 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 18 ms 1868 KB Output is correct
7 Correct 21 ms 1848 KB Output is correct
8 Correct 20 ms 1840 KB Output is correct
9 Correct 19 ms 1864 KB Output is correct
10 Correct 28 ms 1868 KB Output is correct
11 Correct 21 ms 1912 KB Output is correct
12 Correct 20 ms 1868 KB Output is correct
13 Correct 22 ms 1876 KB Output is correct
14 Correct 22 ms 1868 KB Output is correct
15 Correct 23 ms 1928 KB Output is correct
16 Correct 21 ms 1840 KB Output is correct
17 Correct 22 ms 1800 KB Output is correct
18 Correct 22 ms 1804 KB Output is correct
19 Correct 26 ms 1852 KB Output is correct
20 Correct 23 ms 1868 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 20 ms 1868 KB Output is correct
27 Correct 24 ms 1824 KB Output is correct
28 Correct 36 ms 1740 KB Output is correct
29 Correct 21 ms 1840 KB Output is correct
30 Correct 25 ms 1860 KB Output is correct