Submission #1093567

# Submission time Handle Problem Language Result Execution time Memory
1093567 2024-09-27T03:48:22 Z Zero_OP Joker (BOI20_joker) C++14
100 / 100
248 ms 15164 KB
#include "bits/stdc++.h"

using namespace std;

const int MAX = 2e5 + 5;

int n, m, q, f[MAX], eu[MAX], ev[MAX], lab[MAX], d[MAX];
bool exist_odd_cycle;
struct state{
    int x, y;
};

stack<state> states;

void init(int n){
    fill(lab, lab + n, -1);
    fill(d, d + n, 0);
    exist_odd_cycle = false;
}

int find_root(int u){
    return lab[u] < 0 ? u : find_root(lab[u]);
}

int find_dist(int u){
    return lab[u] < 0 ? 0 : d[u] ^ find_dist(lab[u]);
}

bool unite(int u, int v){
    int l = find_dist(u) ^ find_dist(v) ^ 1;
    u = find_root(u); v = find_root(v);

    if(u == v){
        states.push({-1, exist_odd_cycle});
        if(l) exist_odd_cycle = true;
        return false;
    }

    if(lab[u] > lab[v]) swap(u, v);
    states.push({u, lab[u]});
    states.push({v, lab[v]});
    states.push({-1, exist_odd_cycle});
    lab[u] += lab[v];
    lab[v] = u;
    d[v] = l;
    return true;
}

int snap(){
    return (int)states.size();
}

void rollback(){
    if(states.top().x == -1){
        exist_odd_cycle = states.top().y;
    } else{
        lab[states.top().x] = states.top().y;
        d[states.top().x] = 0;
    }
    states.pop();
}

void back_to(int k){
    while((int)states.size() > k){
        rollback();
    }
}

void solve(int l, int r, int optL, int optR){
    if(l > r || optL > optR) return;
    int curSnap = snap();
    int mid = l + r >> 1;

    for(int i = l; i < mid; ++i){
        unite(eu[i], ev[i]);
    }

    int optMid = mid - 1, nxtSnap = snap();
    for(int i = optR; i >= max(optL, mid); --i){
        if(exist_odd_cycle){
            optMid = i;
            break;
        }

        unite(eu[i], ev[i]);
    }

    f[mid] = optMid;
    back_to(nxtSnap);

    unite(eu[mid], ev[mid]);
    solve(mid + 1, r, optMid, optR);
    back_to(curSnap);

    for(int i = optR; i > optMid; --i){
        unite(eu[i], ev[i]);
    }

    solve(l, mid - 1, optL, optMid);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    if(fopen("task.inp", "r")){
        freopen("task.inp", "r", stdin);
        freopen("task.out", "w", stdout);
    }

    cin >> n >> m >> q;
    for(int i = 1; i <= m; ++i){
        cin >> eu[i] >> ev[i];
        --eu[i], --ev[i];
    }

    init(n);
    for(int i = 1; i <= m; ++i) f[i] = 0;
    solve(1, m, 1, m);

    while(q--){
        int l, r;
        cin >> l >> r;
        cout << (f[l] >= r ? "YES\n" : "NO\n");
    }

    return 0;
}

Compilation message

Joker.cpp: In function 'void solve(int, int, int, int)':
Joker.cpp:72:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   72 |     int mid = l + r >> 1;
      |               ~~^~~
Joker.cpp: In function 'int main()':
Joker.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen("task.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 472 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 472 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 568 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 2 ms 480 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 2 ms 596 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 143 ms 9964 KB Output is correct
4 Correct 240 ms 13220 KB Output is correct
5 Correct 126 ms 11348 KB Output is correct
6 Correct 141 ms 10536 KB Output is correct
7 Correct 144 ms 10560 KB Output is correct
8 Correct 144 ms 9164 KB Output is correct
9 Correct 182 ms 9824 KB Output is correct
10 Correct 229 ms 11348 KB Output is correct
11 Correct 166 ms 10456 KB Output is correct
12 Correct 184 ms 11544 KB Output is correct
13 Correct 141 ms 9292 KB Output is correct
14 Correct 144 ms 9816 KB Output is correct
15 Correct 213 ms 10924 KB Output is correct
16 Correct 221 ms 11564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 472 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 143 ms 9964 KB Output is correct
30 Correct 240 ms 13220 KB Output is correct
31 Correct 126 ms 11348 KB Output is correct
32 Correct 141 ms 10536 KB Output is correct
33 Correct 144 ms 10560 KB Output is correct
34 Correct 144 ms 9164 KB Output is correct
35 Correct 182 ms 9824 KB Output is correct
36 Correct 229 ms 11348 KB Output is correct
37 Correct 166 ms 10456 KB Output is correct
38 Correct 184 ms 11544 KB Output is correct
39 Correct 141 ms 9292 KB Output is correct
40 Correct 144 ms 9816 KB Output is correct
41 Correct 213 ms 10924 KB Output is correct
42 Correct 221 ms 11564 KB Output is correct
43 Correct 146 ms 10980 KB Output is correct
44 Correct 248 ms 13728 KB Output is correct
45 Correct 204 ms 14052 KB Output is correct
46 Correct 133 ms 10960 KB Output is correct
47 Correct 136 ms 10836 KB Output is correct
48 Correct 180 ms 10228 KB Output is correct
49 Correct 229 ms 11648 KB Output is correct
50 Correct 161 ms 10328 KB Output is correct
51 Correct 183 ms 11240 KB Output is correct
52 Correct 190 ms 11900 KB Output is correct
53 Correct 147 ms 9520 KB Output is correct
54 Correct 182 ms 10284 KB Output is correct
55 Correct 218 ms 11288 KB Output is correct
56 Correct 228 ms 11852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 472 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 568 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 2 ms 480 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 2 ms 596 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
55 Correct 123 ms 9044 KB Output is correct
56 Correct 222 ms 11860 KB Output is correct
57 Correct 93 ms 11264 KB Output is correct
58 Correct 122 ms 9040 KB Output is correct
59 Correct 172 ms 8980 KB Output is correct
60 Correct 207 ms 10524 KB Output is correct
61 Correct 152 ms 9044 KB Output is correct
62 Correct 161 ms 10836 KB Output is correct
63 Correct 124 ms 7956 KB Output is correct
64 Correct 167 ms 9552 KB Output is correct
65 Correct 200 ms 10832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 472 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 568 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 2 ms 480 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 2 ms 596 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
55 Correct 143 ms 9964 KB Output is correct
56 Correct 240 ms 13220 KB Output is correct
57 Correct 126 ms 11348 KB Output is correct
58 Correct 141 ms 10536 KB Output is correct
59 Correct 144 ms 10560 KB Output is correct
60 Correct 144 ms 9164 KB Output is correct
61 Correct 182 ms 9824 KB Output is correct
62 Correct 229 ms 11348 KB Output is correct
63 Correct 166 ms 10456 KB Output is correct
64 Correct 184 ms 11544 KB Output is correct
65 Correct 141 ms 9292 KB Output is correct
66 Correct 144 ms 9816 KB Output is correct
67 Correct 213 ms 10924 KB Output is correct
68 Correct 221 ms 11564 KB Output is correct
69 Correct 146 ms 10980 KB Output is correct
70 Correct 248 ms 13728 KB Output is correct
71 Correct 204 ms 14052 KB Output is correct
72 Correct 133 ms 10960 KB Output is correct
73 Correct 136 ms 10836 KB Output is correct
74 Correct 180 ms 10228 KB Output is correct
75 Correct 229 ms 11648 KB Output is correct
76 Correct 161 ms 10328 KB Output is correct
77 Correct 183 ms 11240 KB Output is correct
78 Correct 190 ms 11900 KB Output is correct
79 Correct 147 ms 9520 KB Output is correct
80 Correct 182 ms 10284 KB Output is correct
81 Correct 218 ms 11288 KB Output is correct
82 Correct 228 ms 11852 KB Output is correct
83 Correct 123 ms 9044 KB Output is correct
84 Correct 222 ms 11860 KB Output is correct
85 Correct 93 ms 11264 KB Output is correct
86 Correct 122 ms 9040 KB Output is correct
87 Correct 172 ms 8980 KB Output is correct
88 Correct 207 ms 10524 KB Output is correct
89 Correct 152 ms 9044 KB Output is correct
90 Correct 161 ms 10836 KB Output is correct
91 Correct 124 ms 7956 KB Output is correct
92 Correct 167 ms 9552 KB Output is correct
93 Correct 200 ms 10832 KB Output is correct
94 Correct 146 ms 11792 KB Output is correct
95 Correct 182 ms 14392 KB Output is correct
96 Correct 128 ms 11356 KB Output is correct
97 Correct 144 ms 11656 KB Output is correct
98 Correct 152 ms 11620 KB Output is correct
99 Correct 164 ms 10728 KB Output is correct
100 Correct 211 ms 12932 KB Output is correct
101 Correct 171 ms 11064 KB Output is correct
102 Correct 187 ms 11864 KB Output is correct
103 Correct 214 ms 12916 KB Output is correct
104 Correct 151 ms 10504 KB Output is correct
105 Correct 211 ms 11600 KB Output is correct
106 Correct 230 ms 12800 KB Output is correct
107 Correct 130 ms 15164 KB Output is correct
108 Correct 146 ms 11240 KB Output is correct
109 Correct 151 ms 11456 KB Output is correct
110 Correct 149 ms 11352 KB Output is correct
111 Correct 146 ms 11420 KB Output is correct
112 Correct 143 ms 11492 KB Output is correct
113 Correct 142 ms 11492 KB Output is correct
114 Correct 150 ms 11492 KB Output is correct
115 Correct 143 ms 11240 KB Output is correct
116 Correct 152 ms 11356 KB Output is correct