답안 #765464

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765464 2023-06-24T14:10:33 Z Hegdahl Joker (BOI20_joker) C++17
49 / 100
1352 ms 10944 KB
#include <bits/stdc++.h>

using namespace std;

struct UnionFind {
  vector<int> parent_or_neg_size, edge_parity;
  struct UndoInfo {
    int i, i_pz, i_ep;
    int j, j_pz, j_ep;
  };
  vector<UndoInfo> undo_info;

  UnionFind(int n) : parent_or_neg_size(n, -1),
                     edge_parity(n, 0) {}

  pair<int, int> find_root_and_parity(int i) {
    int parity = 0;
    while (parent_or_neg_size[i] >= 0) {
      parity ^= edge_parity[i];
      i = parent_or_neg_size[i];
    }
    return {i, parity};
  }

  enum UniteResult { NO_CYCLE, EVEN_CYCLE, ODD_CYCLE };
  UniteResult unite(int i, int j, bool new_edge_parity = true, bool save_undo_info = false) {
    auto [root_i, parity_i] = find_root_and_parity(i);
    auto [root_j, parity_j] = find_root_and_parity(j);
    if (root_i == root_j) {
      if (save_undo_info) {
        undo_info.push_back({0, 0, 0, 0, 0, 0});
      }

      if (parity_i ^ parity_j ^ new_edge_parity) {
        return ODD_CYCLE;
      } else {
        return EVEN_CYCLE;
      }
    }
    if (parent_or_neg_size[root_i] > parent_or_neg_size[root_j]) {
      swap(root_i, root_j);
      swap(parity_i, parity_j);
    }

    if (save_undo_info) {
      undo_info.push_back({
          .i = root_i,
          .i_pz = parent_or_neg_size[root_i],
          .i_ep = edge_parity[root_i],
          .j = root_j,
          .j_pz = parent_or_neg_size[root_j],
          .j_ep = edge_parity[root_j],
      });
    }

    parent_or_neg_size[root_i] += parent_or_neg_size[root_j];
    parent_or_neg_size[root_j] = root_i;
    edge_parity[root_j] = parity_i ^ parity_j ^ new_edge_parity;

    return NO_CYCLE;
  }

  void undo() {
    assert(!undo_info.empty());
    auto [i, i_pz, i_ep, j, j_pz, j_ep] = undo_info.back();
    undo_info.pop_back();
    if (i == 0 && j == 0) {
      return;
    }

    parent_or_neg_size[i] = i_pz;
    edge_parity[i] = i_ep;
    parent_or_neg_size[j] = j_pz;
    edge_parity[j] = j_ep;
  }

  void undo_all() {
    while (!undo_info.empty()) {
      undo();
    }
  }
};

int main() {
  int n, m, q;
  cin >> n >> m >> q;

  vector<array<int, 2>> edges(m);
  for (auto &[u, v] : edges) {
    cin >> u >> v;
    --u;
    --v;
  }

  static constexpr auto BLOCK_SIZE = 400;

  std::vector<array<int, 2>> queries(q);
  std::vector<std::basic_string<int>> blocks((q + BLOCK_SIZE - 1) / BLOCK_SIZE);
  std::vector<bool> answers(q);
  for (int qq = 0; qq != q; ++qq) {
    auto &[l, r] = queries[qq];
    cin >> l >> r;
    --l;
    blocks[l / BLOCK_SIZE].push_back(qq);
  }

  UnionFind uf(n);
  for (int lid = 0; lid != int(blocks.size()); ++lid) {
    uf.parent_or_neg_size.assign(n, -1);

    std::sort(blocks[lid].begin(), blocks[lid].end(), [&](int i, int j) {
      auto &[l0, r0] = queries[i];
      auto &[l1, r1] = queries[j];
      return r0 > r1;
    });

    int min_l = lid * BLOCK_SIZE;

    bool base_has_odd_cycle = false;

    for (int j = 0; j != min_l; ++j) {
      auto [u, v] = edges[j];
      if (uf.unite(u, v) == UnionFind::ODD_CYCLE) {
        base_has_odd_cycle = true;
        break;
      }
    }

    int last_added = m;
    for (int qq : blocks[lid]) {
      auto &[l, r] = queries[qq];
      while (!base_has_odd_cycle && r < last_added) {
        auto [u, v] = edges[--last_added];
        if (uf.unite(u, v) == UnionFind::ODD_CYCLE) {
          base_has_odd_cycle = true;
        }
      }

      bool has_odd_cycle = base_has_odd_cycle;
      for (int j = min_l; !has_odd_cycle && j != l; ++j) {
        auto [u, v] = edges[j];
        if (uf.unite(u, v, true, true) == UnionFind::ODD_CYCLE) {
          has_odd_cycle = true;
        }
      }

      answers[qq] = has_odd_cycle;
      uf.undo_all();
    }
  }

  for (bool answer : answers) {
    cout << (answer ? "YES" : "NO") << '\n';
  }
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 7 ms 340 KB Output is correct
30 Correct 4 ms 304 KB Output is correct
31 Correct 4 ms 308 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 4 ms 340 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 7 ms 340 KB Output is correct
38 Correct 8 ms 420 KB Output is correct
39 Correct 6 ms 340 KB Output is correct
40 Correct 4 ms 412 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 4 ms 340 KB Output is correct
43 Correct 3 ms 496 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 316 KB Output is correct
47 Correct 4 ms 340 KB Output is correct
48 Correct 4 ms 340 KB Output is correct
49 Correct 5 ms 308 KB Output is correct
50 Correct 5 ms 340 KB Output is correct
51 Correct 4 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 5 ms 420 KB Output is correct
54 Correct 5 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 651 ms 5544 KB Output is correct
4 Correct 856 ms 6464 KB Output is correct
5 Correct 855 ms 6304 KB Output is correct
6 Correct 706 ms 5636 KB Output is correct
7 Correct 708 ms 5568 KB Output is correct
8 Correct 1078 ms 5556 KB Output is correct
9 Correct 1028 ms 5808 KB Output is correct
10 Correct 797 ms 6720 KB Output is correct
11 Correct 831 ms 5544 KB Output is correct
12 Correct 706 ms 6176 KB Output is correct
13 Correct 1045 ms 5060 KB Output is correct
14 Correct 1082 ms 5236 KB Output is correct
15 Correct 958 ms 5868 KB Output is correct
16 Correct 815 ms 6468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 651 ms 5544 KB Output is correct
30 Correct 856 ms 6464 KB Output is correct
31 Correct 855 ms 6304 KB Output is correct
32 Correct 706 ms 5636 KB Output is correct
33 Correct 708 ms 5568 KB Output is correct
34 Correct 1078 ms 5556 KB Output is correct
35 Correct 1028 ms 5808 KB Output is correct
36 Correct 797 ms 6720 KB Output is correct
37 Correct 831 ms 5544 KB Output is correct
38 Correct 706 ms 6176 KB Output is correct
39 Correct 1045 ms 5060 KB Output is correct
40 Correct 1082 ms 5236 KB Output is correct
41 Correct 958 ms 5868 KB Output is correct
42 Correct 815 ms 6468 KB Output is correct
43 Correct 918 ms 9872 KB Output is correct
44 Correct 1189 ms 10508 KB Output is correct
45 Correct 1178 ms 10856 KB Output is correct
46 Correct 912 ms 9992 KB Output is correct
47 Correct 971 ms 9996 KB Output is correct
48 Correct 1059 ms 9664 KB Output is correct
49 Correct 854 ms 10764 KB Output is correct
50 Correct 1020 ms 9672 KB Output is correct
51 Correct 1025 ms 10308 KB Output is correct
52 Correct 1010 ms 10844 KB Output is correct
53 Correct 1279 ms 9252 KB Output is correct
54 Correct 1352 ms 9756 KB Output is correct
55 Correct 1213 ms 10308 KB Output is correct
56 Correct 1077 ms 10944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 7 ms 340 KB Output is correct
30 Correct 4 ms 304 KB Output is correct
31 Correct 4 ms 308 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 4 ms 340 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 7 ms 340 KB Output is correct
38 Correct 8 ms 420 KB Output is correct
39 Correct 6 ms 340 KB Output is correct
40 Correct 4 ms 412 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 4 ms 340 KB Output is correct
43 Correct 3 ms 496 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 316 KB Output is correct
47 Correct 4 ms 340 KB Output is correct
48 Correct 4 ms 340 KB Output is correct
49 Correct 5 ms 308 KB Output is correct
50 Correct 5 ms 340 KB Output is correct
51 Correct 4 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 5 ms 420 KB Output is correct
54 Correct 5 ms 340 KB Output is correct
55 Runtime error 82 ms 4584 KB Execution killed with signal 11
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 7 ms 340 KB Output is correct
30 Correct 4 ms 304 KB Output is correct
31 Correct 4 ms 308 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 4 ms 340 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 7 ms 340 KB Output is correct
38 Correct 8 ms 420 KB Output is correct
39 Correct 6 ms 340 KB Output is correct
40 Correct 4 ms 412 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 4 ms 340 KB Output is correct
43 Correct 3 ms 496 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 316 KB Output is correct
47 Correct 4 ms 340 KB Output is correct
48 Correct 4 ms 340 KB Output is correct
49 Correct 5 ms 308 KB Output is correct
50 Correct 5 ms 340 KB Output is correct
51 Correct 4 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 5 ms 420 KB Output is correct
54 Correct 5 ms 340 KB Output is correct
55 Correct 651 ms 5544 KB Output is correct
56 Correct 856 ms 6464 KB Output is correct
57 Correct 855 ms 6304 KB Output is correct
58 Correct 706 ms 5636 KB Output is correct
59 Correct 708 ms 5568 KB Output is correct
60 Correct 1078 ms 5556 KB Output is correct
61 Correct 1028 ms 5808 KB Output is correct
62 Correct 797 ms 6720 KB Output is correct
63 Correct 831 ms 5544 KB Output is correct
64 Correct 706 ms 6176 KB Output is correct
65 Correct 1045 ms 5060 KB Output is correct
66 Correct 1082 ms 5236 KB Output is correct
67 Correct 958 ms 5868 KB Output is correct
68 Correct 815 ms 6468 KB Output is correct
69 Correct 918 ms 9872 KB Output is correct
70 Correct 1189 ms 10508 KB Output is correct
71 Correct 1178 ms 10856 KB Output is correct
72 Correct 912 ms 9992 KB Output is correct
73 Correct 971 ms 9996 KB Output is correct
74 Correct 1059 ms 9664 KB Output is correct
75 Correct 854 ms 10764 KB Output is correct
76 Correct 1020 ms 9672 KB Output is correct
77 Correct 1025 ms 10308 KB Output is correct
78 Correct 1010 ms 10844 KB Output is correct
79 Correct 1279 ms 9252 KB Output is correct
80 Correct 1352 ms 9756 KB Output is correct
81 Correct 1213 ms 10308 KB Output is correct
82 Correct 1077 ms 10944 KB Output is correct
83 Runtime error 82 ms 4584 KB Execution killed with signal 11
84 Halted 0 ms 0 KB -