답안 #973436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973436 2024-05-02T02:25:34 Z cot7302 Joker (BOI20_joker) C++17
0 / 100
181 ms 10952 KB
#include <bits/stdc++.h>
using namespace std;

struct disjoint_set {
    int n;
    vector<int> _;
    vector<tuple<int, int, int>> op;
    vector<bool> not_bipartite;
    disjoint_set(int n) : n(n), _(2 * n + 1, -1), op(), not_bipartite() {}

    int root(int x) { while (_[x] > 0) x = _[x]; return x; }

    void __join(int a, int b) {
        a = root(a), b = root(b);
        if (a == b) return;
        if (_[a] > _[b]) swap(a, b);
        op.emplace_back(a, b, _[b]);
        _[a] += _[b];
        _[b] = a;
    }

    void join(int a, int b) {
        __join(a, b + n);
        __join(a + n, b);
        not_bipartite.emplace_back(root(a) == root(b));
    }

    void join(pair<int, int> x) {
        auto [a, b] = x;
        join(a, b);
    }

    bool is_bipartite() const {
        return not_bipartite.empty() || !not_bipartite.back();
    }

    void roll_back(int cnta, int cntb) {
        while (cnta < (int)size(op)) {
            auto [a, b, sz] = op.back();
            _[a] -= sz;
            _[b] = sz;
            op.pop_back();
        }
        while (cntb < (int)size(not_bipartite)) not_bipartite.pop_back();
    }

    void roll_back(pair<int, int> x) {
        roll_back(x.first, x.second);
    }

    pair<int, int> snapshot() const {
        return {(int)size(op), (int)size(not_bipartite)};
    }
};

pair<int, int> edges[200001];

int ans[200001];

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m, q; cin >> n >> m >> q;
    for (int i = 1; i <= m; i++) cin >> edges[i].first >> edges[i].second;
    disjoint_set dsu(n);
    for (int i = 1; i <= m; i++) dsu.join(edges[i]);
    if (dsu.is_bipartite()) {
        while (q--) cout << "NO\n";
        return 0;
    }
    dsu.roll_back(0, 0);
    auto solve = [&](auto&& f, int l, int r, int ql, int qr) -> void {
        if (l > r) return;
        int mid = (l + r) >> 1;
        auto snap = dsu.snapshot();
        for (int i = l; i < mid; i++) dsu.join(edges[i]);
        int opt = qr;
        while (dsu.is_bipartite()) dsu.join(edges[opt--]);
        ans[mid] = opt;
        dsu.roll_back(snap);
        for (int i = opt + 1; i <= qr; i++) dsu.join(edges[i]);
        f(f, l, mid - 1, ql, opt);
        dsu.roll_back(snap);
        for (int i = l; i <= mid; i++) dsu.join(edges[i]);
        f(f, mid + 1, r, opt, qr);
        dsu.roll_back(snap);
    };

    solve(solve, 1, m, 0, m);
    //for (int i = 0; i < m; i++) cout << ans[i] << " \n"[i == m - 1];
    for (int l, r; q--; ) {
        cin >> l >> r;
        cout << ((r <= ans[l]) ? "YES\n" : "NO\n");
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 3 ms 2396 KB Output is correct
4 Correct 1 ms 2424 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Incorrect 2 ms 2396 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 3 ms 2396 KB Output is correct
4 Correct 1 ms 2424 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Incorrect 2 ms 2396 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 181 ms 7124 KB Output is correct
4 Correct 44 ms 10952 KB Output is correct
5 Incorrect 153 ms 10952 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 3 ms 2396 KB Output is correct
4 Correct 1 ms 2424 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Incorrect 2 ms 2396 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 3 ms 2396 KB Output is correct
4 Correct 1 ms 2424 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Incorrect 2 ms 2396 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 3 ms 2396 KB Output is correct
4 Correct 1 ms 2424 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Incorrect 2 ms 2396 KB Output isn't correct
9 Halted 0 ms 0 KB -