Submission #1049735

# Submission time Handle Problem Language Result Execution time Memory
1049735 2024-08-09T04:33:03 Z shiomusubi496 Joker (BOI20_joker) C++17
100 / 100
1923 ms 21184 KB
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)

using namespace std;

using ll = long long;

template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }

class UndoableUF {
    bool flag = true;
    vector<int> par, wei;
    vector<tuple<int, int, int, bool>> history;

public:
    UndoableUF(int n_) : par(n_, -1), wei(n_, 0) {}
    int find(int x) { return par[x] < 0 ? x : find(par[x]); }
    bool same(int x, int y) { return find(x) == find(y); }
    int weight(int x) { return par[x] < 0 ? wei[x] : wei[x] ^ weight(par[x]); }
    pair<int, int> weifin(int x) {
        if (par[x] < 0) return {x, 0};
        auto [r, w] = weifin(par[x]);
        return {r, w ^ wei[x]};
    }
    bool merge(int x, int y) {
        auto [xr, xw] = weifin(x);
        auto [yr, yw] = weifin(y);
        x = xr, y = yr;
        int w = xw ^ yw;
        if (x == y) {
            history.emplace_back(-1, -1, -1, flag);
            history.emplace_back(-1, -1, -1, flag);
            if (w == 0) flag = false;
            return false;
        }
        history.emplace_back(x, par[x], wei[x], flag);
        history.emplace_back(y, par[y], wei[y], flag);
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        wei[y] = 1 ^ w;
        return true;
    }
    int find2(int x) {
        if (par[x] < 0) return x;
        int r = find2(par[x]);
        // history.emplace_back(x, par[x], wei[x], flag);
        wei[x] ^= wei[par[x]];
        return par[x] = r;
    }
    bool same2(int x, int y) { return find2(x) == find2(y); }
    bool merge2(int x, int y) {
        int xr = find2(x), yr = find2(y);
        int w = wei[x] ^ wei[y];
        x = xr, y = yr;
        // history.emplace_back(x, par[x], wei[x], flag);
        // history.emplace_back(y, par[y], wei[y], flag);
        if (x == y) {
            if (w == 0) flag = false;
            return false;
        }
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        wei[y] = 1 ^ w;
        return true;
    }
    void undo() {
        auto [x, px, wx, _] = history.back(); history.pop_back();
        auto [y, py, wy, f] = history.back(); history.pop_back();
        if (x == -1) {
            flag = f;
        }
        else {
            par[x] = px;
            par[y] = py;
            wei[x] = wx;
            wei[y] = wy;
        }
    }
    void snapshot() { history.clear(); }
    void rollback() {
        while (!history.empty()) {
            auto [x, px, wx, f] = history.back(); history.pop_back();
            if (x == -1) flag = f;
            else {
                par[x] = px;
                wei[x] = wx;
            }
        }
    }
    bool is_bip() { return flag; }
};

int main() {
    int N, M, Q; cin >> N >> M >> Q;
    vector<pair<int, int>> A(M);
    rep (i, M) {
        cin >> A[i].first >> A[i].second;
        --A[i].first, --A[i].second;
    }
    vector<array<int, 3>> B(Q);
    rep (i, Q) {
        cin >> B[i][0] >> B[i][1];
        --B[i][0];
        B[i][2] = i;
    }
    static constexpr int block = 500;
    sort(all(B), [&](auto a, auto b) {
        if (a[0] / block != b[0] / block) return a[0] < b[0];
        return a[1] > b[1];
    });
    vector<bool> ans(Q);
    int curl = block, curr = M;
    UndoableUF uf(N);
    bool f = false;
    for (auto [l, r, k] : B) {
        if (f) {
            ans[k] = false;
            continue;
        }
        while (curl <= l) {
            uf.rollback();
            rep2 (i, curl - block, curl) {
                if (i >= M) break;
                uf.merge2(A[i].first, A[i].second);
            }
            uf.snapshot();
            if (!uf.is_bip()) {
                ans[k] = false;
                f = true;
                break;
            }
            curl += block;
            curr = M;
        }
        if (f) continue;
        assert(curr >= r);
        rrep2 (i, r, curr) {
            uf.merge(A[i].first, A[i].second);
        }
        curr = r;
        rep2 (i, curl - block, l) {
            uf.merge(A[i].first, A[i].second);
        }
        ans[k] = uf.is_bip();
        rep2 (i, curl - block, l) uf.undo();
    }
    rep (i, Q) puts(ans[i] ? "NO" : "YES");
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 436 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 436 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 6 ms 348 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 5 ms 612 KB Output is correct
32 Correct 5 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 4 ms 604 KB Output is correct
35 Correct 5 ms 604 KB Output is correct
36 Correct 4 ms 600 KB Output is correct
37 Correct 6 ms 604 KB Output is correct
38 Correct 7 ms 604 KB Output is correct
39 Correct 6 ms 608 KB Output is correct
40 Correct 4 ms 532 KB Output is correct
41 Correct 5 ms 604 KB Output is correct
42 Correct 4 ms 604 KB Output is correct
43 Correct 5 ms 604 KB Output is correct
44 Correct 5 ms 604 KB Output is correct
45 Correct 5 ms 604 KB Output is correct
46 Correct 5 ms 604 KB Output is correct
47 Correct 5 ms 604 KB Output is correct
48 Correct 6 ms 600 KB Output is correct
49 Correct 6 ms 600 KB Output is correct
50 Correct 5 ms 604 KB Output is correct
51 Correct 5 ms 604 KB Output is correct
52 Correct 5 ms 604 KB Output is correct
53 Correct 5 ms 604 KB Output is correct
54 Correct 5 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 117 ms 14524 KB Output is correct
4 Correct 106 ms 14780 KB Output is correct
5 Correct 119 ms 14268 KB Output is correct
6 Correct 132 ms 13628 KB Output is correct
7 Correct 119 ms 13504 KB Output is correct
8 Correct 109 ms 14388 KB Output is correct
9 Correct 104 ms 13760 KB Output is correct
10 Correct 109 ms 15036 KB Output is correct
11 Correct 123 ms 14908 KB Output is correct
12 Correct 116 ms 15804 KB Output is correct
13 Correct 112 ms 13636 KB Output is correct
14 Correct 118 ms 15040 KB Output is correct
15 Correct 117 ms 14908 KB Output is correct
16 Correct 124 ms 14272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 436 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 117 ms 14524 KB Output is correct
30 Correct 106 ms 14780 KB Output is correct
31 Correct 119 ms 14268 KB Output is correct
32 Correct 132 ms 13628 KB Output is correct
33 Correct 119 ms 13504 KB Output is correct
34 Correct 109 ms 14388 KB Output is correct
35 Correct 104 ms 13760 KB Output is correct
36 Correct 109 ms 15036 KB Output is correct
37 Correct 123 ms 14908 KB Output is correct
38 Correct 116 ms 15804 KB Output is correct
39 Correct 112 ms 13636 KB Output is correct
40 Correct 118 ms 15040 KB Output is correct
41 Correct 117 ms 14908 KB Output is correct
42 Correct 124 ms 14272 KB Output is correct
43 Correct 340 ms 15296 KB Output is correct
44 Correct 344 ms 15160 KB Output is correct
45 Correct 336 ms 16220 KB Output is correct
46 Correct 329 ms 13612 KB Output is correct
47 Correct 371 ms 14396 KB Output is correct
48 Correct 419 ms 14524 KB Output is correct
49 Correct 435 ms 14520 KB Output is correct
50 Correct 319 ms 15200 KB Output is correct
51 Correct 351 ms 14264 KB Output is correct
52 Correct 358 ms 16192 KB Output is correct
53 Correct 350 ms 12840 KB Output is correct
54 Correct 434 ms 14424 KB Output is correct
55 Correct 434 ms 14008 KB Output is correct
56 Correct 451 ms 15800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 436 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 6 ms 348 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 5 ms 612 KB Output is correct
32 Correct 5 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 4 ms 604 KB Output is correct
35 Correct 5 ms 604 KB Output is correct
36 Correct 4 ms 600 KB Output is correct
37 Correct 6 ms 604 KB Output is correct
38 Correct 7 ms 604 KB Output is correct
39 Correct 6 ms 608 KB Output is correct
40 Correct 4 ms 532 KB Output is correct
41 Correct 5 ms 604 KB Output is correct
42 Correct 4 ms 604 KB Output is correct
43 Correct 5 ms 604 KB Output is correct
44 Correct 5 ms 604 KB Output is correct
45 Correct 5 ms 604 KB Output is correct
46 Correct 5 ms 604 KB Output is correct
47 Correct 5 ms 604 KB Output is correct
48 Correct 6 ms 600 KB Output is correct
49 Correct 6 ms 600 KB Output is correct
50 Correct 5 ms 604 KB Output is correct
51 Correct 5 ms 604 KB Output is correct
52 Correct 5 ms 604 KB Output is correct
53 Correct 5 ms 604 KB Output is correct
54 Correct 5 ms 604 KB Output is correct
55 Correct 378 ms 12992 KB Output is correct
56 Correct 1054 ms 12736 KB Output is correct
57 Correct 597 ms 13760 KB Output is correct
58 Correct 509 ms 12476 KB Output is correct
59 Correct 1172 ms 12480 KB Output is correct
60 Correct 1308 ms 11708 KB Output is correct
61 Correct 515 ms 12224 KB Output is correct
62 Correct 621 ms 12480 KB Output is correct
63 Correct 465 ms 10812 KB Output is correct
64 Correct 667 ms 11964 KB Output is correct
65 Correct 710 ms 12220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 436 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 6 ms 348 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 5 ms 612 KB Output is correct
32 Correct 5 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 4 ms 604 KB Output is correct
35 Correct 5 ms 604 KB Output is correct
36 Correct 4 ms 600 KB Output is correct
37 Correct 6 ms 604 KB Output is correct
38 Correct 7 ms 604 KB Output is correct
39 Correct 6 ms 608 KB Output is correct
40 Correct 4 ms 532 KB Output is correct
41 Correct 5 ms 604 KB Output is correct
42 Correct 4 ms 604 KB Output is correct
43 Correct 5 ms 604 KB Output is correct
44 Correct 5 ms 604 KB Output is correct
45 Correct 5 ms 604 KB Output is correct
46 Correct 5 ms 604 KB Output is correct
47 Correct 5 ms 604 KB Output is correct
48 Correct 6 ms 600 KB Output is correct
49 Correct 6 ms 600 KB Output is correct
50 Correct 5 ms 604 KB Output is correct
51 Correct 5 ms 604 KB Output is correct
52 Correct 5 ms 604 KB Output is correct
53 Correct 5 ms 604 KB Output is correct
54 Correct 5 ms 604 KB Output is correct
55 Correct 117 ms 14524 KB Output is correct
56 Correct 106 ms 14780 KB Output is correct
57 Correct 119 ms 14268 KB Output is correct
58 Correct 132 ms 13628 KB Output is correct
59 Correct 119 ms 13504 KB Output is correct
60 Correct 109 ms 14388 KB Output is correct
61 Correct 104 ms 13760 KB Output is correct
62 Correct 109 ms 15036 KB Output is correct
63 Correct 123 ms 14908 KB Output is correct
64 Correct 116 ms 15804 KB Output is correct
65 Correct 112 ms 13636 KB Output is correct
66 Correct 118 ms 15040 KB Output is correct
67 Correct 117 ms 14908 KB Output is correct
68 Correct 124 ms 14272 KB Output is correct
69 Correct 340 ms 15296 KB Output is correct
70 Correct 344 ms 15160 KB Output is correct
71 Correct 336 ms 16220 KB Output is correct
72 Correct 329 ms 13612 KB Output is correct
73 Correct 371 ms 14396 KB Output is correct
74 Correct 419 ms 14524 KB Output is correct
75 Correct 435 ms 14520 KB Output is correct
76 Correct 319 ms 15200 KB Output is correct
77 Correct 351 ms 14264 KB Output is correct
78 Correct 358 ms 16192 KB Output is correct
79 Correct 350 ms 12840 KB Output is correct
80 Correct 434 ms 14424 KB Output is correct
81 Correct 434 ms 14008 KB Output is correct
82 Correct 451 ms 15800 KB Output is correct
83 Correct 378 ms 12992 KB Output is correct
84 Correct 1054 ms 12736 KB Output is correct
85 Correct 597 ms 13760 KB Output is correct
86 Correct 509 ms 12476 KB Output is correct
87 Correct 1172 ms 12480 KB Output is correct
88 Correct 1308 ms 11708 KB Output is correct
89 Correct 515 ms 12224 KB Output is correct
90 Correct 621 ms 12480 KB Output is correct
91 Correct 465 ms 10812 KB Output is correct
92 Correct 667 ms 11964 KB Output is correct
93 Correct 710 ms 12220 KB Output is correct
94 Correct 930 ms 14272 KB Output is correct
95 Correct 1768 ms 15548 KB Output is correct
96 Correct 1812 ms 15148 KB Output is correct
97 Correct 1332 ms 13500 KB Output is correct
98 Correct 1382 ms 14012 KB Output is correct
99 Correct 1393 ms 14016 KB Output is correct
100 Correct 1923 ms 15036 KB Output is correct
101 Correct 1240 ms 19648 KB Output is correct
102 Correct 1432 ms 20672 KB Output is correct
103 Correct 1522 ms 21184 KB Output is correct
104 Correct 1158 ms 18620 KB Output is correct
105 Correct 1773 ms 20000 KB Output is correct
106 Correct 1878 ms 20412 KB Output is correct
107 Correct 1024 ms 11696 KB Output is correct
108 Correct 1056 ms 14512 KB Output is correct
109 Correct 1044 ms 14528 KB Output is correct
110 Correct 1042 ms 14768 KB Output is correct
111 Correct 1076 ms 14128 KB Output is correct
112 Correct 1102 ms 14288 KB Output is correct
113 Correct 1018 ms 14668 KB Output is correct
114 Correct 994 ms 15524 KB Output is correct
115 Correct 1016 ms 15280 KB Output is correct
116 Correct 964 ms 15048 KB Output is correct