Submission #768677

# Submission time Handle Problem Language Result Execution time Memory
768677 2023-06-28T11:29:15 Z jakobrs Joker (BOI20_joker) C++17
71 / 100
2000 ms 15504 KB
#include <algorithm>
#include <iostream>
#include <vector>

using i64 = int64_t;
using i32 = int32_t;

struct Operation {
    i32 l1, sizes_l1, r1, sizes_r1;
    bool is_bipartite;
};

constexpr i64 root(i64 n) {
    i64 i = 0;
    for (; i * i <= n; i++)
        ;
    return i - 1;
}

class UnionFind {
    std::vector<i32> parent;
    std::vector<i32> sizes;
    std::vector<bool> edge_parity;

    std::vector<Operation> operations;

   public:
    bool is_bipartite = true;

    UnionFind(size_t sz)
        : parent(sz, -1), sizes(sz, 1), edge_parity(sz, false), operations() {}

    template <bool PATH_COMPRESSION>
    i32 find(i32 idx) {
        if (parent[idx] < 0)
            return idx;
        else if constexpr (PATH_COMPRESSION) {
            i64 root = find<PATH_COMPRESSION>(parent[idx]);
            edge_parity[idx] = edge_parity[idx] ^ edge_parity[parent[idx]];
            return parent[idx] = root;
        } else
            return find<PATH_COMPRESSION>(parent[idx]);
    }

    bool parity(i32 idx) {
        if (parent[idx] < 0)
            return false;
        else
            return parity(parent[idx]) ^ edge_parity[idx];
    }

    template <bool PATH_COMPRESSION>
    std::pair<i32, bool> both(i32 idx) {
        if constexpr (PATH_COMPRESSION) {
            return {find<true>(idx), parity(idx)};
        } else {
            // in this case there's an easy iterative version
            bool parity = false;

            while (parent[idx] >= 0) {
                parity = parity ^ edge_parity[idx];
                idx = parent[idx];
            }

            return {idx, parity};
        }
    }

    template <bool PATH_COMPRESSION>
    bool lag_fagforening(i32 l, i32 r) {
        auto [l1, lp] = both<PATH_COMPRESSION>(l);
        auto [r1, rp] = both<PATH_COMPRESSION>(r);

        operations.push_back({
            .l1 = l1,
            .sizes_l1 = sizes[l1],
            .r1 = r1,
            .sizes_r1 = sizes[r1],
            .is_bipartite = is_bipartite,
        });

        if (l1 == r1) {
            return !(is_bipartite &= lp != rp);
        } else {
            if (sizes[l1] < sizes[r1]) {
                std::swap(l1, r1);
                std::swap(lp, rp);
            }

            sizes[l1] += sizes[r1];
            parent[r1] = l1;

            edge_parity[r1] = 1 ^ lp ^ rp;

            return false;
        }
    }

    void tvungen_lønnsnemnd() {
        auto [l1, sizes_l1, r1, sizes_r1, bpt] = operations.back();
        operations.pop_back();

        sizes[l1] = sizes_l1;
        sizes[r1] = sizes_r1;

        parent[l1] = -1;
        parent[r1] = -1;

        edge_parity[l1] = edge_parity[r1] = false;

        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"

main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    i32 n, m, q;
    std::cin >> n >> m >> q;

    std::vector<std::pair<i32, i32>> edges(m);
    for (i32 i = 0; i < m; i++) {
        edges[i] = {(i32)in - 1, (i32)in - 1};
    }

    UnionFind dsu(n);

    static constexpr i32 root_m = 1024;

    struct Query {
        i32 l, r, i;
    };

    std::vector<Query> queries(q);
    for (i32 i = 0; i < q; i++) queries[i] = {(i32)in - 1, (i32)in, i};

    std::sort(entire(queries), [](auto &&lhs, auto &&rhs) {
        return std::pair<i32, i32>{lhs.l / root_m, -lhs.r} <
               std::pair<i32, i32>{rhs.l / root_m, -rhs.r};
    });

    std::vector<bool> answers(q);

    i32 l = 0;
    i32 r = 0;

    for (i32 start = 0; start < m; start += root_m) {
        l = r;
        while (r < q && queries[r].l < start + root_m) r++;

        i32 w = m;
        for (i32 i = l; i < r; i++) {
            for (i32 o = queries[i].r; o < w; o++)
                dsu.lag_fagforening<false>(edges[o].first, edges[o].second);
            w = queries[i].r;

            for (i32 g = start; g < queries[i].l; g++)
                dsu.lag_fagforening<false>(edges[g].first, edges[g].second);

            answers[queries[i].i] = dsu.is_bipartite;

            for (i32 g = start; g < queries[i].l; g++) dsu.tvungen_lønnsnemnd();
        }

        for (i32 x = w; x < m; x++) dsu.tvungen_lønnsnemnd();

        for (i32 y = start; y < start + root_m && y < m; y++)
            dsu.lag_fagforening<true>(edges[y].first, edges[y].second);
    }

    for (auto x : answers) {
        std::cout << (x ? "NO\n" : "YES\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 1 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 212 KB Output is correct
13 Correct 1 ms 212 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 212 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 340 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 340 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 1 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 212 KB Output is correct
13 Correct 1 ms 212 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 212 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 340 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 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 15 ms 340 KB Output is correct
30 Correct 15 ms 444 KB Output is correct
31 Correct 17 ms 456 KB Output is correct
32 Correct 18 ms 340 KB Output is correct
33 Correct 20 ms 468 KB Output is correct
34 Correct 17 ms 340 KB Output is correct
35 Correct 17 ms 460 KB Output is correct
36 Correct 17 ms 340 KB Output is correct
37 Correct 17 ms 464 KB Output is correct
38 Correct 16 ms 456 KB Output is correct
39 Correct 16 ms 452 KB Output is correct
40 Correct 17 ms 444 KB Output is correct
41 Correct 19 ms 440 KB Output is correct
42 Correct 17 ms 340 KB Output is correct
43 Correct 18 ms 444 KB Output is correct
44 Correct 18 ms 452 KB Output is correct
45 Correct 18 ms 340 KB Output is correct
46 Correct 18 ms 452 KB Output is correct
47 Correct 18 ms 424 KB Output is correct
48 Correct 20 ms 444 KB Output is correct
49 Correct 17 ms 468 KB Output is correct
50 Correct 17 ms 340 KB Output is correct
51 Correct 20 ms 440 KB Output is correct
52 Correct 17 ms 340 KB Output is correct
53 Correct 20 ms 436 KB Output is correct
54 Correct 20 ms 452 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 83 ms 10264 KB Output is correct
4 Correct 71 ms 11096 KB Output is correct
5 Correct 89 ms 10952 KB Output is correct
6 Correct 73 ms 10204 KB Output is correct
7 Correct 76 ms 10260 KB Output is correct
8 Correct 73 ms 9936 KB Output is correct
9 Correct 75 ms 10268 KB Output is correct
10 Correct 82 ms 11060 KB Output is correct
11 Correct 75 ms 10264 KB Output is correct
12 Correct 76 ms 10832 KB Output is correct
13 Correct 71 ms 9620 KB Output is correct
14 Correct 73 ms 9940 KB Output is correct
15 Correct 85 ms 10568 KB Output is correct
16 Correct 93 ms 11056 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 1 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 212 KB Output is correct
13 Correct 1 ms 212 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 212 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 340 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 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 83 ms 10264 KB Output is correct
30 Correct 71 ms 11096 KB Output is correct
31 Correct 89 ms 10952 KB Output is correct
32 Correct 73 ms 10204 KB Output is correct
33 Correct 76 ms 10260 KB Output is correct
34 Correct 73 ms 9936 KB Output is correct
35 Correct 75 ms 10268 KB Output is correct
36 Correct 82 ms 11060 KB Output is correct
37 Correct 75 ms 10264 KB Output is correct
38 Correct 76 ms 10832 KB Output is correct
39 Correct 71 ms 9620 KB Output is correct
40 Correct 73 ms 9940 KB Output is correct
41 Correct 85 ms 10568 KB Output is correct
42 Correct 93 ms 11056 KB Output is correct
43 Correct 380 ms 10260 KB Output is correct
44 Correct 382 ms 11028 KB Output is correct
45 Correct 397 ms 11068 KB Output is correct
46 Correct 399 ms 10224 KB Output is correct
47 Correct 437 ms 14608 KB Output is correct
48 Correct 508 ms 14056 KB Output is correct
49 Correct 486 ms 15136 KB Output is correct
50 Correct 404 ms 14336 KB Output is correct
51 Correct 409 ms 14952 KB Output is correct
52 Correct 410 ms 15400 KB Output is correct
53 Correct 463 ms 13772 KB Output is correct
54 Correct 450 ms 14392 KB Output is correct
55 Correct 449 ms 15052 KB Output is correct
56 Correct 466 ms 15504 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 1 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 212 KB Output is correct
13 Correct 1 ms 212 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 212 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 340 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 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 15 ms 340 KB Output is correct
30 Correct 15 ms 444 KB Output is correct
31 Correct 17 ms 456 KB Output is correct
32 Correct 18 ms 340 KB Output is correct
33 Correct 20 ms 468 KB Output is correct
34 Correct 17 ms 340 KB Output is correct
35 Correct 17 ms 460 KB Output is correct
36 Correct 17 ms 340 KB Output is correct
37 Correct 17 ms 464 KB Output is correct
38 Correct 16 ms 456 KB Output is correct
39 Correct 16 ms 452 KB Output is correct
40 Correct 17 ms 444 KB Output is correct
41 Correct 19 ms 440 KB Output is correct
42 Correct 17 ms 340 KB Output is correct
43 Correct 18 ms 444 KB Output is correct
44 Correct 18 ms 452 KB Output is correct
45 Correct 18 ms 340 KB Output is correct
46 Correct 18 ms 452 KB Output is correct
47 Correct 18 ms 424 KB Output is correct
48 Correct 20 ms 444 KB Output is correct
49 Correct 17 ms 468 KB Output is correct
50 Correct 17 ms 340 KB Output is correct
51 Correct 20 ms 440 KB Output is correct
52 Correct 17 ms 340 KB Output is correct
53 Correct 20 ms 436 KB Output is correct
54 Correct 20 ms 452 KB Output is correct
55 Correct 311 ms 7868 KB Output is correct
56 Correct 516 ms 11276 KB Output is correct
57 Correct 395 ms 11068 KB Output is correct
58 Correct 431 ms 10292 KB Output is correct
59 Correct 682 ms 10296 KB Output is correct
60 Correct 814 ms 10816 KB Output is correct
61 Correct 468 ms 10308 KB Output is correct
62 Correct 548 ms 11048 KB Output is correct
63 Correct 383 ms 9656 KB Output is correct
64 Correct 545 ms 10512 KB Output is correct
65 Correct 636 ms 11052 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 1 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 212 KB Output is correct
13 Correct 1 ms 212 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 212 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 340 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 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 15 ms 340 KB Output is correct
30 Correct 15 ms 444 KB Output is correct
31 Correct 17 ms 456 KB Output is correct
32 Correct 18 ms 340 KB Output is correct
33 Correct 20 ms 468 KB Output is correct
34 Correct 17 ms 340 KB Output is correct
35 Correct 17 ms 460 KB Output is correct
36 Correct 17 ms 340 KB Output is correct
37 Correct 17 ms 464 KB Output is correct
38 Correct 16 ms 456 KB Output is correct
39 Correct 16 ms 452 KB Output is correct
40 Correct 17 ms 444 KB Output is correct
41 Correct 19 ms 440 KB Output is correct
42 Correct 17 ms 340 KB Output is correct
43 Correct 18 ms 444 KB Output is correct
44 Correct 18 ms 452 KB Output is correct
45 Correct 18 ms 340 KB Output is correct
46 Correct 18 ms 452 KB Output is correct
47 Correct 18 ms 424 KB Output is correct
48 Correct 20 ms 444 KB Output is correct
49 Correct 17 ms 468 KB Output is correct
50 Correct 17 ms 340 KB Output is correct
51 Correct 20 ms 440 KB Output is correct
52 Correct 17 ms 340 KB Output is correct
53 Correct 20 ms 436 KB Output is correct
54 Correct 20 ms 452 KB Output is correct
55 Correct 83 ms 10264 KB Output is correct
56 Correct 71 ms 11096 KB Output is correct
57 Correct 89 ms 10952 KB Output is correct
58 Correct 73 ms 10204 KB Output is correct
59 Correct 76 ms 10260 KB Output is correct
60 Correct 73 ms 9936 KB Output is correct
61 Correct 75 ms 10268 KB Output is correct
62 Correct 82 ms 11060 KB Output is correct
63 Correct 75 ms 10264 KB Output is correct
64 Correct 76 ms 10832 KB Output is correct
65 Correct 71 ms 9620 KB Output is correct
66 Correct 73 ms 9940 KB Output is correct
67 Correct 85 ms 10568 KB Output is correct
68 Correct 93 ms 11056 KB Output is correct
69 Correct 380 ms 10260 KB Output is correct
70 Correct 382 ms 11028 KB Output is correct
71 Correct 397 ms 11068 KB Output is correct
72 Correct 399 ms 10224 KB Output is correct
73 Correct 437 ms 14608 KB Output is correct
74 Correct 508 ms 14056 KB Output is correct
75 Correct 486 ms 15136 KB Output is correct
76 Correct 404 ms 14336 KB Output is correct
77 Correct 409 ms 14952 KB Output is correct
78 Correct 410 ms 15400 KB Output is correct
79 Correct 463 ms 13772 KB Output is correct
80 Correct 450 ms 14392 KB Output is correct
81 Correct 449 ms 15052 KB Output is correct
82 Correct 466 ms 15504 KB Output is correct
83 Correct 311 ms 7868 KB Output is correct
84 Correct 516 ms 11276 KB Output is correct
85 Correct 395 ms 11068 KB Output is correct
86 Correct 431 ms 10292 KB Output is correct
87 Correct 682 ms 10296 KB Output is correct
88 Correct 814 ms 10816 KB Output is correct
89 Correct 468 ms 10308 KB Output is correct
90 Correct 548 ms 11048 KB Output is correct
91 Correct 383 ms 9656 KB Output is correct
92 Correct 545 ms 10512 KB Output is correct
93 Correct 636 ms 11052 KB Output is correct
94 Execution timed out 2047 ms 15168 KB Time limit exceeded
95 Halted 0 ms 0 KB -