답안 #765474

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765474 2023-06-24T14:29:20 Z jakobrs Joker (BOI20_joker) C++17
49 / 100
2000 ms 20204 KB
#include <algorithm>
#include <iostream>
#include <vector>
 
using i64 = int64_t;
 
struct Operation {
    i64 l1, parent_l1, r1, parent_r1;
    bool is_bipartite;
};
 
constexpr i64 root(i64 n) {
    i64 i = 0;
    for (; i * i <= n; i++)
        ;
    return i - 1;
}
 
class UnionFind {
    std::vector<i64> parent;
    std::vector<bool> edge_parity;
 
    std::vector<Operation> operations;
 
   public:
    bool is_bipartite = true;
 
    UnionFind(size_t sz)
        : parent(sz, -1), edge_parity(sz, false), operations() {}
 
    i64 find(i64 idx) const {
        if (parent[idx] < 0)
            return idx;
        else
            return find(parent[idx]);
    }
 
    bool parity(i64 idx) {
        if (parent[idx] < 0)
            return false;
        else
            return parity(parent[idx]) ^ edge_parity[idx];
    }
 
    std::pair<i64, bool> both(i64 idx) { return {find(idx), parity(idx)}; }
 
    bool lag_fagforening(i64 l, i64 r) {
        auto [l1, lp] = both(l);
        auto [r1, rp] = both(r);
 
        operations.push_back({
            .l1 = l1,
            .parent_l1 = parent[l1],
            .r1 = r1,
            .parent_r1 = parent[r1],
            .is_bipartite = is_bipartite,
        });
 
        if (l1 == r1) {
            return !(is_bipartite &= lp != rp);
        } else {
            if (-parent[l1] < -parent[r1]) {
                std::swap(l1, r1);
                std::swap(lp, rp);
            }
 
            parent[l1] += parent[r1];
            parent[r1] = l1;
 
            edge_parity[r1] = 1 ^ lp ^ rp;
 
            return false;
        }
    }
 
    void rollback() {
        auto [l1, parent_l1, r1, parent_r1, bpt] = operations.back();
        operations.pop_back();
 
        parent[l1] = parent_l1;
        parent[r1] = parent_r1;
 
        is_bipartite = bpt;
    }
};
 
struct {
    template <typename T>
    operator T() const {
        T x;
        std::cin >> x;
        return x;
    }
} in;
 
#define entire(v) std::begin(v), std::end(v)
 
#pragma GCC diagnostic ignored "-Wreturn-type"
 
template <class T, class U>
struct SwapComparator {
    void operator()(std::pair<T, U> const &lhs, std::pair<T, U> const &rhs) {
        return std::tie(lhs.second, lhs.first) <
               std::tie(rhs.second, rhs.first);
    }
};
 
main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
 
    i64 n, m, q;
    std::cin >> n >> m >> q;
 
    std::vector<std::pair<i64, i64>> edges(m);
    for (i64 i = 0; i < m; i++) {
        edges[i] = {(i64)in - 1, (i64)in - 1};
    }
 
    UnionFind dsu(n);
 
    static constexpr i64 root_m = 450;
 
    struct Query {
        i64 l, r, i;
    };
 
    std::vector<Query> queries(q);
    for (i64 i = 0; i < q; i++) queries[i] = {(i64)in - 1, (i64)in, i};
 
    std::sort(entire(queries), [](auto &&lhs, auto &&rhs) {
        return std::pair<i64, i64>{lhs.l / root_m, -lhs.r} <
               std::pair<i64, i64>{rhs.l / root_m, -rhs.r};
    });
 
    std::vector<bool> answers(q);
 
    i64 l = 0;
    i64 r = 0;
 
    for (i64 start = 0; start < m; start += root_m) {
        l = r;
        while (r < q && queries[r].l < start + root_m) r++;
 
        i64 w = m;
        for (i64 i = l; i < r; i++) {
            for (i64 o = queries[i].r; o < w; o++)
                dsu.lag_fagforening(edges[o].first, edges[o].second);
            w = queries[i].r;
 
            for (i64 g = start; g < queries[i].l; g++)
                dsu.lag_fagforening(edges[g].first, edges[g].second);
 
            answers[queries[i].i] = dsu.is_bipartite;
 
            for (i64 g = start; g < queries[i].l; g++)
                dsu.rollback();
        }
 
        for (i64 x = w; x < m; x++)
            dsu.rollback();
 
        for (i64 y = start; y < start + root_m && y < m; y++) {
            dsu.lag_fagforening(edges[y].first, edges[y].second);
        }
    }
 
    for (auto x : answers) {
        std::cout << (x ? "NO\n" : "YES\n");
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 340 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 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 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 340 KB Output is correct
19 Correct 1 ms 340 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 276 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 340 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 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 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 340 KB Output is correct
19 Correct 1 ms 340 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 276 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 7 ms 476 KB Output is correct
30 Correct 10 ms 564 KB Output is correct
31 Correct 9 ms 572 KB Output is correct
32 Correct 9 ms 552 KB Output is correct
33 Correct 9 ms 564 KB Output is correct
34 Correct 9 ms 576 KB Output is correct
35 Correct 9 ms 468 KB Output is correct
36 Correct 8 ms 468 KB Output is correct
37 Correct 8 ms 580 KB Output is correct
38 Correct 10 ms 576 KB Output is correct
39 Correct 8 ms 468 KB Output is correct
40 Correct 9 ms 476 KB Output is correct
41 Correct 9 ms 468 KB Output is correct
42 Correct 9 ms 536 KB Output is correct
43 Correct 9 ms 568 KB Output is correct
44 Correct 9 ms 572 KB Output is correct
45 Correct 9 ms 572 KB Output is correct
46 Correct 9 ms 468 KB Output is correct
47 Correct 8 ms 468 KB Output is correct
48 Correct 8 ms 564 KB Output is correct
49 Correct 8 ms 564 KB Output is correct
50 Correct 9 ms 480 KB Output is correct
51 Correct 8 ms 564 KB Output is correct
52 Correct 9 ms 572 KB Output is correct
53 Correct 9 ms 564 KB Output is correct
54 Correct 9 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 74 ms 19304 KB Output is correct
4 Correct 77 ms 20084 KB Output is correct
5 Correct 80 ms 19952 KB Output is correct
6 Correct 80 ms 19296 KB Output is correct
7 Correct 83 ms 19308 KB Output is correct
8 Correct 82 ms 19004 KB Output is correct
9 Correct 79 ms 19312 KB Output is correct
10 Correct 90 ms 20204 KB Output is correct
11 Correct 79 ms 19288 KB Output is correct
12 Correct 82 ms 19908 KB Output is correct
13 Correct 80 ms 18672 KB Output is correct
14 Correct 82 ms 18968 KB Output is correct
15 Correct 84 ms 19644 KB Output is correct
16 Correct 86 ms 20156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 340 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 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 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 340 KB Output is correct
19 Correct 1 ms 340 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 276 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 74 ms 19304 KB Output is correct
30 Correct 77 ms 20084 KB Output is correct
31 Correct 80 ms 19952 KB Output is correct
32 Correct 80 ms 19296 KB Output is correct
33 Correct 83 ms 19308 KB Output is correct
34 Correct 82 ms 19004 KB Output is correct
35 Correct 79 ms 19312 KB Output is correct
36 Correct 90 ms 20204 KB Output is correct
37 Correct 79 ms 19288 KB Output is correct
38 Correct 82 ms 19908 KB Output is correct
39 Correct 80 ms 18672 KB Output is correct
40 Correct 82 ms 18968 KB Output is correct
41 Correct 84 ms 19644 KB Output is correct
42 Correct 86 ms 20156 KB Output is correct
43 Correct 404 ms 19348 KB Output is correct
44 Correct 419 ms 20092 KB Output is correct
45 Correct 399 ms 20084 KB Output is correct
46 Correct 448 ms 19352 KB Output is correct
47 Correct 494 ms 19332 KB Output is correct
48 Correct 557 ms 19304 KB Output is correct
49 Correct 528 ms 20092 KB Output is correct
50 Correct 424 ms 19252 KB Output is correct
51 Correct 429 ms 19660 KB Output is correct
52 Correct 426 ms 20164 KB Output is correct
53 Correct 456 ms 18664 KB Output is correct
54 Correct 493 ms 19092 KB Output is correct
55 Correct 486 ms 19612 KB Output is correct
56 Correct 477 ms 20148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 340 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 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 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 340 KB Output is correct
19 Correct 1 ms 340 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 276 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 7 ms 476 KB Output is correct
30 Correct 10 ms 564 KB Output is correct
31 Correct 9 ms 572 KB Output is correct
32 Correct 9 ms 552 KB Output is correct
33 Correct 9 ms 564 KB Output is correct
34 Correct 9 ms 576 KB Output is correct
35 Correct 9 ms 468 KB Output is correct
36 Correct 8 ms 468 KB Output is correct
37 Correct 8 ms 580 KB Output is correct
38 Correct 10 ms 576 KB Output is correct
39 Correct 8 ms 468 KB Output is correct
40 Correct 9 ms 476 KB Output is correct
41 Correct 9 ms 468 KB Output is correct
42 Correct 9 ms 536 KB Output is correct
43 Correct 9 ms 568 KB Output is correct
44 Correct 9 ms 572 KB Output is correct
45 Correct 9 ms 572 KB Output is correct
46 Correct 9 ms 468 KB Output is correct
47 Correct 8 ms 468 KB Output is correct
48 Correct 8 ms 564 KB Output is correct
49 Correct 8 ms 564 KB Output is correct
50 Correct 9 ms 480 KB Output is correct
51 Correct 8 ms 564 KB Output is correct
52 Correct 9 ms 572 KB Output is correct
53 Correct 9 ms 564 KB Output is correct
54 Correct 9 ms 468 KB Output is correct
55 Correct 499 ms 14616 KB Output is correct
56 Correct 1123 ms 15472 KB Output is correct
57 Correct 582 ms 17764 KB Output is correct
58 Correct 803 ms 16952 KB Output is correct
59 Execution timed out 2070 ms 16964 KB Time limit exceeded
60 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 340 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 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 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 340 KB Output is correct
19 Correct 1 ms 340 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 276 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 7 ms 476 KB Output is correct
30 Correct 10 ms 564 KB Output is correct
31 Correct 9 ms 572 KB Output is correct
32 Correct 9 ms 552 KB Output is correct
33 Correct 9 ms 564 KB Output is correct
34 Correct 9 ms 576 KB Output is correct
35 Correct 9 ms 468 KB Output is correct
36 Correct 8 ms 468 KB Output is correct
37 Correct 8 ms 580 KB Output is correct
38 Correct 10 ms 576 KB Output is correct
39 Correct 8 ms 468 KB Output is correct
40 Correct 9 ms 476 KB Output is correct
41 Correct 9 ms 468 KB Output is correct
42 Correct 9 ms 536 KB Output is correct
43 Correct 9 ms 568 KB Output is correct
44 Correct 9 ms 572 KB Output is correct
45 Correct 9 ms 572 KB Output is correct
46 Correct 9 ms 468 KB Output is correct
47 Correct 8 ms 468 KB Output is correct
48 Correct 8 ms 564 KB Output is correct
49 Correct 8 ms 564 KB Output is correct
50 Correct 9 ms 480 KB Output is correct
51 Correct 8 ms 564 KB Output is correct
52 Correct 9 ms 572 KB Output is correct
53 Correct 9 ms 564 KB Output is correct
54 Correct 9 ms 468 KB Output is correct
55 Correct 74 ms 19304 KB Output is correct
56 Correct 77 ms 20084 KB Output is correct
57 Correct 80 ms 19952 KB Output is correct
58 Correct 80 ms 19296 KB Output is correct
59 Correct 83 ms 19308 KB Output is correct
60 Correct 82 ms 19004 KB Output is correct
61 Correct 79 ms 19312 KB Output is correct
62 Correct 90 ms 20204 KB Output is correct
63 Correct 79 ms 19288 KB Output is correct
64 Correct 82 ms 19908 KB Output is correct
65 Correct 80 ms 18672 KB Output is correct
66 Correct 82 ms 18968 KB Output is correct
67 Correct 84 ms 19644 KB Output is correct
68 Correct 86 ms 20156 KB Output is correct
69 Correct 404 ms 19348 KB Output is correct
70 Correct 419 ms 20092 KB Output is correct
71 Correct 399 ms 20084 KB Output is correct
72 Correct 448 ms 19352 KB Output is correct
73 Correct 494 ms 19332 KB Output is correct
74 Correct 557 ms 19304 KB Output is correct
75 Correct 528 ms 20092 KB Output is correct
76 Correct 424 ms 19252 KB Output is correct
77 Correct 429 ms 19660 KB Output is correct
78 Correct 426 ms 20164 KB Output is correct
79 Correct 456 ms 18664 KB Output is correct
80 Correct 493 ms 19092 KB Output is correct
81 Correct 486 ms 19612 KB Output is correct
82 Correct 477 ms 20148 KB Output is correct
83 Correct 499 ms 14616 KB Output is correct
84 Correct 1123 ms 15472 KB Output is correct
85 Correct 582 ms 17764 KB Output is correct
86 Correct 803 ms 16952 KB Output is correct
87 Execution timed out 2070 ms 16964 KB Time limit exceeded
88 Halted 0 ms 0 KB -