답안 #765476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765476 2023-06-24T14:32:22 Z Hegdahl Joker (BOI20_joker) C++17
71 / 100
2000 ms 11576 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((m + 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 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 212 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 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 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 300 KB Output is correct
25 Correct 1 ms 212 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 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 212 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 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 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 300 KB Output is correct
25 Correct 1 ms 212 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 340 KB Output is correct
31 Correct 4 ms 384 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 3 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 6 ms 384 KB Output is correct
38 Correct 8 ms 316 KB Output is correct
39 Correct 6 ms 384 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 5 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 4 ms 376 KB Output is correct
48 Correct 4 ms 340 KB Output is correct
49 Correct 4 ms 340 KB Output is correct
50 Correct 4 ms 388 KB Output is correct
51 Correct 4 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 4 ms 340 KB Output is correct
54 Correct 4 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 665 ms 5556 KB Output is correct
4 Correct 874 ms 6460 KB Output is correct
5 Correct 864 ms 6300 KB Output is correct
6 Correct 702 ms 5552 KB Output is correct
7 Correct 714 ms 5544 KB Output is correct
8 Correct 1111 ms 5492 KB Output is correct
9 Correct 1035 ms 5800 KB Output is correct
10 Correct 786 ms 6716 KB Output is correct
11 Correct 858 ms 5568 KB Output is correct
12 Correct 731 ms 6168 KB Output is correct
13 Correct 1094 ms 5060 KB Output is correct
14 Correct 1109 ms 5240 KB Output is correct
15 Correct 933 ms 5868 KB Output is correct
16 Correct 825 ms 6464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 212 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 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 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 300 KB Output is correct
25 Correct 1 ms 212 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 665 ms 5556 KB Output is correct
30 Correct 874 ms 6460 KB Output is correct
31 Correct 864 ms 6300 KB Output is correct
32 Correct 702 ms 5552 KB Output is correct
33 Correct 714 ms 5544 KB Output is correct
34 Correct 1111 ms 5492 KB Output is correct
35 Correct 1035 ms 5800 KB Output is correct
36 Correct 786 ms 6716 KB Output is correct
37 Correct 858 ms 5568 KB Output is correct
38 Correct 731 ms 6168 KB Output is correct
39 Correct 1094 ms 5060 KB Output is correct
40 Correct 1109 ms 5240 KB Output is correct
41 Correct 933 ms 5868 KB Output is correct
42 Correct 825 ms 6464 KB Output is correct
43 Correct 907 ms 5556 KB Output is correct
44 Correct 1168 ms 6464 KB Output is correct
45 Correct 1150 ms 6464 KB Output is correct
46 Correct 921 ms 5680 KB Output is correct
47 Correct 946 ms 5752 KB Output is correct
48 Correct 1095 ms 5816 KB Output is correct
49 Correct 862 ms 6720 KB Output is correct
50 Correct 1012 ms 5440 KB Output is correct
51 Correct 1030 ms 5868 KB Output is correct
52 Correct 1018 ms 6468 KB Output is correct
53 Correct 1305 ms 5052 KB Output is correct
54 Correct 1361 ms 5524 KB Output is correct
55 Correct 1237 ms 5964 KB Output is correct
56 Correct 1144 ms 6460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 212 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 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 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 300 KB Output is correct
25 Correct 1 ms 212 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 340 KB Output is correct
31 Correct 4 ms 384 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 3 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 6 ms 384 KB Output is correct
38 Correct 8 ms 316 KB Output is correct
39 Correct 6 ms 384 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 5 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 4 ms 376 KB Output is correct
48 Correct 4 ms 340 KB Output is correct
49 Correct 4 ms 340 KB Output is correct
50 Correct 4 ms 388 KB Output is correct
51 Correct 4 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 4 ms 340 KB Output is correct
54 Correct 4 ms 340 KB Output is correct
55 Correct 782 ms 2712 KB Output is correct
56 Correct 1675 ms 3496 KB Output is correct
57 Correct 784 ms 3272 KB Output is correct
58 Correct 627 ms 2636 KB Output is correct
59 Correct 1033 ms 2708 KB Output is correct
60 Correct 988 ms 5580 KB Output is correct
61 Correct 837 ms 5040 KB Output is correct
62 Correct 857 ms 5844 KB Output is correct
63 Correct 992 ms 4504 KB Output is correct
64 Correct 1020 ms 5324 KB Output is correct
65 Correct 956 ms 5852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 212 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 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 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 300 KB Output is correct
25 Correct 1 ms 212 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 340 KB Output is correct
31 Correct 4 ms 384 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 3 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 6 ms 384 KB Output is correct
38 Correct 8 ms 316 KB Output is correct
39 Correct 6 ms 384 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 5 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 4 ms 376 KB Output is correct
48 Correct 4 ms 340 KB Output is correct
49 Correct 4 ms 340 KB Output is correct
50 Correct 4 ms 388 KB Output is correct
51 Correct 4 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 4 ms 340 KB Output is correct
54 Correct 4 ms 340 KB Output is correct
55 Correct 665 ms 5556 KB Output is correct
56 Correct 874 ms 6460 KB Output is correct
57 Correct 864 ms 6300 KB Output is correct
58 Correct 702 ms 5552 KB Output is correct
59 Correct 714 ms 5544 KB Output is correct
60 Correct 1111 ms 5492 KB Output is correct
61 Correct 1035 ms 5800 KB Output is correct
62 Correct 786 ms 6716 KB Output is correct
63 Correct 858 ms 5568 KB Output is correct
64 Correct 731 ms 6168 KB Output is correct
65 Correct 1094 ms 5060 KB Output is correct
66 Correct 1109 ms 5240 KB Output is correct
67 Correct 933 ms 5868 KB Output is correct
68 Correct 825 ms 6464 KB Output is correct
69 Correct 907 ms 5556 KB Output is correct
70 Correct 1168 ms 6464 KB Output is correct
71 Correct 1150 ms 6464 KB Output is correct
72 Correct 921 ms 5680 KB Output is correct
73 Correct 946 ms 5752 KB Output is correct
74 Correct 1095 ms 5816 KB Output is correct
75 Correct 862 ms 6720 KB Output is correct
76 Correct 1012 ms 5440 KB Output is correct
77 Correct 1030 ms 5868 KB Output is correct
78 Correct 1018 ms 6468 KB Output is correct
79 Correct 1305 ms 5052 KB Output is correct
80 Correct 1361 ms 5524 KB Output is correct
81 Correct 1237 ms 5964 KB Output is correct
82 Correct 1144 ms 6460 KB Output is correct
83 Correct 782 ms 2712 KB Output is correct
84 Correct 1675 ms 3496 KB Output is correct
85 Correct 784 ms 3272 KB Output is correct
86 Correct 627 ms 2636 KB Output is correct
87 Correct 1033 ms 2708 KB Output is correct
88 Correct 988 ms 5580 KB Output is correct
89 Correct 837 ms 5040 KB Output is correct
90 Correct 857 ms 5844 KB Output is correct
91 Correct 992 ms 4504 KB Output is correct
92 Correct 1020 ms 5324 KB Output is correct
93 Correct 956 ms 5852 KB Output is correct
94 Correct 1187 ms 11396 KB Output is correct
95 Execution timed out 2072 ms 11576 KB Time limit exceeded
96 Halted 0 ms 0 KB -