Submission #1031244

# Submission time Handle Problem Language Result Execution time Memory
1031244 2024-07-22T16:23:41 Z kl0989e Joker (BOI20_joker) C++17
100 / 100
492 ms 21900 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define all(x) (x).begin(),(x).end()

const int maxn=2e5+10;
vector<pi> edge(maxn);
vi head(maxn);
vi col(maxn,1);
vi depth(maxn,1);
vector<vi> changes;
bool is_bipartite=1;

pi get(int a) {
    if (a==head[a]) {
        return {a,col[a]};
    }
    else {
        pi temp=get(head[a]);
        temp.se^=col[a];
        return temp;
    }
}

void unite(int a, int b) {
    int cola,colb;
    tie(a,cola)=get(a);
    tie(b,colb)=get(b);
    if (a==b) {
        if (cola==colb) {
            is_bipartite=0;
        }
        return;
    }
    if (depth[a]<depth[b]) {
        swap(a,b);
    }
    changes.pb({b,head[b],col[b],a,int(depth[a]==depth[b])});
    head[b]=a;
    col[b]=cola^colb^1;
    depth[a]+=int(depth[a]==depth[b]);
}

void rollback(int k) {
    while (changes.size()>k) {
        vi t=changes.back();
        changes.pop_back();
        head[t[0]]=t[1];
        col[t[0]]=t[2];
        depth[t[3]]-=t[4];
    }
}

int n,m,q;
vi lim(maxn);// max r where graph is not bipartite;

void dfs(int l_added=0, int r_added=m-1, int tl=0, int tr=m-1) {
    bool temp_is_bip=is_bipartite;
    int temp_sze=changes.size();
    int tm=tl+(tr-tl)/2;
    int l=l_added,r=r_added;
    while (l<tm) {
        unite(edge[l].fi,edge[l].se);
        l++;
    }
    while (is_bipartite && r>=tm) {
        unite(edge[r].fi,edge[r].se);
        r--;
    }
    lim[tm]=r;
    rollback(temp_sze);
    is_bipartite=temp_is_bip;
    if (tl==tr) {
        return;
    }
    int tempr=r_added;
    while (r_added>r) {
        unite(edge[r_added].fi,edge[r_added].se);
        r_added--;
    }
    if (tl!=tm) {
        dfs(l_added,r_added,tl,tm-1);
    }
    is_bipartite=temp_is_bip;
    rollback(temp_sze);
    while (l_added<=tm) {
        unite(edge[l_added].fi,edge[l_added].se);
        l_added++;
    }
    dfs(l_added,tempr,tm+1,tr);
    is_bipartite=temp_is_bip;
    rollback(temp_sze);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> q;
    iota(all(head),0);
    for (int i=0; i<m; i++) {
        cin >> edge[i].fi >> edge[i].se;
        edge[i].fi--;
        edge[i].se--;
    }
    dfs();
    int l,r;
    for (int i=0; i<q; i++) {
        cin >> l >> r;
        cout << (r-1<=lim[l-1]?"YES\n":"NO\n");
    }
    return 0;
}

Compilation message

Joker.cpp: In function 'void rollback(int)':
Joker.cpp:51:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   51 |     while (changes.size()>k) {
      |            ~~~~~~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 2 ms 5172 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 4 ms 4956 KB Output is correct
9 Correct 4 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5084 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 5120 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 5008 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 2 ms 5176 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 4956 KB Output is correct
22 Correct 3 ms 4956 KB Output is correct
23 Correct 3 ms 4952 KB Output is correct
24 Correct 4 ms 4956 KB Output is correct
25 Correct 3 ms 5176 KB Output is correct
26 Correct 2 ms 4956 KB Output is correct
27 Correct 4 ms 4956 KB Output is correct
28 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 2 ms 5172 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 4 ms 4956 KB Output is correct
9 Correct 4 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5084 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 5120 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 5008 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 2 ms 5176 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 4956 KB Output is correct
22 Correct 3 ms 4956 KB Output is correct
23 Correct 3 ms 4952 KB Output is correct
24 Correct 4 ms 4956 KB Output is correct
25 Correct 3 ms 5176 KB Output is correct
26 Correct 2 ms 4956 KB Output is correct
27 Correct 4 ms 4956 KB Output is correct
28 Correct 2 ms 4956 KB Output is correct
29 Correct 4 ms 5208 KB Output is correct
30 Correct 5 ms 5212 KB Output is correct
31 Correct 6 ms 5208 KB Output is correct
32 Correct 4 ms 5212 KB Output is correct
33 Correct 4 ms 5212 KB Output is correct
34 Correct 4 ms 5212 KB Output is correct
35 Correct 5 ms 5212 KB Output is correct
36 Correct 4 ms 5240 KB Output is correct
37 Correct 5 ms 5328 KB Output is correct
38 Correct 5 ms 5328 KB Output is correct
39 Correct 7 ms 5212 KB Output is correct
40 Correct 4 ms 5212 KB Output is correct
41 Correct 4 ms 5212 KB Output is correct
42 Correct 4 ms 5212 KB Output is correct
43 Correct 5 ms 5264 KB Output is correct
44 Correct 4 ms 5212 KB Output is correct
45 Correct 5 ms 5212 KB Output is correct
46 Correct 5 ms 5212 KB Output is correct
47 Correct 5 ms 5212 KB Output is correct
48 Correct 6 ms 5212 KB Output is correct
49 Correct 4 ms 5208 KB Output is correct
50 Correct 6 ms 5212 KB Output is correct
51 Correct 4 ms 5212 KB Output is correct
52 Correct 4 ms 5264 KB Output is correct
53 Correct 4 ms 5212 KB Output is correct
54 Correct 5 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 255 ms 15240 KB Output is correct
4 Correct 492 ms 20104 KB Output is correct
5 Correct 254 ms 19668 KB Output is correct
6 Correct 245 ms 15104 KB Output is correct
7 Correct 307 ms 15084 KB Output is correct
8 Correct 271 ms 12316 KB Output is correct
9 Correct 332 ms 14604 KB Output is correct
10 Correct 489 ms 18600 KB Output is correct
11 Correct 284 ms 14988 KB Output is correct
12 Correct 366 ms 18428 KB Output is correct
13 Correct 222 ms 10760 KB Output is correct
14 Correct 250 ms 13060 KB Output is correct
15 Correct 388 ms 17996 KB Output is correct
16 Correct 430 ms 18924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 2 ms 5172 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 4 ms 4956 KB Output is correct
9 Correct 4 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5084 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 5120 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 5008 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 2 ms 5176 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 4956 KB Output is correct
22 Correct 3 ms 4956 KB Output is correct
23 Correct 3 ms 4952 KB Output is correct
24 Correct 4 ms 4956 KB Output is correct
25 Correct 3 ms 5176 KB Output is correct
26 Correct 2 ms 4956 KB Output is correct
27 Correct 4 ms 4956 KB Output is correct
28 Correct 2 ms 4956 KB Output is correct
29 Correct 255 ms 15240 KB Output is correct
30 Correct 492 ms 20104 KB Output is correct
31 Correct 254 ms 19668 KB Output is correct
32 Correct 245 ms 15104 KB Output is correct
33 Correct 307 ms 15084 KB Output is correct
34 Correct 271 ms 12316 KB Output is correct
35 Correct 332 ms 14604 KB Output is correct
36 Correct 489 ms 18600 KB Output is correct
37 Correct 284 ms 14988 KB Output is correct
38 Correct 366 ms 18428 KB Output is correct
39 Correct 222 ms 10760 KB Output is correct
40 Correct 250 ms 13060 KB Output is correct
41 Correct 388 ms 17996 KB Output is correct
42 Correct 430 ms 18924 KB Output is correct
43 Correct 255 ms 15444 KB Output is correct
44 Correct 398 ms 20712 KB Output is correct
45 Correct 343 ms 21108 KB Output is correct
46 Correct 218 ms 15620 KB Output is correct
47 Correct 245 ms 15620 KB Output is correct
48 Correct 298 ms 15104 KB Output is correct
49 Correct 378 ms 19172 KB Output is correct
50 Correct 241 ms 14236 KB Output is correct
51 Correct 287 ms 17920 KB Output is correct
52 Correct 363 ms 19212 KB Output is correct
53 Correct 203 ms 11020 KB Output is correct
54 Correct 293 ms 14508 KB Output is correct
55 Correct 341 ms 17880 KB Output is correct
56 Correct 414 ms 19268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 2 ms 5172 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 4 ms 4956 KB Output is correct
9 Correct 4 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5084 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 5120 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 5008 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 2 ms 5176 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 4956 KB Output is correct
22 Correct 3 ms 4956 KB Output is correct
23 Correct 3 ms 4952 KB Output is correct
24 Correct 4 ms 4956 KB Output is correct
25 Correct 3 ms 5176 KB Output is correct
26 Correct 2 ms 4956 KB Output is correct
27 Correct 4 ms 4956 KB Output is correct
28 Correct 2 ms 4956 KB Output is correct
29 Correct 4 ms 5208 KB Output is correct
30 Correct 5 ms 5212 KB Output is correct
31 Correct 6 ms 5208 KB Output is correct
32 Correct 4 ms 5212 KB Output is correct
33 Correct 4 ms 5212 KB Output is correct
34 Correct 4 ms 5212 KB Output is correct
35 Correct 5 ms 5212 KB Output is correct
36 Correct 4 ms 5240 KB Output is correct
37 Correct 5 ms 5328 KB Output is correct
38 Correct 5 ms 5328 KB Output is correct
39 Correct 7 ms 5212 KB Output is correct
40 Correct 4 ms 5212 KB Output is correct
41 Correct 4 ms 5212 KB Output is correct
42 Correct 4 ms 5212 KB Output is correct
43 Correct 5 ms 5264 KB Output is correct
44 Correct 4 ms 5212 KB Output is correct
45 Correct 5 ms 5212 KB Output is correct
46 Correct 5 ms 5212 KB Output is correct
47 Correct 5 ms 5212 KB Output is correct
48 Correct 6 ms 5212 KB Output is correct
49 Correct 4 ms 5208 KB Output is correct
50 Correct 6 ms 5212 KB Output is correct
51 Correct 4 ms 5212 KB Output is correct
52 Correct 4 ms 5264 KB Output is correct
53 Correct 4 ms 5212 KB Output is correct
54 Correct 5 ms 5212 KB Output is correct
55 Correct 212 ms 12800 KB Output is correct
56 Correct 378 ms 18660 KB Output is correct
57 Correct 201 ms 17872 KB Output is correct
58 Correct 222 ms 12812 KB Output is correct
59 Correct 285 ms 13056 KB Output is correct
60 Correct 394 ms 17920 KB Output is correct
61 Correct 276 ms 12704 KB Output is correct
62 Correct 302 ms 17924 KB Output is correct
63 Correct 193 ms 9992 KB Output is correct
64 Correct 325 ms 13888 KB Output is correct
65 Correct 403 ms 17780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 2 ms 5172 KB Output is correct
4 Correct 4 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 4 ms 4956 KB Output is correct
9 Correct 4 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 5084 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 5120 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 5008 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 2 ms 5176 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 4956 KB Output is correct
22 Correct 3 ms 4956 KB Output is correct
23 Correct 3 ms 4952 KB Output is correct
24 Correct 4 ms 4956 KB Output is correct
25 Correct 3 ms 5176 KB Output is correct
26 Correct 2 ms 4956 KB Output is correct
27 Correct 4 ms 4956 KB Output is correct
28 Correct 2 ms 4956 KB Output is correct
29 Correct 4 ms 5208 KB Output is correct
30 Correct 5 ms 5212 KB Output is correct
31 Correct 6 ms 5208 KB Output is correct
32 Correct 4 ms 5212 KB Output is correct
33 Correct 4 ms 5212 KB Output is correct
34 Correct 4 ms 5212 KB Output is correct
35 Correct 5 ms 5212 KB Output is correct
36 Correct 4 ms 5240 KB Output is correct
37 Correct 5 ms 5328 KB Output is correct
38 Correct 5 ms 5328 KB Output is correct
39 Correct 7 ms 5212 KB Output is correct
40 Correct 4 ms 5212 KB Output is correct
41 Correct 4 ms 5212 KB Output is correct
42 Correct 4 ms 5212 KB Output is correct
43 Correct 5 ms 5264 KB Output is correct
44 Correct 4 ms 5212 KB Output is correct
45 Correct 5 ms 5212 KB Output is correct
46 Correct 5 ms 5212 KB Output is correct
47 Correct 5 ms 5212 KB Output is correct
48 Correct 6 ms 5212 KB Output is correct
49 Correct 4 ms 5208 KB Output is correct
50 Correct 6 ms 5212 KB Output is correct
51 Correct 4 ms 5212 KB Output is correct
52 Correct 4 ms 5264 KB Output is correct
53 Correct 4 ms 5212 KB Output is correct
54 Correct 5 ms 5212 KB Output is correct
55 Correct 255 ms 15240 KB Output is correct
56 Correct 492 ms 20104 KB Output is correct
57 Correct 254 ms 19668 KB Output is correct
58 Correct 245 ms 15104 KB Output is correct
59 Correct 307 ms 15084 KB Output is correct
60 Correct 271 ms 12316 KB Output is correct
61 Correct 332 ms 14604 KB Output is correct
62 Correct 489 ms 18600 KB Output is correct
63 Correct 284 ms 14988 KB Output is correct
64 Correct 366 ms 18428 KB Output is correct
65 Correct 222 ms 10760 KB Output is correct
66 Correct 250 ms 13060 KB Output is correct
67 Correct 388 ms 17996 KB Output is correct
68 Correct 430 ms 18924 KB Output is correct
69 Correct 255 ms 15444 KB Output is correct
70 Correct 398 ms 20712 KB Output is correct
71 Correct 343 ms 21108 KB Output is correct
72 Correct 218 ms 15620 KB Output is correct
73 Correct 245 ms 15620 KB Output is correct
74 Correct 298 ms 15104 KB Output is correct
75 Correct 378 ms 19172 KB Output is correct
76 Correct 241 ms 14236 KB Output is correct
77 Correct 287 ms 17920 KB Output is correct
78 Correct 363 ms 19212 KB Output is correct
79 Correct 203 ms 11020 KB Output is correct
80 Correct 293 ms 14508 KB Output is correct
81 Correct 341 ms 17880 KB Output is correct
82 Correct 414 ms 19268 KB Output is correct
83 Correct 212 ms 12800 KB Output is correct
84 Correct 378 ms 18660 KB Output is correct
85 Correct 201 ms 17872 KB Output is correct
86 Correct 222 ms 12812 KB Output is correct
87 Correct 285 ms 13056 KB Output is correct
88 Correct 394 ms 17920 KB Output is correct
89 Correct 276 ms 12704 KB Output is correct
90 Correct 302 ms 17924 KB Output is correct
91 Correct 193 ms 9992 KB Output is correct
92 Correct 325 ms 13888 KB Output is correct
93 Correct 403 ms 17780 KB Output is correct
94 Correct 253 ms 16260 KB Output is correct
95 Correct 346 ms 21568 KB Output is correct
96 Correct 251 ms 20728 KB Output is correct
97 Correct 233 ms 16264 KB Output is correct
98 Correct 255 ms 16132 KB Output is correct
99 Correct 253 ms 14080 KB Output is correct
100 Correct 395 ms 20092 KB Output is correct
101 Correct 260 ms 15104 KB Output is correct
102 Correct 310 ms 18168 KB Output is correct
103 Correct 393 ms 20148 KB Output is correct
104 Correct 249 ms 12804 KB Output is correct
105 Correct 358 ms 17108 KB Output is correct
106 Correct 412 ms 19444 KB Output is correct
107 Correct 205 ms 21900 KB Output is correct
108 Correct 239 ms 15872 KB Output is correct
109 Correct 247 ms 15876 KB Output is correct
110 Correct 230 ms 15872 KB Output is correct
111 Correct 245 ms 15848 KB Output is correct
112 Correct 244 ms 16012 KB Output is correct
113 Correct 231 ms 15956 KB Output is correct
114 Correct 218 ms 15848 KB Output is correct
115 Correct 237 ms 15872 KB Output is correct
116 Correct 249 ms 15868 KB Output is correct