답안 #765497

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765497 2023-06-24T15:17:48 Z adrilen Joker (BOI20_joker) C++17
49 / 100
2000 ms 9848 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;

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)
    {
        if (lhs[1] == rhs[1]) return lhs[0] > rhs[0];
        return lhs[1] > rhs[1];
    });

    vector <int> output(q);

    UnionFind Graph(n, m);

    // const int max_range = 200;

    bool have_found_cycle = false, this_cycle;

    auto right_edge = edges.end() - 1;
    auto left_edge = edges.begin();
    for (auto &i : queries)
    {
        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
        assert(left_edge == edges.begin());
        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 != edges.begin())
        {
            Graph.undo();
            left_edge--;
        }

    }

    for (auto &i : output) cout << (i ? "YES" : "NO") << "\n";

    // for (auto &i : queries)
    // {
    //     cout << (i[1] < a ? "YES" : "NO") << "\n";
    // }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 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 212 KB Output is correct
15 Correct 1 ms 212 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 212 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 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 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 212 KB Output is correct
15 Correct 1 ms 212 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 212 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 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 13 ms 340 KB Output is correct
30 Correct 16 ms 340 KB Output is correct
31 Correct 16 ms 404 KB Output is correct
32 Correct 10 ms 392 KB Output is correct
33 Correct 7 ms 340 KB Output is correct
34 Correct 13 ms 404 KB Output is correct
35 Correct 19 ms 424 KB Output is correct
36 Correct 18 ms 400 KB Output is correct
37 Correct 24 ms 340 KB Output is correct
38 Correct 27 ms 400 KB Output is correct
39 Correct 20 ms 340 KB Output is correct
40 Correct 15 ms 340 KB Output is correct
41 Correct 15 ms 392 KB Output is correct
42 Correct 14 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 4 ms 340 KB Output is correct
45 Correct 5 ms 340 KB Output is correct
46 Correct 11 ms 416 KB Output is correct
47 Correct 14 ms 388 KB Output is correct
48 Correct 16 ms 396 KB Output is correct
49 Correct 16 ms 412 KB Output is correct
50 Correct 18 ms 420 KB Output is correct
51 Correct 14 ms 396 KB Output is correct
52 Correct 16 ms 396 KB Output is correct
53 Correct 16 ms 340 KB Output is correct
54 Correct 19 ms 420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 64 ms 7648 KB Output is correct
4 Correct 71 ms 9612 KB Output is correct
5 Correct 66 ms 7956 KB Output is correct
6 Correct 65 ms 7684 KB Output is correct
7 Correct 67 ms 7692 KB Output is correct
8 Correct 62 ms 7296 KB Output is correct
9 Correct 62 ms 7812 KB Output is correct
10 Correct 67 ms 9040 KB Output is correct
11 Correct 65 ms 7680 KB Output is correct
12 Correct 71 ms 8648 KB Output is correct
13 Correct 66 ms 6676 KB Output is correct
14 Correct 65 ms 7176 KB Output is correct
15 Correct 66 ms 8136 KB Output is correct
16 Correct 67 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 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 212 KB Output is correct
15 Correct 1 ms 212 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 212 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 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 64 ms 7648 KB Output is correct
30 Correct 71 ms 9612 KB Output is correct
31 Correct 66 ms 7956 KB Output is correct
32 Correct 65 ms 7684 KB Output is correct
33 Correct 67 ms 7692 KB Output is correct
34 Correct 62 ms 7296 KB Output is correct
35 Correct 62 ms 7812 KB Output is correct
36 Correct 67 ms 9040 KB Output is correct
37 Correct 65 ms 7680 KB Output is correct
38 Correct 71 ms 8648 KB Output is correct
39 Correct 66 ms 6676 KB Output is correct
40 Correct 65 ms 7176 KB Output is correct
41 Correct 66 ms 8136 KB Output is correct
42 Correct 67 ms 8796 KB Output is correct
43 Correct 310 ms 7684 KB Output is correct
44 Correct 335 ms 9848 KB Output is correct
45 Correct 329 ms 9796 KB Output is correct
46 Correct 258 ms 7940 KB Output is correct
47 Correct 253 ms 8008 KB Output is correct
48 Correct 93 ms 8140 KB Output is correct
49 Correct 112 ms 9056 KB Output is correct
50 Correct 236 ms 7556 KB Output is correct
51 Correct 267 ms 8208 KB Output is correct
52 Correct 284 ms 8904 KB Output is correct
53 Correct 225 ms 6804 KB Output is correct
54 Correct 300 ms 7420 KB Output is correct
55 Correct 310 ms 8192 KB Output is correct
56 Correct 324 ms 9212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 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 212 KB Output is correct
15 Correct 1 ms 212 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 212 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 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 13 ms 340 KB Output is correct
30 Correct 16 ms 340 KB Output is correct
31 Correct 16 ms 404 KB Output is correct
32 Correct 10 ms 392 KB Output is correct
33 Correct 7 ms 340 KB Output is correct
34 Correct 13 ms 404 KB Output is correct
35 Correct 19 ms 424 KB Output is correct
36 Correct 18 ms 400 KB Output is correct
37 Correct 24 ms 340 KB Output is correct
38 Correct 27 ms 400 KB Output is correct
39 Correct 20 ms 340 KB Output is correct
40 Correct 15 ms 340 KB Output is correct
41 Correct 15 ms 392 KB Output is correct
42 Correct 14 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 4 ms 340 KB Output is correct
45 Correct 5 ms 340 KB Output is correct
46 Correct 11 ms 416 KB Output is correct
47 Correct 14 ms 388 KB Output is correct
48 Correct 16 ms 396 KB Output is correct
49 Correct 16 ms 412 KB Output is correct
50 Correct 18 ms 420 KB Output is correct
51 Correct 14 ms 396 KB Output is correct
52 Correct 16 ms 396 KB Output is correct
53 Correct 16 ms 340 KB Output is correct
54 Correct 19 ms 420 KB Output is correct
55 Execution timed out 2045 ms 4424 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 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 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 212 KB Output is correct
15 Correct 1 ms 212 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 212 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 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 13 ms 340 KB Output is correct
30 Correct 16 ms 340 KB Output is correct
31 Correct 16 ms 404 KB Output is correct
32 Correct 10 ms 392 KB Output is correct
33 Correct 7 ms 340 KB Output is correct
34 Correct 13 ms 404 KB Output is correct
35 Correct 19 ms 424 KB Output is correct
36 Correct 18 ms 400 KB Output is correct
37 Correct 24 ms 340 KB Output is correct
38 Correct 27 ms 400 KB Output is correct
39 Correct 20 ms 340 KB Output is correct
40 Correct 15 ms 340 KB Output is correct
41 Correct 15 ms 392 KB Output is correct
42 Correct 14 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 4 ms 340 KB Output is correct
45 Correct 5 ms 340 KB Output is correct
46 Correct 11 ms 416 KB Output is correct
47 Correct 14 ms 388 KB Output is correct
48 Correct 16 ms 396 KB Output is correct
49 Correct 16 ms 412 KB Output is correct
50 Correct 18 ms 420 KB Output is correct
51 Correct 14 ms 396 KB Output is correct
52 Correct 16 ms 396 KB Output is correct
53 Correct 16 ms 340 KB Output is correct
54 Correct 19 ms 420 KB Output is correct
55 Correct 64 ms 7648 KB Output is correct
56 Correct 71 ms 9612 KB Output is correct
57 Correct 66 ms 7956 KB Output is correct
58 Correct 65 ms 7684 KB Output is correct
59 Correct 67 ms 7692 KB Output is correct
60 Correct 62 ms 7296 KB Output is correct
61 Correct 62 ms 7812 KB Output is correct
62 Correct 67 ms 9040 KB Output is correct
63 Correct 65 ms 7680 KB Output is correct
64 Correct 71 ms 8648 KB Output is correct
65 Correct 66 ms 6676 KB Output is correct
66 Correct 65 ms 7176 KB Output is correct
67 Correct 66 ms 8136 KB Output is correct
68 Correct 67 ms 8796 KB Output is correct
69 Correct 310 ms 7684 KB Output is correct
70 Correct 335 ms 9848 KB Output is correct
71 Correct 329 ms 9796 KB Output is correct
72 Correct 258 ms 7940 KB Output is correct
73 Correct 253 ms 8008 KB Output is correct
74 Correct 93 ms 8140 KB Output is correct
75 Correct 112 ms 9056 KB Output is correct
76 Correct 236 ms 7556 KB Output is correct
77 Correct 267 ms 8208 KB Output is correct
78 Correct 284 ms 8904 KB Output is correct
79 Correct 225 ms 6804 KB Output is correct
80 Correct 300 ms 7420 KB Output is correct
81 Correct 310 ms 8192 KB Output is correct
82 Correct 324 ms 9212 KB Output is correct
83 Execution timed out 2045 ms 4424 KB Time limit exceeded
84 Halted 0 ms 0 KB -