답안 #765484

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765484 2023-06-24T14:46:08 Z Hegdahl Joker (BOI20_joker) C++17
100 / 100
1926 ms 12444 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

struct UnionFind {
  vector<int> parent_or_neg_size, edge_parity;
  struct UndoInfo {
    int i, j, j_sz;
  };
  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};
  }

  int path_compressing_find(int i) {
    if (parent_or_neg_size[i] < 0) {
      edge_parity[i] = 0;
      return i;
    }

    int new_parent = path_compressing_find(parent_or_neg_size[i]);
    edge_parity[i] ^= edge_parity[parent_or_neg_size[i]];
    return parent_or_neg_size[i] = new_parent;
  }

  enum UniteResult { NO_CYCLE, EVEN_CYCLE, ODD_CYCLE };
  UniteResult persistent_unite(int i, int j, bool new_edge_parity = true) {
    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) {
      undo_info.push_back({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);
    }

    undo_info.push_back({
        .i = root_i,
        .j = root_j,
        .j_sz = parent_or_neg_size[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;
  }

  UniteResult fast_unite(int i, int j, bool new_edge_parity = true) {
    int pi = path_compressing_find(i);
    int pj = path_compressing_find(j);
    if (pi == pj) {
      if (edge_parity[i] ^ edge_parity[j] ^ new_edge_parity) {
        return ODD_CYCLE;
      } else {
        return EVEN_CYCLE;
      }
    }

    if (parent_or_neg_size[pi] > parent_or_neg_size[pj]) {
      swap(pi, pj);
      swap(i, j);
    }

    parent_or_neg_size[pi] += parent_or_neg_size[pj];
    parent_or_neg_size[pj] = pi;
    edge_parity[pj] = edge_parity[i] ^ edge_parity[j] ^ new_edge_parity;

    return NO_CYCLE;
  }

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

    parent_or_neg_size[j] = j_sz;
    parent_or_neg_size[i] -= j_sz;
  }

  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.fast_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.fast_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.persistent_unite(u, v) == 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 1 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 212 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 212 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 1 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 212 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 212 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 4 ms 340 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 3 ms 340 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 312 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 4 ms 340 KB Output is correct
38 Correct 5 ms 340 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 3 ms 328 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 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 3 ms 340 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 3 ms 368 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 3 ms 340 KB Output is correct
53 Correct 3 ms 340 KB Output is correct
54 Correct 3 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 670 ms 5564 KB Output is correct
4 Correct 901 ms 6468 KB Output is correct
5 Correct 844 ms 6300 KB Output is correct
6 Correct 717 ms 5608 KB Output is correct
7 Correct 705 ms 5548 KB Output is correct
8 Correct 842 ms 5560 KB Output is correct
9 Correct 928 ms 5808 KB Output is correct
10 Correct 833 ms 6720 KB Output is correct
11 Correct 828 ms 5548 KB Output is correct
12 Correct 688 ms 6288 KB Output is correct
13 Correct 559 ms 5064 KB Output is correct
14 Correct 875 ms 5328 KB Output is correct
15 Correct 890 ms 5944 KB Output is correct
16 Correct 781 ms 6476 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 1 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 212 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 212 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 670 ms 5564 KB Output is correct
30 Correct 901 ms 6468 KB Output is correct
31 Correct 844 ms 6300 KB Output is correct
32 Correct 717 ms 5608 KB Output is correct
33 Correct 705 ms 5548 KB Output is correct
34 Correct 842 ms 5560 KB Output is correct
35 Correct 928 ms 5808 KB Output is correct
36 Correct 833 ms 6720 KB Output is correct
37 Correct 828 ms 5548 KB Output is correct
38 Correct 688 ms 6288 KB Output is correct
39 Correct 559 ms 5064 KB Output is correct
40 Correct 875 ms 5328 KB Output is correct
41 Correct 890 ms 5944 KB Output is correct
42 Correct 781 ms 6476 KB Output is correct
43 Correct 779 ms 5612 KB Output is correct
44 Correct 1025 ms 6464 KB Output is correct
45 Correct 1006 ms 6588 KB Output is correct
46 Correct 789 ms 5688 KB Output is correct
47 Correct 834 ms 5680 KB Output is correct
48 Correct 944 ms 5828 KB Output is correct
49 Correct 803 ms 6716 KB Output is correct
50 Correct 907 ms 5440 KB Output is correct
51 Correct 908 ms 5968 KB Output is correct
52 Correct 910 ms 6464 KB Output is correct
53 Correct 696 ms 5048 KB Output is correct
54 Correct 1128 ms 5456 KB Output is correct
55 Correct 1027 ms 5992 KB Output is correct
56 Correct 938 ms 6580 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 1 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 212 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 212 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 4 ms 340 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 3 ms 340 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 312 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 4 ms 340 KB Output is correct
38 Correct 5 ms 340 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 3 ms 328 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 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 3 ms 340 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 3 ms 368 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 3 ms 340 KB Output is correct
53 Correct 3 ms 340 KB Output is correct
54 Correct 3 ms 340 KB Output is correct
55 Correct 805 ms 2696 KB Output is correct
56 Correct 1704 ms 3488 KB Output is correct
57 Correct 788 ms 3408 KB Output is correct
58 Correct 641 ms 2692 KB Output is correct
59 Correct 955 ms 2696 KB Output is correct
60 Correct 945 ms 3176 KB Output is correct
61 Correct 842 ms 2696 KB Output is correct
62 Correct 850 ms 3264 KB Output is correct
63 Correct 684 ms 2232 KB Output is correct
64 Correct 972 ms 2864 KB Output is correct
65 Correct 961 ms 3324 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 1 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 212 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 212 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 4 ms 340 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 3 ms 340 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 312 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 4 ms 340 KB Output is correct
38 Correct 5 ms 340 KB Output is correct
39 Correct 4 ms 340 KB Output is correct
40 Correct 3 ms 328 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 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 3 ms 340 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 3 ms 368 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 3 ms 340 KB Output is correct
53 Correct 3 ms 340 KB Output is correct
54 Correct 3 ms 340 KB Output is correct
55 Correct 670 ms 5564 KB Output is correct
56 Correct 901 ms 6468 KB Output is correct
57 Correct 844 ms 6300 KB Output is correct
58 Correct 717 ms 5608 KB Output is correct
59 Correct 705 ms 5548 KB Output is correct
60 Correct 842 ms 5560 KB Output is correct
61 Correct 928 ms 5808 KB Output is correct
62 Correct 833 ms 6720 KB Output is correct
63 Correct 828 ms 5548 KB Output is correct
64 Correct 688 ms 6288 KB Output is correct
65 Correct 559 ms 5064 KB Output is correct
66 Correct 875 ms 5328 KB Output is correct
67 Correct 890 ms 5944 KB Output is correct
68 Correct 781 ms 6476 KB Output is correct
69 Correct 779 ms 5612 KB Output is correct
70 Correct 1025 ms 6464 KB Output is correct
71 Correct 1006 ms 6588 KB Output is correct
72 Correct 789 ms 5688 KB Output is correct
73 Correct 834 ms 5680 KB Output is correct
74 Correct 944 ms 5828 KB Output is correct
75 Correct 803 ms 6716 KB Output is correct
76 Correct 907 ms 5440 KB Output is correct
77 Correct 908 ms 5968 KB Output is correct
78 Correct 910 ms 6464 KB Output is correct
79 Correct 696 ms 5048 KB Output is correct
80 Correct 1128 ms 5456 KB Output is correct
81 Correct 1027 ms 5992 KB Output is correct
82 Correct 938 ms 6580 KB Output is correct
83 Correct 805 ms 2696 KB Output is correct
84 Correct 1704 ms 3488 KB Output is correct
85 Correct 788 ms 3408 KB Output is correct
86 Correct 641 ms 2692 KB Output is correct
87 Correct 955 ms 2696 KB Output is correct
88 Correct 945 ms 3176 KB Output is correct
89 Correct 842 ms 2696 KB Output is correct
90 Correct 850 ms 3264 KB Output is correct
91 Correct 684 ms 2232 KB Output is correct
92 Correct 972 ms 2864 KB Output is correct
93 Correct 961 ms 3324 KB Output is correct
94 Correct 1092 ms 6460 KB Output is correct
95 Correct 1926 ms 7116 KB Output is correct
96 Correct 1071 ms 12208 KB Output is correct
97 Correct 870 ms 11372 KB Output is correct
98 Correct 789 ms 11400 KB Output is correct
99 Correct 897 ms 11056 KB Output is correct
100 Correct 881 ms 12444 KB Output is correct
101 Correct 995 ms 11176 KB Output is correct
102 Correct 1110 ms 11824 KB Output is correct
103 Correct 1116 ms 12384 KB Output is correct
104 Correct 855 ms 10836 KB Output is correct
105 Correct 1201 ms 11636 KB Output is correct
106 Correct 1268 ms 12236 KB Output is correct
107 Correct 974 ms 11828 KB Output is correct
108 Correct 1384 ms 11060 KB Output is correct
109 Correct 1436 ms 11220 KB Output is correct
110 Correct 1405 ms 11260 KB Output is correct
111 Correct 1399 ms 11256 KB Output is correct
112 Correct 1387 ms 11292 KB Output is correct
113 Correct 1403 ms 11276 KB Output is correct
114 Correct 1417 ms 11272 KB Output is correct
115 Correct 1472 ms 11236 KB Output is correct
116 Correct 1385 ms 11224 KB Output is correct