Submission #823906

# Submission time Handle Problem Language Result Execution time Memory
823906 2023-08-13T09:58:24 Z tch1cherin Joker (BOI20_joker) C++17
100 / 100
561 ms 50548 KB
#include <bits/stdc++.h>
using namespace std;

struct dsu {
  int size;
  int _bipartite;
  vector<vector<pair<int*, int>>> updates;
  vector<int> parent, rank;

  dsu(int _size) : size(_size), _bipartite(1), parent(2 * _size, -1), rank(2 * _size, 1) {}

  int _get(int u) {
    return parent[u] == -1 ? u : _get(parent[u]);
  }

  void _unite(int u, int v) {
    u = _get(u), v = _get(v);
    if (u != v) {
      if (rank[u] < rank[v]) {
        swap(u, v);
      }
      updates.back().push_back(pair {&parent[v], parent[v]});
      updates.back().push_back(pair {&rank[u], rank[u]});
      parent[v] = u;
      rank[u] += rank[v];
    }
  }

  void unite(int u, int v) {
    updates.push_back({});
    updates.back().push_back(pair {&_bipartite, _bipartite});
    _unite(u, v + size);
    _unite(v, u + size);
    if (_get(u) == _get(u + size)) {
      _bipartite = 0;
    }
    if (_get(v) == _get(v + size)) {
      _bipartite = 0;
    }
  }

  void undo() {
    for (auto &[x, y] : updates.back()) {
      *x = y;
    }
    updates.pop_back();
  }

  bool is_bipartite() {
    return _bipartite > 0;
  }
};

struct dsu_queue {
  struct update {
    int type;
    int u, v;
  };

  int size;
  vector<update> updates;
  int sign;
  int cntA = 0, cntB = 0;
  dsu base;

  dsu_queue(int _size) : size(_size), sign(1), base(_size) {}

  void unite(int u, int v) {
    base.unite(u, v);
    (sign > 0 ? cntA : cntB)++;
    updates.push_back({sign, u, v});
  }

  void undo() {
    if (sign == 1) {
      for (int i = 0; i < (int)updates.size(); i++) {
        base.undo();
      }
      reverse(updates.begin(), updates.end());
      for (auto [type, x, y] : updates) {
        base.unite(x, y);
      }
      sign = -1;
      swap(cntA, cntB);
    }
    vector<update> old_updates, new_updates;
    while (old_updates.size() < new_updates.size() || old_updates.empty()) {
      if (updates.empty()) {
        break;
      }
      if (updates.back().type > 0) {
        old_updates.push_back(updates.back());
      } else {
        new_updates.push_back(updates.back());
      }
      updates.pop_back();
      base.undo();
    }
    for (int i = (int)new_updates.size() - 1; i >= 0; i--) {
      auto [type, x, y] = new_updates[i];
      base.unite(x, y);
      updates.push_back({type, x, y});
    }
    for (int i = (int)old_updates.size() - 1; i > 0; i--) {
      auto [type, x, y] = old_updates[i];
      base.unite(x, y);
      updates.push_back({type, x, y});
    }
    cntA--;
    if (cntA == 0) {
      cntA = cntB;
      for (auto &[type, x, y] : updates) {
        type = 1;
      }
      sign = 1;
    }
  }

  bool is_bipartite() {
    return base.is_bipartite();
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<pair<int, int>> edges(M);
  dsu_queue q(N);
  for (auto &[u, v] : edges) {
    cin >> u >> v;
    u--, v--;
    q.unite(u, v);
  }
  vector<int> R(M);
  for (int i = 0, j = 0; i < M; i++) {
    while (j < M && !q.is_bipartite()) {
      q.undo();
      j++;
    }
    if (q.is_bipartite()) {
      R[i] = j;
    } else {
      R[i] = M + 1;
    }
    if (j > i) {
      q.unite(edges[i].first, edges[i].second);
    }
  }
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    cout << (R[--l] > r ? "YES\n" : "NO\n");
  }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 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 328 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 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 328 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 3 ms 716 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 3 ms 692 KB Output is correct
35 Correct 3 ms 724 KB Output is correct
36 Correct 4 ms 580 KB Output is correct
37 Correct 3 ms 724 KB Output is correct
38 Correct 1 ms 724 KB Output is correct
39 Correct 3 ms 724 KB Output is correct
40 Correct 2 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 3 ms 584 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 3 ms 596 KB Output is correct
46 Correct 3 ms 724 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 3 ms 636 KB Output is correct
49 Correct 3 ms 596 KB Output is correct
50 Correct 3 ms 724 KB Output is correct
51 Correct 2 ms 596 KB Output is correct
52 Correct 3 ms 596 KB Output is correct
53 Correct 3 ms 596 KB Output is correct
54 Correct 3 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 464 ms 33112 KB Output is correct
4 Correct 113 ms 45284 KB Output is correct
5 Correct 275 ms 45924 KB Output is correct
6 Correct 294 ms 33132 KB Output is correct
7 Correct 353 ms 33136 KB Output is correct
8 Correct 292 ms 27584 KB Output is correct
9 Correct 289 ms 32376 KB Output is correct
10 Correct 408 ms 41704 KB Output is correct
11 Correct 429 ms 34200 KB Output is correct
12 Correct 300 ms 40876 KB Output is correct
13 Correct 177 ms 22964 KB Output is correct
14 Correct 265 ms 28100 KB Output is correct
15 Correct 422 ms 37156 KB Output is correct
16 Correct 422 ms 42208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 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 328 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 464 ms 33112 KB Output is correct
30 Correct 113 ms 45284 KB Output is correct
31 Correct 275 ms 45924 KB Output is correct
32 Correct 294 ms 33132 KB Output is correct
33 Correct 353 ms 33136 KB Output is correct
34 Correct 292 ms 27584 KB Output is correct
35 Correct 289 ms 32376 KB Output is correct
36 Correct 408 ms 41704 KB Output is correct
37 Correct 429 ms 34200 KB Output is correct
38 Correct 300 ms 40876 KB Output is correct
39 Correct 177 ms 22964 KB Output is correct
40 Correct 265 ms 28100 KB Output is correct
41 Correct 422 ms 37156 KB Output is correct
42 Correct 422 ms 42208 KB Output is correct
43 Correct 561 ms 33368 KB Output is correct
44 Correct 104 ms 45616 KB Output is correct
45 Correct 327 ms 46912 KB Output is correct
46 Correct 339 ms 33500 KB Output is correct
47 Correct 509 ms 33500 KB Output is correct
48 Correct 338 ms 32908 KB Output is correct
49 Correct 359 ms 42200 KB Output is correct
50 Correct 435 ms 31836 KB Output is correct
51 Correct 417 ms 38848 KB Output is correct
52 Correct 404 ms 42492 KB Output is correct
53 Correct 185 ms 23232 KB Output is correct
54 Correct 349 ms 30888 KB Output is correct
55 Correct 396 ms 37548 KB Output is correct
56 Correct 358 ms 42488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 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 328 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 3 ms 716 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 3 ms 692 KB Output is correct
35 Correct 3 ms 724 KB Output is correct
36 Correct 4 ms 580 KB Output is correct
37 Correct 3 ms 724 KB Output is correct
38 Correct 1 ms 724 KB Output is correct
39 Correct 3 ms 724 KB Output is correct
40 Correct 2 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 3 ms 584 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 3 ms 596 KB Output is correct
46 Correct 3 ms 724 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 3 ms 636 KB Output is correct
49 Correct 3 ms 596 KB Output is correct
50 Correct 3 ms 724 KB Output is correct
51 Correct 2 ms 596 KB Output is correct
52 Correct 3 ms 596 KB Output is correct
53 Correct 3 ms 596 KB Output is correct
54 Correct 3 ms 720 KB Output is correct
55 Correct 436 ms 30840 KB Output is correct
56 Correct 101 ms 43560 KB Output is correct
57 Correct 185 ms 40984 KB Output is correct
58 Correct 298 ms 30872 KB Output is correct
59 Correct 301 ms 30644 KB Output is correct
60 Correct 384 ms 36896 KB Output is correct
61 Correct 423 ms 32044 KB Output is correct
62 Correct 320 ms 40324 KB Output is correct
63 Correct 192 ms 23160 KB Output is correct
64 Correct 363 ms 32924 KB Output is correct
65 Correct 417 ms 38696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 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 328 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 3 ms 716 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 3 ms 692 KB Output is correct
35 Correct 3 ms 724 KB Output is correct
36 Correct 4 ms 580 KB Output is correct
37 Correct 3 ms 724 KB Output is correct
38 Correct 1 ms 724 KB Output is correct
39 Correct 3 ms 724 KB Output is correct
40 Correct 2 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 3 ms 584 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 3 ms 596 KB Output is correct
46 Correct 3 ms 724 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 3 ms 636 KB Output is correct
49 Correct 3 ms 596 KB Output is correct
50 Correct 3 ms 724 KB Output is correct
51 Correct 2 ms 596 KB Output is correct
52 Correct 3 ms 596 KB Output is correct
53 Correct 3 ms 596 KB Output is correct
54 Correct 3 ms 720 KB Output is correct
55 Correct 464 ms 33112 KB Output is correct
56 Correct 113 ms 45284 KB Output is correct
57 Correct 275 ms 45924 KB Output is correct
58 Correct 294 ms 33132 KB Output is correct
59 Correct 353 ms 33136 KB Output is correct
60 Correct 292 ms 27584 KB Output is correct
61 Correct 289 ms 32376 KB Output is correct
62 Correct 408 ms 41704 KB Output is correct
63 Correct 429 ms 34200 KB Output is correct
64 Correct 300 ms 40876 KB Output is correct
65 Correct 177 ms 22964 KB Output is correct
66 Correct 265 ms 28100 KB Output is correct
67 Correct 422 ms 37156 KB Output is correct
68 Correct 422 ms 42208 KB Output is correct
69 Correct 561 ms 33368 KB Output is correct
70 Correct 104 ms 45616 KB Output is correct
71 Correct 327 ms 46912 KB Output is correct
72 Correct 339 ms 33500 KB Output is correct
73 Correct 509 ms 33500 KB Output is correct
74 Correct 338 ms 32908 KB Output is correct
75 Correct 359 ms 42200 KB Output is correct
76 Correct 435 ms 31836 KB Output is correct
77 Correct 417 ms 38848 KB Output is correct
78 Correct 404 ms 42492 KB Output is correct
79 Correct 185 ms 23232 KB Output is correct
80 Correct 349 ms 30888 KB Output is correct
81 Correct 396 ms 37548 KB Output is correct
82 Correct 358 ms 42488 KB Output is correct
83 Correct 436 ms 30840 KB Output is correct
84 Correct 101 ms 43560 KB Output is correct
85 Correct 185 ms 40984 KB Output is correct
86 Correct 298 ms 30872 KB Output is correct
87 Correct 301 ms 30644 KB Output is correct
88 Correct 384 ms 36896 KB Output is correct
89 Correct 423 ms 32044 KB Output is correct
90 Correct 320 ms 40324 KB Output is correct
91 Correct 192 ms 23160 KB Output is correct
92 Correct 363 ms 32924 KB Output is correct
93 Correct 417 ms 38696 KB Output is correct
94 Correct 463 ms 34144 KB Output is correct
95 Correct 549 ms 50548 KB Output is correct
96 Correct 227 ms 44284 KB Output is correct
97 Correct 237 ms 34108 KB Output is correct
98 Correct 315 ms 34140 KB Output is correct
99 Correct 269 ms 29056 KB Output is correct
100 Correct 305 ms 43356 KB Output is correct
101 Correct 477 ms 32284 KB Output is correct
102 Correct 401 ms 39668 KB Output is correct
103 Correct 370 ms 45016 KB Output is correct
104 Correct 232 ms 26492 KB Output is correct
105 Correct 350 ms 36152 KB Output is correct
106 Correct 484 ms 41840 KB Output is correct
107 Correct 194 ms 46964 KB Output is correct
108 Correct 464 ms 33868 KB Output is correct
109 Correct 491 ms 33900 KB Output is correct
110 Correct 459 ms 33904 KB Output is correct
111 Correct 519 ms 33868 KB Output is correct
112 Correct 520 ms 33892 KB Output is correct
113 Correct 524 ms 33876 KB Output is correct
114 Correct 466 ms 33892 KB Output is correct
115 Correct 472 ms 33884 KB Output is correct
116 Correct 472 ms 33908 KB Output is correct