#include "swap.h"
#include<bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
const int mxn=2e5+1;
const int inf=2e9;
int L[mxn],R[mxn],cost[mxn];
int n,m;
vector<int> sorted;
struct reachability_tree {
int par[mxn];
int lineW[mxn];
int sz[mxn];
int start[mxn];
int end[mxn];
int pw[mxn];
vector<int> w[mxn];
vector<int> li[mxn];
void init(int n) {
for(int j=0;j<n;j++) {
sz[j]=1;
par[j]=j;
start[j]=j;
end[j]=j;
lineW[j]=inf;
}
}
int findrep(int u) {
return par[u]==u ? u : findrep(par[u]);
}
void join(int u,int v,int weight) {
int _u=findrep(u);
int _v=findrep(v);
if(_u==_v) {
lineW[_u]=min(lineW[_u],weight);
w[_u].push_back(weight);
li[_u].push_back(lineW[_u]);
return;
}
if(sz[_u]>sz[_v]) {
swap(_u,_v);
swap(u,v);
}
par[_u]=_v;
sz[_v]+=sz[_u];
pw[_u]=weight;
lineW[_v]=min(lineW[_v],lineW[_u]);
if(lineW[_v]==inf && ((u==start[_u] || u==end[_u]) && (v==start[_v] || v==end[_v]))) {
if(v==start[_v]) {
start[_v]=end[_v];
end[_v]=start[_u] ^ end[_u] ^ u;
} else {
end[_v]=start[_u] ^ end[_u] ^ u;
}
} else {
lineW[_v]=min(lineW[_v],weight);
}
w[_v].push_back(weight);
li[_v].push_back(lineW[_v]);
}
int goup(int u,int weight) {
while(par[u]!=u && pw[u]<=weight) {
u=par[u];
}
return u;
}
bool check(int x,int weight) {
int k=upper_bound(w[x].begin(),w[x].end(),weight)-w[x].begin();
if(k==0) return false;
int v=li[x][k-1];
return v<=weight;
}
} rt;
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n=N;
m=M;
for(int i=0;i<M;i++) {
L[i]=U[i];
R[i]=V[i];
cost[i]=W[i];
sorted.push_back(i);
}
sort(sorted.begin(),sorted.end(),[](int a,int b) {
return cost[a]<cost[b];
});
rt.init(n);
for(int i=0;i<m;i++) {
int u=L[sorted[i]];
int v=R[sorted[i]];
int c=cost[sorted[i]];
rt.join(u,v,c);
}
}
int getMinimumFuelCapacity(int X, int Y) {
int lo=0;
int hi=m-1;
int ans=-1;
while(lo<=hi) {
int m=lo+hi>>1;
int v=cost[sorted[m]];
int a=rt.goup(X,v);
int b=rt.goup(Y,v);
if(a==b && rt.check(a,v)) {
ans=v;
hi=m-1;
} else {
lo=m+1;
}
}
return ans;
}
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:144:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
144 | int m=lo+hi>>1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9856 KB |
Output is correct |
6 |
Correct |
8 ms |
9856 KB |
Output is correct |
7 |
Correct |
8 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
86 ms |
18416 KB |
Output is correct |
10 |
Correct |
110 ms |
20340 KB |
Output is correct |
11 |
Correct |
113 ms |
20208 KB |
Output is correct |
12 |
Correct |
117 ms |
20848 KB |
Output is correct |
13 |
Correct |
77 ms |
19312 KB |
Output is correct |
14 |
Correct |
106 ms |
18800 KB |
Output is correct |
15 |
Correct |
306 ms |
24572 KB |
Output is correct |
16 |
Correct |
302 ms |
24352 KB |
Output is correct |
17 |
Correct |
307 ms |
24920 KB |
Output is correct |
18 |
Correct |
239 ms |
23640 KB |
Output is correct |
19 |
Correct |
150 ms |
16632 KB |
Output is correct |
20 |
Correct |
299 ms |
25588 KB |
Output is correct |
21 |
Correct |
302 ms |
25648 KB |
Output is correct |
22 |
Correct |
320 ms |
26368 KB |
Output is correct |
23 |
Correct |
252 ms |
25048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
322 ms |
23828 KB |
Output is correct |
4 |
Correct |
329 ms |
23896 KB |
Output is correct |
5 |
Correct |
343 ms |
24328 KB |
Output is correct |
6 |
Correct |
316 ms |
23896 KB |
Output is correct |
7 |
Correct |
330 ms |
24176 KB |
Output is correct |
8 |
Correct |
325 ms |
23828 KB |
Output is correct |
9 |
Correct |
327 ms |
23888 KB |
Output is correct |
10 |
Correct |
321 ms |
23852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9856 KB |
Output is correct |
7 |
Correct |
8 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
9 ms |
9856 KB |
Output is correct |
11 |
Correct |
8 ms |
9856 KB |
Output is correct |
12 |
Correct |
9 ms |
9856 KB |
Output is correct |
13 |
Correct |
7 ms |
9856 KB |
Output is correct |
14 |
Correct |
7 ms |
9856 KB |
Output is correct |
15 |
Correct |
8 ms |
9856 KB |
Output is correct |
16 |
Correct |
8 ms |
9856 KB |
Output is correct |
17 |
Correct |
7 ms |
9856 KB |
Output is correct |
18 |
Correct |
8 ms |
9856 KB |
Output is correct |
19 |
Correct |
7 ms |
9856 KB |
Output is correct |
20 |
Correct |
7 ms |
9856 KB |
Output is correct |
21 |
Correct |
8 ms |
9856 KB |
Output is correct |
22 |
Correct |
8 ms |
9856 KB |
Output is correct |
23 |
Correct |
7 ms |
9856 KB |
Output is correct |
24 |
Correct |
8 ms |
9856 KB |
Output is correct |
25 |
Correct |
8 ms |
9856 KB |
Output is correct |
26 |
Correct |
8 ms |
9856 KB |
Output is correct |
27 |
Correct |
8 ms |
9856 KB |
Output is correct |
28 |
Correct |
8 ms |
9856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9856 KB |
Output is correct |
7 |
Correct |
8 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
86 ms |
18416 KB |
Output is correct |
11 |
Correct |
110 ms |
20340 KB |
Output is correct |
12 |
Correct |
113 ms |
20208 KB |
Output is correct |
13 |
Correct |
117 ms |
20848 KB |
Output is correct |
14 |
Correct |
77 ms |
19312 KB |
Output is correct |
15 |
Correct |
9 ms |
9856 KB |
Output is correct |
16 |
Correct |
8 ms |
9856 KB |
Output is correct |
17 |
Correct |
9 ms |
9856 KB |
Output is correct |
18 |
Correct |
7 ms |
9856 KB |
Output is correct |
19 |
Correct |
7 ms |
9856 KB |
Output is correct |
20 |
Correct |
8 ms |
9856 KB |
Output is correct |
21 |
Correct |
8 ms |
9856 KB |
Output is correct |
22 |
Correct |
7 ms |
9856 KB |
Output is correct |
23 |
Correct |
8 ms |
9856 KB |
Output is correct |
24 |
Correct |
17 ms |
11136 KB |
Output is correct |
25 |
Correct |
110 ms |
20464 KB |
Output is correct |
26 |
Correct |
112 ms |
20596 KB |
Output is correct |
27 |
Correct |
109 ms |
20464 KB |
Output is correct |
28 |
Correct |
108 ms |
20336 KB |
Output is correct |
29 |
Correct |
112 ms |
20208 KB |
Output is correct |
30 |
Correct |
97 ms |
19184 KB |
Output is correct |
31 |
Correct |
116 ms |
20852 KB |
Output is correct |
32 |
Correct |
110 ms |
20468 KB |
Output is correct |
33 |
Correct |
70 ms |
19440 KB |
Output is correct |
34 |
Correct |
114 ms |
20464 KB |
Output is correct |
35 |
Correct |
7 ms |
9856 KB |
Output is correct |
36 |
Correct |
7 ms |
9856 KB |
Output is correct |
37 |
Correct |
8 ms |
9856 KB |
Output is correct |
38 |
Correct |
8 ms |
9856 KB |
Output is correct |
39 |
Correct |
7 ms |
9856 KB |
Output is correct |
40 |
Correct |
8 ms |
9856 KB |
Output is correct |
41 |
Correct |
8 ms |
9856 KB |
Output is correct |
42 |
Correct |
8 ms |
9856 KB |
Output is correct |
43 |
Correct |
8 ms |
9856 KB |
Output is correct |
44 |
Correct |
8 ms |
9856 KB |
Output is correct |
45 |
Correct |
129 ms |
23288 KB |
Output is correct |
46 |
Correct |
114 ms |
20592 KB |
Output is correct |
47 |
Correct |
114 ms |
20468 KB |
Output is correct |
48 |
Correct |
108 ms |
20924 KB |
Output is correct |
49 |
Correct |
101 ms |
21100 KB |
Output is correct |
50 |
Correct |
83 ms |
18536 KB |
Output is correct |
51 |
Correct |
106 ms |
21232 KB |
Output is correct |
52 |
Correct |
170 ms |
25196 KB |
Output is correct |
53 |
Correct |
185 ms |
26220 KB |
Output is correct |
54 |
Correct |
190 ms |
28012 KB |
Output is correct |
55 |
Correct |
72 ms |
19312 KB |
Output is correct |
56 |
Correct |
165 ms |
25580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9856 KB |
Output is correct |
6 |
Correct |
8 ms |
9856 KB |
Output is correct |
7 |
Correct |
8 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
86 ms |
18416 KB |
Output is correct |
10 |
Correct |
110 ms |
20340 KB |
Output is correct |
11 |
Correct |
113 ms |
20208 KB |
Output is correct |
12 |
Correct |
117 ms |
20848 KB |
Output is correct |
13 |
Correct |
77 ms |
19312 KB |
Output is correct |
14 |
Correct |
106 ms |
18800 KB |
Output is correct |
15 |
Correct |
306 ms |
24572 KB |
Output is correct |
16 |
Correct |
302 ms |
24352 KB |
Output is correct |
17 |
Correct |
307 ms |
24920 KB |
Output is correct |
18 |
Correct |
239 ms |
23640 KB |
Output is correct |
19 |
Correct |
322 ms |
23828 KB |
Output is correct |
20 |
Correct |
329 ms |
23896 KB |
Output is correct |
21 |
Correct |
343 ms |
24328 KB |
Output is correct |
22 |
Correct |
316 ms |
23896 KB |
Output is correct |
23 |
Correct |
330 ms |
24176 KB |
Output is correct |
24 |
Correct |
325 ms |
23828 KB |
Output is correct |
25 |
Correct |
327 ms |
23888 KB |
Output is correct |
26 |
Correct |
321 ms |
23852 KB |
Output is correct |
27 |
Correct |
9 ms |
9856 KB |
Output is correct |
28 |
Correct |
8 ms |
9856 KB |
Output is correct |
29 |
Correct |
9 ms |
9856 KB |
Output is correct |
30 |
Correct |
7 ms |
9856 KB |
Output is correct |
31 |
Correct |
7 ms |
9856 KB |
Output is correct |
32 |
Correct |
8 ms |
9856 KB |
Output is correct |
33 |
Correct |
8 ms |
9856 KB |
Output is correct |
34 |
Correct |
7 ms |
9856 KB |
Output is correct |
35 |
Correct |
8 ms |
9856 KB |
Output is correct |
36 |
Correct |
17 ms |
11136 KB |
Output is correct |
37 |
Correct |
110 ms |
20464 KB |
Output is correct |
38 |
Correct |
112 ms |
20596 KB |
Output is correct |
39 |
Correct |
109 ms |
20464 KB |
Output is correct |
40 |
Correct |
108 ms |
20336 KB |
Output is correct |
41 |
Correct |
112 ms |
20208 KB |
Output is correct |
42 |
Correct |
97 ms |
19184 KB |
Output is correct |
43 |
Correct |
116 ms |
20852 KB |
Output is correct |
44 |
Correct |
110 ms |
20468 KB |
Output is correct |
45 |
Correct |
70 ms |
19440 KB |
Output is correct |
46 |
Correct |
114 ms |
20464 KB |
Output is correct |
47 |
Correct |
34 ms |
11668 KB |
Output is correct |
48 |
Correct |
310 ms |
25592 KB |
Output is correct |
49 |
Correct |
311 ms |
25432 KB |
Output is correct |
50 |
Correct |
315 ms |
25432 KB |
Output is correct |
51 |
Correct |
323 ms |
25444 KB |
Output is correct |
52 |
Correct |
308 ms |
24736 KB |
Output is correct |
53 |
Correct |
279 ms |
22760 KB |
Output is correct |
54 |
Correct |
315 ms |
26456 KB |
Output is correct |
55 |
Correct |
304 ms |
25432 KB |
Output is correct |
56 |
Correct |
285 ms |
24152 KB |
Output is correct |
57 |
Correct |
337 ms |
26072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9856 KB |
Output is correct |
7 |
Correct |
8 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
86 ms |
18416 KB |
Output is correct |
11 |
Correct |
110 ms |
20340 KB |
Output is correct |
12 |
Correct |
113 ms |
20208 KB |
Output is correct |
13 |
Correct |
117 ms |
20848 KB |
Output is correct |
14 |
Correct |
77 ms |
19312 KB |
Output is correct |
15 |
Correct |
106 ms |
18800 KB |
Output is correct |
16 |
Correct |
306 ms |
24572 KB |
Output is correct |
17 |
Correct |
302 ms |
24352 KB |
Output is correct |
18 |
Correct |
307 ms |
24920 KB |
Output is correct |
19 |
Correct |
239 ms |
23640 KB |
Output is correct |
20 |
Correct |
322 ms |
23828 KB |
Output is correct |
21 |
Correct |
329 ms |
23896 KB |
Output is correct |
22 |
Correct |
343 ms |
24328 KB |
Output is correct |
23 |
Correct |
316 ms |
23896 KB |
Output is correct |
24 |
Correct |
330 ms |
24176 KB |
Output is correct |
25 |
Correct |
325 ms |
23828 KB |
Output is correct |
26 |
Correct |
327 ms |
23888 KB |
Output is correct |
27 |
Correct |
321 ms |
23852 KB |
Output is correct |
28 |
Correct |
9 ms |
9856 KB |
Output is correct |
29 |
Correct |
8 ms |
9856 KB |
Output is correct |
30 |
Correct |
9 ms |
9856 KB |
Output is correct |
31 |
Correct |
7 ms |
9856 KB |
Output is correct |
32 |
Correct |
7 ms |
9856 KB |
Output is correct |
33 |
Correct |
8 ms |
9856 KB |
Output is correct |
34 |
Correct |
8 ms |
9856 KB |
Output is correct |
35 |
Correct |
7 ms |
9856 KB |
Output is correct |
36 |
Correct |
8 ms |
9856 KB |
Output is correct |
37 |
Correct |
17 ms |
11136 KB |
Output is correct |
38 |
Correct |
110 ms |
20464 KB |
Output is correct |
39 |
Correct |
112 ms |
20596 KB |
Output is correct |
40 |
Correct |
109 ms |
20464 KB |
Output is correct |
41 |
Correct |
108 ms |
20336 KB |
Output is correct |
42 |
Correct |
112 ms |
20208 KB |
Output is correct |
43 |
Correct |
97 ms |
19184 KB |
Output is correct |
44 |
Correct |
116 ms |
20852 KB |
Output is correct |
45 |
Correct |
110 ms |
20468 KB |
Output is correct |
46 |
Correct |
70 ms |
19440 KB |
Output is correct |
47 |
Correct |
114 ms |
20464 KB |
Output is correct |
48 |
Correct |
34 ms |
11668 KB |
Output is correct |
49 |
Correct |
310 ms |
25592 KB |
Output is correct |
50 |
Correct |
311 ms |
25432 KB |
Output is correct |
51 |
Correct |
315 ms |
25432 KB |
Output is correct |
52 |
Correct |
323 ms |
25444 KB |
Output is correct |
53 |
Correct |
308 ms |
24736 KB |
Output is correct |
54 |
Correct |
279 ms |
22760 KB |
Output is correct |
55 |
Correct |
315 ms |
26456 KB |
Output is correct |
56 |
Correct |
304 ms |
25432 KB |
Output is correct |
57 |
Correct |
285 ms |
24152 KB |
Output is correct |
58 |
Correct |
337 ms |
26072 KB |
Output is correct |
59 |
Correct |
150 ms |
16632 KB |
Output is correct |
60 |
Correct |
299 ms |
25588 KB |
Output is correct |
61 |
Correct |
302 ms |
25648 KB |
Output is correct |
62 |
Correct |
320 ms |
26368 KB |
Output is correct |
63 |
Correct |
252 ms |
25048 KB |
Output is correct |
64 |
Correct |
7 ms |
9856 KB |
Output is correct |
65 |
Correct |
7 ms |
9856 KB |
Output is correct |
66 |
Correct |
8 ms |
9856 KB |
Output is correct |
67 |
Correct |
8 ms |
9856 KB |
Output is correct |
68 |
Correct |
7 ms |
9856 KB |
Output is correct |
69 |
Correct |
8 ms |
9856 KB |
Output is correct |
70 |
Correct |
8 ms |
9856 KB |
Output is correct |
71 |
Correct |
8 ms |
9856 KB |
Output is correct |
72 |
Correct |
8 ms |
9856 KB |
Output is correct |
73 |
Correct |
8 ms |
9856 KB |
Output is correct |
74 |
Correct |
129 ms |
23288 KB |
Output is correct |
75 |
Correct |
114 ms |
20592 KB |
Output is correct |
76 |
Correct |
114 ms |
20468 KB |
Output is correct |
77 |
Correct |
108 ms |
20924 KB |
Output is correct |
78 |
Correct |
101 ms |
21100 KB |
Output is correct |
79 |
Correct |
83 ms |
18536 KB |
Output is correct |
80 |
Correct |
106 ms |
21232 KB |
Output is correct |
81 |
Correct |
170 ms |
25196 KB |
Output is correct |
82 |
Correct |
185 ms |
26220 KB |
Output is correct |
83 |
Correct |
190 ms |
28012 KB |
Output is correct |
84 |
Correct |
72 ms |
19312 KB |
Output is correct |
85 |
Correct |
165 ms |
25580 KB |
Output is correct |
86 |
Correct |
114 ms |
15400 KB |
Output is correct |
87 |
Correct |
325 ms |
25432 KB |
Output is correct |
88 |
Correct |
318 ms |
25432 KB |
Output is correct |
89 |
Correct |
429 ms |
25360 KB |
Output is correct |
90 |
Correct |
369 ms |
25020 KB |
Output is correct |
91 |
Correct |
409 ms |
24940 KB |
Output is correct |
92 |
Correct |
477 ms |
26044 KB |
Output is correct |
93 |
Correct |
488 ms |
29752 KB |
Output is correct |
94 |
Correct |
521 ms |
30928 KB |
Output is correct |
95 |
Correct |
443 ms |
31672 KB |
Output is correct |
96 |
Correct |
291 ms |
24280 KB |
Output is correct |
97 |
Correct |
475 ms |
28076 KB |
Output is correct |