Submission #765482

# Submission time Handle Problem Language Result Execution time Memory
765482 2023-06-24T14:38:55 Z Hegdahl Joker (BOI20_joker) C++17
71 / 100
2000 ms 8396 KB
#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};
  }

  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});
      }

      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,
          .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;
  }

  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.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';
  }
}

# Verdict Execution time Memory Grader output
1 Correct 1 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 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 312 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 312 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
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 312 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 312 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 4 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 2 ms 340 KB Output is correct
34 Correct 3 ms 372 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 6 ms 340 KB Output is correct
38 Correct 7 ms 340 KB Output is correct
39 Correct 6 ms 340 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 4 ms 364 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 ms 372 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 4 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 4 ms 372 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 4 ms 368 KB Output is correct
54 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 649 ms 5552 KB Output is correct
4 Correct 853 ms 6580 KB Output is correct
5 Correct 847 ms 6304 KB Output is correct
6 Correct 697 ms 5632 KB Output is correct
7 Correct 698 ms 5552 KB Output is correct
8 Correct 1070 ms 5492 KB Output is correct
9 Correct 1010 ms 5804 KB Output is correct
10 Correct 767 ms 6720 KB Output is correct
11 Correct 828 ms 5696 KB Output is correct
12 Correct 715 ms 6416 KB Output is correct
13 Correct 1020 ms 5064 KB Output is correct
14 Correct 1153 ms 5368 KB Output is correct
15 Correct 949 ms 6076 KB Output is correct
16 Correct 792 ms 6584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 312 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 312 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 649 ms 5552 KB Output is correct
30 Correct 853 ms 6580 KB Output is correct
31 Correct 847 ms 6304 KB Output is correct
32 Correct 697 ms 5632 KB Output is correct
33 Correct 698 ms 5552 KB Output is correct
34 Correct 1070 ms 5492 KB Output is correct
35 Correct 1010 ms 5804 KB Output is correct
36 Correct 767 ms 6720 KB Output is correct
37 Correct 828 ms 5696 KB Output is correct
38 Correct 715 ms 6416 KB Output is correct
39 Correct 1020 ms 5064 KB Output is correct
40 Correct 1153 ms 5368 KB Output is correct
41 Correct 949 ms 6076 KB Output is correct
42 Correct 792 ms 6584 KB Output is correct
43 Correct 869 ms 5556 KB Output is correct
44 Correct 1125 ms 6716 KB Output is correct
45 Correct 1119 ms 6592 KB Output is correct
46 Correct 884 ms 5808 KB Output is correct
47 Correct 889 ms 5804 KB Output is correct
48 Correct 1044 ms 5936 KB Output is correct
49 Correct 843 ms 6740 KB Output is correct
50 Correct 936 ms 5392 KB Output is correct
51 Correct 994 ms 5952 KB Output is correct
52 Correct 976 ms 6460 KB Output is correct
53 Correct 1217 ms 5056 KB Output is correct
54 Correct 1322 ms 5524 KB Output is correct
55 Correct 1140 ms 6080 KB Output is correct
56 Correct 1027 ms 6596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 312 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 312 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 4 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 2 ms 340 KB Output is correct
34 Correct 3 ms 372 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 6 ms 340 KB Output is correct
38 Correct 7 ms 340 KB Output is correct
39 Correct 6 ms 340 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 4 ms 364 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 ms 372 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 4 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 4 ms 372 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 4 ms 368 KB Output is correct
54 Correct 4 ms 340 KB Output is correct
55 Correct 766 ms 2832 KB Output is correct
56 Correct 1660 ms 3496 KB Output is correct
57 Correct 787 ms 3452 KB Output is correct
58 Correct 627 ms 2828 KB Output is correct
59 Correct 1012 ms 2824 KB Output is correct
60 Correct 957 ms 3304 KB Output is correct
61 Correct 847 ms 4620 KB Output is correct
62 Correct 856 ms 5300 KB Output is correct
63 Correct 952 ms 4060 KB Output is correct
64 Correct 1000 ms 4824 KB Output is correct
65 Correct 927 ms 5200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 312 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 312 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 4 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 2 ms 340 KB Output is correct
34 Correct 3 ms 372 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 4 ms 340 KB Output is correct
37 Correct 6 ms 340 KB Output is correct
38 Correct 7 ms 340 KB Output is correct
39 Correct 6 ms 340 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 4 ms 340 KB Output is correct
42 Correct 4 ms 364 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 ms 372 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 4 ms 340 KB Output is correct
49 Correct 3 ms 340 KB Output is correct
50 Correct 4 ms 372 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 4 ms 340 KB Output is correct
53 Correct 4 ms 368 KB Output is correct
54 Correct 4 ms 340 KB Output is correct
55 Correct 649 ms 5552 KB Output is correct
56 Correct 853 ms 6580 KB Output is correct
57 Correct 847 ms 6304 KB Output is correct
58 Correct 697 ms 5632 KB Output is correct
59 Correct 698 ms 5552 KB Output is correct
60 Correct 1070 ms 5492 KB Output is correct
61 Correct 1010 ms 5804 KB Output is correct
62 Correct 767 ms 6720 KB Output is correct
63 Correct 828 ms 5696 KB Output is correct
64 Correct 715 ms 6416 KB Output is correct
65 Correct 1020 ms 5064 KB Output is correct
66 Correct 1153 ms 5368 KB Output is correct
67 Correct 949 ms 6076 KB Output is correct
68 Correct 792 ms 6584 KB Output is correct
69 Correct 869 ms 5556 KB Output is correct
70 Correct 1125 ms 6716 KB Output is correct
71 Correct 1119 ms 6592 KB Output is correct
72 Correct 884 ms 5808 KB Output is correct
73 Correct 889 ms 5804 KB Output is correct
74 Correct 1044 ms 5936 KB Output is correct
75 Correct 843 ms 6740 KB Output is correct
76 Correct 936 ms 5392 KB Output is correct
77 Correct 994 ms 5952 KB Output is correct
78 Correct 976 ms 6460 KB Output is correct
79 Correct 1217 ms 5056 KB Output is correct
80 Correct 1322 ms 5524 KB Output is correct
81 Correct 1140 ms 6080 KB Output is correct
82 Correct 1027 ms 6596 KB Output is correct
83 Correct 766 ms 2832 KB Output is correct
84 Correct 1660 ms 3496 KB Output is correct
85 Correct 787 ms 3452 KB Output is correct
86 Correct 627 ms 2828 KB Output is correct
87 Correct 1012 ms 2824 KB Output is correct
88 Correct 957 ms 3304 KB Output is correct
89 Correct 847 ms 4620 KB Output is correct
90 Correct 856 ms 5300 KB Output is correct
91 Correct 952 ms 4060 KB Output is correct
92 Correct 1000 ms 4824 KB Output is correct
93 Correct 927 ms 5200 KB Output is correct
94 Correct 1155 ms 8380 KB Output is correct
95 Execution timed out 2045 ms 8396 KB Time limit exceeded
96 Halted 0 ms 0 KB -