답안 #768136

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768136 2023-06-27T14:09:35 Z adrilen Joker (BOI20_joker) C++17
100 / 100
1594 ms 19228 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
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 maxm = 2e5;
constexpr int sqrt_n = 800, sqrt_nn = maxm / sqrt_n + 1; 


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;
    }

    // Make a fnd with compression, such that it will be O(1)

    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_nn);
    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";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 320 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 316 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 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 320 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 320 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 316 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 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 320 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 5 ms 468 KB Output is correct
30 Correct 4 ms 468 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 4 ms 464 KB Output is correct
37 Correct 5 ms 468 KB Output is correct
38 Correct 6 ms 500 KB Output is correct
39 Correct 5 ms 468 KB Output is correct
40 Correct 3 ms 468 KB Output is correct
41 Correct 3 ms 460 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 1 ms 456 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 3 ms 468 KB Output is correct
48 Correct 3 ms 372 KB Output is correct
49 Correct 4 ms 500 KB Output is correct
50 Correct 3 ms 468 KB Output is correct
51 Correct 3 ms 468 KB Output is correct
52 Correct 3 ms 468 KB Output is correct
53 Correct 4 ms 456 KB Output is correct
54 Correct 4 ms 500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 76 ms 11728 KB Output is correct
4 Correct 79 ms 14072 KB Output is correct
5 Correct 76 ms 11324 KB Output is correct
6 Correct 75 ms 11720 KB Output is correct
7 Correct 92 ms 11716 KB Output is correct
8 Correct 72 ms 11320 KB Output is correct
9 Correct 75 ms 11896 KB Output is correct
10 Correct 75 ms 13048 KB Output is correct
11 Correct 76 ms 11712 KB Output is correct
12 Correct 83 ms 12608 KB Output is correct
13 Correct 76 ms 10736 KB Output is correct
14 Correct 80 ms 11260 KB Output is correct
15 Correct 77 ms 12148 KB Output is correct
16 Correct 78 ms 12864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 320 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 316 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 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 320 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 76 ms 11728 KB Output is correct
30 Correct 79 ms 14072 KB Output is correct
31 Correct 76 ms 11324 KB Output is correct
32 Correct 75 ms 11720 KB Output is correct
33 Correct 92 ms 11716 KB Output is correct
34 Correct 72 ms 11320 KB Output is correct
35 Correct 75 ms 11896 KB Output is correct
36 Correct 75 ms 13048 KB Output is correct
37 Correct 76 ms 11712 KB Output is correct
38 Correct 83 ms 12608 KB Output is correct
39 Correct 76 ms 10736 KB Output is correct
40 Correct 80 ms 11260 KB Output is correct
41 Correct 77 ms 12148 KB Output is correct
42 Correct 78 ms 12864 KB Output is correct
43 Correct 194 ms 11700 KB Output is correct
44 Correct 213 ms 14072 KB Output is correct
45 Correct 223 ms 14056 KB Output is correct
46 Correct 171 ms 11684 KB Output is correct
47 Correct 173 ms 11668 KB Output is correct
48 Correct 96 ms 11828 KB Output is correct
49 Correct 109 ms 13000 KB Output is correct
50 Correct 186 ms 11476 KB Output is correct
51 Correct 232 ms 12236 KB Output is correct
52 Correct 195 ms 12828 KB Output is correct
53 Correct 191 ms 10816 KB Output is correct
54 Correct 244 ms 11444 KB Output is correct
55 Correct 249 ms 12116 KB Output is correct
56 Correct 249 ms 12920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 320 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 316 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 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 320 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 5 ms 468 KB Output is correct
30 Correct 4 ms 468 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 4 ms 464 KB Output is correct
37 Correct 5 ms 468 KB Output is correct
38 Correct 6 ms 500 KB Output is correct
39 Correct 5 ms 468 KB Output is correct
40 Correct 3 ms 468 KB Output is correct
41 Correct 3 ms 460 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 1 ms 456 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 3 ms 468 KB Output is correct
48 Correct 3 ms 372 KB Output is correct
49 Correct 4 ms 500 KB Output is correct
50 Correct 3 ms 468 KB Output is correct
51 Correct 3 ms 468 KB Output is correct
52 Correct 3 ms 468 KB Output is correct
53 Correct 4 ms 456 KB Output is correct
54 Correct 4 ms 500 KB Output is correct
55 Correct 558 ms 7568 KB Output is correct
56 Correct 55 ms 8672 KB Output is correct
57 Correct 729 ms 8492 KB Output is correct
58 Correct 738 ms 7624 KB Output is correct
59 Correct 1086 ms 7604 KB Output is correct
60 Correct 1152 ms 8468 KB Output is correct
61 Correct 735 ms 7536 KB Output is correct
62 Correct 981 ms 8608 KB Output is correct
63 Correct 916 ms 6808 KB Output is correct
64 Correct 1084 ms 7760 KB Output is correct
65 Correct 1088 ms 8636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 320 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 316 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 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 320 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 5 ms 468 KB Output is correct
30 Correct 4 ms 468 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 4 ms 464 KB Output is correct
37 Correct 5 ms 468 KB Output is correct
38 Correct 6 ms 500 KB Output is correct
39 Correct 5 ms 468 KB Output is correct
40 Correct 3 ms 468 KB Output is correct
41 Correct 3 ms 460 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 1 ms 456 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 3 ms 468 KB Output is correct
48 Correct 3 ms 372 KB Output is correct
49 Correct 4 ms 500 KB Output is correct
50 Correct 3 ms 468 KB Output is correct
51 Correct 3 ms 468 KB Output is correct
52 Correct 3 ms 468 KB Output is correct
53 Correct 4 ms 456 KB Output is correct
54 Correct 4 ms 500 KB Output is correct
55 Correct 76 ms 11728 KB Output is correct
56 Correct 79 ms 14072 KB Output is correct
57 Correct 76 ms 11324 KB Output is correct
58 Correct 75 ms 11720 KB Output is correct
59 Correct 92 ms 11716 KB Output is correct
60 Correct 72 ms 11320 KB Output is correct
61 Correct 75 ms 11896 KB Output is correct
62 Correct 75 ms 13048 KB Output is correct
63 Correct 76 ms 11712 KB Output is correct
64 Correct 83 ms 12608 KB Output is correct
65 Correct 76 ms 10736 KB Output is correct
66 Correct 80 ms 11260 KB Output is correct
67 Correct 77 ms 12148 KB Output is correct
68 Correct 78 ms 12864 KB Output is correct
69 Correct 194 ms 11700 KB Output is correct
70 Correct 213 ms 14072 KB Output is correct
71 Correct 223 ms 14056 KB Output is correct
72 Correct 171 ms 11684 KB Output is correct
73 Correct 173 ms 11668 KB Output is correct
74 Correct 96 ms 11828 KB Output is correct
75 Correct 109 ms 13000 KB Output is correct
76 Correct 186 ms 11476 KB Output is correct
77 Correct 232 ms 12236 KB Output is correct
78 Correct 195 ms 12828 KB Output is correct
79 Correct 191 ms 10816 KB Output is correct
80 Correct 244 ms 11444 KB Output is correct
81 Correct 249 ms 12116 KB Output is correct
82 Correct 249 ms 12920 KB Output is correct
83 Correct 558 ms 7568 KB Output is correct
84 Correct 55 ms 8672 KB Output is correct
85 Correct 729 ms 8492 KB Output is correct
86 Correct 738 ms 7624 KB Output is correct
87 Correct 1086 ms 7604 KB Output is correct
88 Correct 1152 ms 8468 KB Output is correct
89 Correct 735 ms 7536 KB Output is correct
90 Correct 981 ms 8608 KB Output is correct
91 Correct 916 ms 6808 KB Output is correct
92 Correct 1084 ms 7760 KB Output is correct
93 Correct 1088 ms 8636 KB Output is correct
94 Correct 851 ms 13592 KB Output is correct
95 Correct 1594 ms 14924 KB Output is correct
96 Correct 1016 ms 18984 KB Output is correct
97 Correct 925 ms 17784 KB Output is correct
98 Correct 914 ms 17720 KB Output is correct
99 Correct 876 ms 17140 KB Output is correct
100 Correct 956 ms 19132 KB Output is correct
101 Correct 862 ms 17548 KB Output is correct
102 Correct 1054 ms 18328 KB Output is correct
103 Correct 1209 ms 19228 KB Output is correct
104 Correct 1098 ms 17000 KB Output is correct
105 Correct 1281 ms 18272 KB Output is correct
106 Correct 1451 ms 18852 KB Output is correct
107 Correct 70 ms 13244 KB Output is correct
108 Correct 838 ms 17288 KB Output is correct
109 Correct 871 ms 17356 KB Output is correct
110 Correct 881 ms 17376 KB Output is correct
111 Correct 901 ms 17348 KB Output is correct
112 Correct 914 ms 17380 KB Output is correct
113 Correct 917 ms 17416 KB Output is correct
114 Correct 910 ms 17344 KB Output is correct
115 Correct 900 ms 17424 KB Output is correct
116 Correct 941 ms 17356 KB Output is correct