답안 #766020

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
766020 2023-06-25T08:54:46 Z Hegdahl Joker (BOI20_joker) C++17
100 / 100
1413 ms 9040 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() {
  cin.tie(0)->sync_with_stdio(0);
  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 = 800;

  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 0 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 324 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 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 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 320 KB Output is correct
16 Correct 1 ms 340 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 320 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 316 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 324 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 0 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 324 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 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 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 320 KB Output is correct
16 Correct 1 ms 340 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 320 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 316 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 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 5 ms 340 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 368 KB Output is correct
34 Correct 3 ms 412 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 7 ms 460 KB Output is correct
38 Correct 6 ms 340 KB Output is correct
39 Correct 5 ms 340 KB Output is correct
40 Correct 3 ms 332 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 328 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 328 KB Output is correct
45 Correct 1 ms 372 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 3 ms 328 KB Output is correct
48 Correct 3 ms 328 KB Output is correct
49 Correct 4 ms 328 KB Output is correct
50 Correct 3 ms 340 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 332 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 306 ms 7268 KB Output is correct
4 Correct 443 ms 8012 KB Output is correct
5 Correct 388 ms 7820 KB Output is correct
6 Correct 329 ms 7104 KB Output is correct
7 Correct 322 ms 7188 KB Output is correct
8 Correct 404 ms 7040 KB Output is correct
9 Correct 451 ms 7240 KB Output is correct
10 Correct 367 ms 8240 KB Output is correct
11 Correct 389 ms 7116 KB Output is correct
12 Correct 327 ms 7724 KB Output is correct
13 Correct 269 ms 6572 KB Output is correct
14 Correct 396 ms 6864 KB Output is correct
15 Correct 425 ms 7460 KB Output is correct
16 Correct 372 ms 8012 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 0 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 324 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 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 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 320 KB Output is correct
16 Correct 1 ms 340 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 320 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 316 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 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 306 ms 7268 KB Output is correct
30 Correct 443 ms 8012 KB Output is correct
31 Correct 388 ms 7820 KB Output is correct
32 Correct 329 ms 7104 KB Output is correct
33 Correct 322 ms 7188 KB Output is correct
34 Correct 404 ms 7040 KB Output is correct
35 Correct 451 ms 7240 KB Output is correct
36 Correct 367 ms 8240 KB Output is correct
37 Correct 389 ms 7116 KB Output is correct
38 Correct 327 ms 7724 KB Output is correct
39 Correct 269 ms 6572 KB Output is correct
40 Correct 396 ms 6864 KB Output is correct
41 Correct 425 ms 7460 KB Output is correct
42 Correct 372 ms 8012 KB Output is correct
43 Correct 415 ms 7364 KB Output is correct
44 Correct 562 ms 8268 KB Output is correct
45 Correct 560 ms 8268 KB Output is correct
46 Correct 423 ms 7408 KB Output is correct
47 Correct 430 ms 7592 KB Output is correct
48 Correct 462 ms 7476 KB Output is correct
49 Correct 398 ms 8472 KB Output is correct
50 Correct 489 ms 7264 KB Output is correct
51 Correct 498 ms 7744 KB Output is correct
52 Correct 482 ms 8256 KB Output is correct
53 Correct 352 ms 6940 KB Output is correct
54 Correct 588 ms 7332 KB Output is correct
55 Correct 569 ms 7676 KB Output is correct
56 Correct 522 ms 8260 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 0 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 324 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 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 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 320 KB Output is correct
16 Correct 1 ms 340 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 320 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 316 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 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 5 ms 340 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 368 KB Output is correct
34 Correct 3 ms 412 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 7 ms 460 KB Output is correct
38 Correct 6 ms 340 KB Output is correct
39 Correct 5 ms 340 KB Output is correct
40 Correct 3 ms 332 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 328 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 328 KB Output is correct
45 Correct 1 ms 372 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 3 ms 328 KB Output is correct
48 Correct 3 ms 328 KB Output is correct
49 Correct 4 ms 328 KB Output is correct
50 Correct 3 ms 340 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 332 KB Output is correct
54 Correct 4 ms 340 KB Output is correct
55 Correct 395 ms 3500 KB Output is correct
56 Correct 842 ms 4908 KB Output is correct
57 Correct 366 ms 4104 KB Output is correct
58 Correct 307 ms 3532 KB Output is correct
59 Correct 456 ms 3508 KB Output is correct
60 Correct 470 ms 4532 KB Output is correct
61 Correct 412 ms 3788 KB Output is correct
62 Correct 410 ms 4556 KB Output is correct
63 Correct 314 ms 3348 KB Output is correct
64 Correct 465 ms 3876 KB Output is correct
65 Correct 469 ms 4584 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 0 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 324 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 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 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 320 KB Output is correct
16 Correct 1 ms 340 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 320 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 316 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 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 5 ms 340 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 368 KB Output is correct
34 Correct 3 ms 412 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 7 ms 460 KB Output is correct
38 Correct 6 ms 340 KB Output is correct
39 Correct 5 ms 340 KB Output is correct
40 Correct 3 ms 332 KB Output is correct
41 Correct 3 ms 340 KB Output is correct
42 Correct 3 ms 328 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 328 KB Output is correct
45 Correct 1 ms 372 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 3 ms 328 KB Output is correct
48 Correct 3 ms 328 KB Output is correct
49 Correct 4 ms 328 KB Output is correct
50 Correct 3 ms 340 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 332 KB Output is correct
54 Correct 4 ms 340 KB Output is correct
55 Correct 306 ms 7268 KB Output is correct
56 Correct 443 ms 8012 KB Output is correct
57 Correct 388 ms 7820 KB Output is correct
58 Correct 329 ms 7104 KB Output is correct
59 Correct 322 ms 7188 KB Output is correct
60 Correct 404 ms 7040 KB Output is correct
61 Correct 451 ms 7240 KB Output is correct
62 Correct 367 ms 8240 KB Output is correct
63 Correct 389 ms 7116 KB Output is correct
64 Correct 327 ms 7724 KB Output is correct
65 Correct 269 ms 6572 KB Output is correct
66 Correct 396 ms 6864 KB Output is correct
67 Correct 425 ms 7460 KB Output is correct
68 Correct 372 ms 8012 KB Output is correct
69 Correct 415 ms 7364 KB Output is correct
70 Correct 562 ms 8268 KB Output is correct
71 Correct 560 ms 8268 KB Output is correct
72 Correct 423 ms 7408 KB Output is correct
73 Correct 430 ms 7592 KB Output is correct
74 Correct 462 ms 7476 KB Output is correct
75 Correct 398 ms 8472 KB Output is correct
76 Correct 489 ms 7264 KB Output is correct
77 Correct 498 ms 7744 KB Output is correct
78 Correct 482 ms 8256 KB Output is correct
79 Correct 352 ms 6940 KB Output is correct
80 Correct 588 ms 7332 KB Output is correct
81 Correct 569 ms 7676 KB Output is correct
82 Correct 522 ms 8260 KB Output is correct
83 Correct 395 ms 3500 KB Output is correct
84 Correct 842 ms 4908 KB Output is correct
85 Correct 366 ms 4104 KB Output is correct
86 Correct 307 ms 3532 KB Output is correct
87 Correct 456 ms 3508 KB Output is correct
88 Correct 470 ms 4532 KB Output is correct
89 Correct 412 ms 3788 KB Output is correct
90 Correct 410 ms 4556 KB Output is correct
91 Correct 314 ms 3348 KB Output is correct
92 Correct 465 ms 3876 KB Output is correct
93 Correct 469 ms 4584 KB Output is correct
94 Correct 794 ms 8352 KB Output is correct
95 Correct 1413 ms 9008 KB Output is correct
96 Correct 780 ms 8780 KB Output is correct
97 Correct 545 ms 8200 KB Output is correct
98 Correct 552 ms 8260 KB Output is correct
99 Correct 427 ms 7952 KB Output is correct
100 Correct 418 ms 9040 KB Output is correct
101 Correct 617 ms 8140 KB Output is correct
102 Correct 701 ms 8556 KB Output is correct
103 Correct 763 ms 9036 KB Output is correct
104 Correct 565 ms 7764 KB Output is correct
105 Correct 785 ms 8392 KB Output is correct
106 Correct 861 ms 8816 KB Output is correct
107 Correct 1179 ms 8160 KB Output is correct
108 Correct 1221 ms 8108 KB Output is correct
109 Correct 1308 ms 8144 KB Output is correct
110 Correct 1320 ms 8012 KB Output is correct
111 Correct 1335 ms 8112 KB Output is correct
112 Correct 1346 ms 8396 KB Output is correct
113 Correct 1342 ms 8040 KB Output is correct
114 Correct 1338 ms 8220 KB Output is correct
115 Correct 1337 ms 8120 KB Output is correct
116 Correct 1332 ms 8140 KB Output is correct