Submission #768132

# Submission time Handle Problem Language Result Execution time Memory
768132 2023-06-27T13:57:06 Z adrilen Joker (BOI20_joker) C++17
71 / 100
2000 ms 19112 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
typedef array<int, 2> arr;
typedef array<int, 3> arrr;

/*
Test group size

*/

constexpr int sqrt_n = 800; // Gir feil svar, for 5.in
// 447

struct UnionFind {
    vector <int> par; // >= 0 -> ind, < 0 -> siz
    vector <int> parity;
    vector <int> siz;

    vector <arr> history;

    UnionFind(int n, int m)
    {
        par.assign(n, -1);
        parity.assign(n, 0);
        siz.assign(n, 1);
    }

    pii fnd(int p)
    {
        if (par[p] < 0) return {p, 0};

        pii o = fnd(par[p]);
        o.second ^= parity[p];
        return o;
    }

    bool uni(int a, int b) // Return whether this will be an odd cycle
    {
        int a_parity, b_parity;
        tie(a, a_parity) = fnd(a);
        tie(b, b_parity) = fnd(b);

        if (a == b)
        {
            history.emplace_back(arr{-1, -1});
            if (a_parity == b_parity)
            {
                // Odd cycle
                return true;
            } else {
                return false;
            }
        }


        if (siz[a] < siz[b]) swap(a, b);
        // a > b
        // b will be merged into b

        siz[a] += siz[b];
        parity[b] = a_parity ^ b_parity ^ 1;
        par[b] = a;

        history.emplace_back(arr{a, b});
        return false;
    }

    void undo()
    {
        arr p = history.back();
        history.pop_back();
        if (p[0] == -1) return ;

        siz[p[0]] -= siz[p[1]];
        parity[p[1]] = 0;
        par[p[1]] = -1;
    }
} ;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int n, m, q;
    cin >> n >> m >> q;

    vector<arr> edges(m);
    for (auto &i : edges) {
        cin >> i[0] >> i[1];
        i[0]--, i[1]--;
    }

    int b = 0;
    vector <arrr> queries(q);
    for (auto &i : queries)
    {
        cin >> i[0] >> i[1];
        i[0]--, i[1]--;
        i[2] = b++;
    }

    // Starting with the queries the furthest to the right
    sort(queries.begin(), queries.end(), [] (const auto &lhs, const auto &rhs)
    {
        return lhs[0] < rhs[0];
    });

    vector <int> min_start(sqrt_n, 2e9);
    vector <vector <arrr>> query_groups(sqrt_n);
    for (auto &i : queries)
    {
        query_groups[i[0] / sqrt_n].emplace_back(i);
        min_start[i[0] / sqrt_n] = min(min_start[i[0] / sqrt_n], i[0]);
    }

    for (auto &i : query_groups) sort(i.begin(), i.end(), [] (const auto &lhs, const auto &rhs) {
        return lhs[1] > rhs[1];
    });
    

    vector <int> output(q);

    // Need to have a counter of whats already added


    int c = 0;
    for (auto &y : query_groups)
    {
        if (y.empty()) break;
        UnionFind Graph(n, m);


        auto already_added = edges.begin();
        bool have_found_cycle = false, this_cycle;

        while (already_added - edges.begin() < min_start[c]) {
            // Need to check if solved
            if (Graph.uni((*already_added)[0], (*already_added)[1])) {
                have_found_cycle = true;
            }
            already_added++;
        }


        auto right_edge = edges.end() - 1;
        auto left_edge = already_added;
        for (auto &i : y)
        {
            if (have_found_cycle) {
                output[i[2]] = true;
                continue;
            }

            // The right side

            // Check if the right amount
            while (right_edge - edges.begin() > i[1]) {
                if (Graph.uni((*right_edge)[0], (*right_edge)[1])) {
                    // All queries after this will be true
                    have_found_cycle = true;
                }
                right_edge--;
            }

            // The left side
            this_cycle = false;
            while (left_edge - edges.begin() < i[0])
            {
                if (Graph.uni((*left_edge)[0], (*left_edge)[1])) {
                    this_cycle = true;
                    left_edge++;
                    break;
                }
                left_edge++;
            }
            
            output[i[2]] = (this_cycle || have_found_cycle);

            // Remove last edges
            while (left_edge != already_added)
            {
                Graph.undo();
                left_edge--;
            }

        }
        c++;
    }

    for (auto &i : output) cout << (i ? "YES" : "NO") << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 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 320 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 320 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 316 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 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 320 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 320 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 316 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 9 ms 468 KB Output is correct
30 Correct 6 ms 516 KB Output is correct
31 Correct 5 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 3 ms 364 KB Output is correct
34 Correct 4 ms 496 KB Output is correct
35 Correct 6 ms 468 KB Output is correct
36 Correct 6 ms 468 KB Output is correct
37 Correct 9 ms 516 KB Output is correct
38 Correct 11 ms 512 KB Output is correct
39 Correct 8 ms 460 KB Output is correct
40 Correct 5 ms 468 KB Output is correct
41 Correct 5 ms 468 KB Output is correct
42 Correct 5 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 2 ms 596 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 5 ms 468 KB Output is correct
48 Correct 6 ms 468 KB Output is correct
49 Correct 6 ms 468 KB Output is correct
50 Correct 6 ms 468 KB Output is correct
51 Correct 5 ms 468 KB Output is correct
52 Correct 5 ms 468 KB Output is correct
53 Correct 6 ms 468 KB Output is correct
54 Correct 6 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 69 ms 14948 KB Output is correct
4 Correct 73 ms 16824 KB Output is correct
5 Correct 68 ms 14772 KB Output is correct
6 Correct 69 ms 14912 KB Output is correct
7 Correct 70 ms 14916 KB Output is correct
8 Correct 66 ms 13888 KB Output is correct
9 Correct 65 ms 14392 KB Output is correct
10 Correct 69 ms 15808 KB Output is correct
11 Correct 67 ms 14916 KB Output is correct
12 Correct 71 ms 16048 KB Output is correct
13 Correct 69 ms 13792 KB Output is correct
14 Correct 71 ms 14460 KB Output is correct
15 Correct 69 ms 15420 KB Output is correct
16 Correct 71 ms 16192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 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 320 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 320 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 316 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 69 ms 14948 KB Output is correct
30 Correct 73 ms 16824 KB Output is correct
31 Correct 68 ms 14772 KB Output is correct
32 Correct 69 ms 14912 KB Output is correct
33 Correct 70 ms 14916 KB Output is correct
34 Correct 66 ms 13888 KB Output is correct
35 Correct 65 ms 14392 KB Output is correct
36 Correct 69 ms 15808 KB Output is correct
37 Correct 67 ms 14916 KB Output is correct
38 Correct 71 ms 16048 KB Output is correct
39 Correct 69 ms 13792 KB Output is correct
40 Correct 71 ms 14460 KB Output is correct
41 Correct 69 ms 15420 KB Output is correct
42 Correct 71 ms 16192 KB Output is correct
43 Correct 299 ms 15200 KB Output is correct
44 Correct 341 ms 17488 KB Output is correct
45 Correct 335 ms 17592 KB Output is correct
46 Correct 263 ms 15228 KB Output is correct
47 Correct 257 ms 15332 KB Output is correct
48 Correct 98 ms 14948 KB Output is correct
49 Correct 119 ms 16312 KB Output is correct
50 Correct 246 ms 14912 KB Output is correct
51 Correct 273 ms 15804 KB Output is correct
52 Correct 310 ms 16440 KB Output is correct
53 Correct 230 ms 14132 KB Output is correct
54 Correct 306 ms 15036 KB Output is correct
55 Correct 348 ms 15876 KB Output is correct
56 Correct 332 ms 16488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 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 320 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 320 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 316 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 9 ms 468 KB Output is correct
30 Correct 6 ms 516 KB Output is correct
31 Correct 5 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 3 ms 364 KB Output is correct
34 Correct 4 ms 496 KB Output is correct
35 Correct 6 ms 468 KB Output is correct
36 Correct 6 ms 468 KB Output is correct
37 Correct 9 ms 516 KB Output is correct
38 Correct 11 ms 512 KB Output is correct
39 Correct 8 ms 460 KB Output is correct
40 Correct 5 ms 468 KB Output is correct
41 Correct 5 ms 468 KB Output is correct
42 Correct 5 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 2 ms 596 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 5 ms 468 KB Output is correct
48 Correct 6 ms 468 KB Output is correct
49 Correct 6 ms 468 KB Output is correct
50 Correct 6 ms 468 KB Output is correct
51 Correct 5 ms 468 KB Output is correct
52 Correct 5 ms 468 KB Output is correct
53 Correct 6 ms 468 KB Output is correct
54 Correct 6 ms 460 KB Output is correct
55 Correct 672 ms 9136 KB Output is correct
56 Correct 59 ms 10520 KB Output is correct
57 Correct 882 ms 10392 KB Output is correct
58 Correct 845 ms 9184 KB Output is correct
59 Correct 1195 ms 9256 KB Output is correct
60 Correct 1232 ms 10044 KB Output is correct
61 Correct 845 ms 9172 KB Output is correct
62 Correct 1098 ms 10220 KB Output is correct
63 Correct 982 ms 8348 KB Output is correct
64 Correct 1192 ms 9572 KB Output is correct
65 Correct 1266 ms 10296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 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 320 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 320 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 316 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 9 ms 468 KB Output is correct
30 Correct 6 ms 516 KB Output is correct
31 Correct 5 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 3 ms 364 KB Output is correct
34 Correct 4 ms 496 KB Output is correct
35 Correct 6 ms 468 KB Output is correct
36 Correct 6 ms 468 KB Output is correct
37 Correct 9 ms 516 KB Output is correct
38 Correct 11 ms 512 KB Output is correct
39 Correct 8 ms 460 KB Output is correct
40 Correct 5 ms 468 KB Output is correct
41 Correct 5 ms 468 KB Output is correct
42 Correct 5 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 2 ms 596 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 5 ms 468 KB Output is correct
48 Correct 6 ms 468 KB Output is correct
49 Correct 6 ms 468 KB Output is correct
50 Correct 6 ms 468 KB Output is correct
51 Correct 5 ms 468 KB Output is correct
52 Correct 5 ms 468 KB Output is correct
53 Correct 6 ms 468 KB Output is correct
54 Correct 6 ms 460 KB Output is correct
55 Correct 69 ms 14948 KB Output is correct
56 Correct 73 ms 16824 KB Output is correct
57 Correct 68 ms 14772 KB Output is correct
58 Correct 69 ms 14912 KB Output is correct
59 Correct 70 ms 14916 KB Output is correct
60 Correct 66 ms 13888 KB Output is correct
61 Correct 65 ms 14392 KB Output is correct
62 Correct 69 ms 15808 KB Output is correct
63 Correct 67 ms 14916 KB Output is correct
64 Correct 71 ms 16048 KB Output is correct
65 Correct 69 ms 13792 KB Output is correct
66 Correct 71 ms 14460 KB Output is correct
67 Correct 69 ms 15420 KB Output is correct
68 Correct 71 ms 16192 KB Output is correct
69 Correct 299 ms 15200 KB Output is correct
70 Correct 341 ms 17488 KB Output is correct
71 Correct 335 ms 17592 KB Output is correct
72 Correct 263 ms 15228 KB Output is correct
73 Correct 257 ms 15332 KB Output is correct
74 Correct 98 ms 14948 KB Output is correct
75 Correct 119 ms 16312 KB Output is correct
76 Correct 246 ms 14912 KB Output is correct
77 Correct 273 ms 15804 KB Output is correct
78 Correct 310 ms 16440 KB Output is correct
79 Correct 230 ms 14132 KB Output is correct
80 Correct 306 ms 15036 KB Output is correct
81 Correct 348 ms 15876 KB Output is correct
82 Correct 332 ms 16488 KB Output is correct
83 Correct 672 ms 9136 KB Output is correct
84 Correct 59 ms 10520 KB Output is correct
85 Correct 882 ms 10392 KB Output is correct
86 Correct 845 ms 9184 KB Output is correct
87 Correct 1195 ms 9256 KB Output is correct
88 Correct 1232 ms 10044 KB Output is correct
89 Correct 845 ms 9172 KB Output is correct
90 Correct 1098 ms 10220 KB Output is correct
91 Correct 982 ms 8348 KB Output is correct
92 Correct 1192 ms 9572 KB Output is correct
93 Correct 1266 ms 10296 KB Output is correct
94 Correct 1141 ms 17724 KB Output is correct
95 Execution timed out 2077 ms 19112 KB Time limit exceeded
96 Halted 0 ms 0 KB -