#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
const int maxn = 1e6 + 10;
int n, m;
vector<int> g[maxn];
vector<pair<int,pair<int,int>>> edges;
int head;
int par[maxn];
int indx[maxn];
int deg[maxn];
bool good[maxn];
int cost[maxn];
int parent(int x) {
if (par[x]==x) return x;
return par[x]=parent(par[x]);
}
const int LOG = 20;
int st[LOG+1][maxn];
int ht[maxn];
void dfs(int at, int p=-1) {
for (int j=1; j<LOG; j++) {
st[j][at]=st[j-1][st[j-1][at]];
}
for (int to: g[at]) {
if (to == p) continue;
st[0][to]=at;
ht[to] = 1+ht[at];
dfs(to, at);
}
}
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n=N;
m=M;
head = n;
for (int i=0; i<M; i++) {
edges.push_back({W[i],{U[i],V[i]}});
}
sort(edges.begin(),edges.end());
for (int i=0; i<n; i++) {
par[i]=i;
indx[i]=i;
deg[i]=0;
good[i]=false;
}
for (auto ed: edges) {
int w = ed.first;
int U = ed.second.first;
int V = ed.second.second;
int u = parent(U);
int v = parent(V);
deg[U]++;
deg[V]++;
if (u==v) {
if (!good[indx[u]]) {
g[head].push_back(indx[u]);
good[head]=true;
cost[head]=w;
indx[u] = head++;
}
} else {
bool ok = (good[indx[u]] || good[indx[v]] || deg[U]>2 || deg[V]>2);
par[v]=u;
g[head].push_back(indx[u]);
g[head].push_back(indx[v]);
good[head] = ok;
cost[head] = w;
indx[u] = head++;
}
}
st[0][head-1] = head-1;
dfs(head-1);
}
int lca(int u, int v) {
if (ht[u]<ht[v]) {
swap(u,v);
}
int dx = ht[u]-ht[v];
for (int j=LOG-1; j>=0; j--) {
if (dx>>j&1) {
u=st[j][u];
}
}
if (u==v) return v;
for (int j=LOG-1; j>=0; j--) {
if (st[j][u]!=st[j][v]) {
u=st[j][u];
v=st[j][v];
}
}
return st[0][v];
}
int getMinimumFuelCapacity(int u, int v) {
if (!good[head-1]) return -1;
int mid = lca(u,v);
if (good[mid]) {
return cost[mid];
}
for (int j=LOG-1; j>=0; j--) {
if (ht[mid] < (1<<j)) continue;
if (!good[st[j][mid]]) {
mid=st[j][mid];
}
}
return cost[st[0][mid]];
}
/*
int main() {
// init(4,4,{0,1,2,3},{1,2,3,0},{1,1,1,1});
// cout<<getMinimumFuelCapacity(1, 2)<<endl;
init(5, 6, {0, 0, 1, 1, 1, 2}, {1, 2, 2, 3, 4, 3}, {4, 4, 1, 2, 10, 3});
cout<<getMinimumFuelCapacity(1, 2)<<endl;
cout<<getMinimumFuelCapacity(2, 4)<<endl;
cout<<getMinimumFuelCapacity(0, 1)<<endl;
return 0;
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
16 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
16 ms |
24172 KB |
Output is correct |
5 |
Correct |
17 ms |
24300 KB |
Output is correct |
6 |
Correct |
17 ms |
24300 KB |
Output is correct |
7 |
Correct |
17 ms |
24300 KB |
Output is correct |
8 |
Correct |
17 ms |
24428 KB |
Output is correct |
9 |
Correct |
140 ms |
43616 KB |
Output is correct |
10 |
Correct |
171 ms |
47840 KB |
Output is correct |
11 |
Correct |
166 ms |
47496 KB |
Output is correct |
12 |
Correct |
173 ms |
48992 KB |
Output is correct |
13 |
Correct |
149 ms |
52832 KB |
Output is correct |
14 |
Correct |
145 ms |
43744 KB |
Output is correct |
15 |
Correct |
231 ms |
49900 KB |
Output is correct |
16 |
Correct |
229 ms |
49296 KB |
Output is correct |
17 |
Correct |
240 ms |
50632 KB |
Output is correct |
18 |
Correct |
213 ms |
54696 KB |
Output is correct |
19 |
Correct |
124 ms |
31532 KB |
Output is correct |
20 |
Correct |
376 ms |
51168 KB |
Output is correct |
21 |
Correct |
369 ms |
50588 KB |
Output is correct |
22 |
Correct |
393 ms |
52048 KB |
Output is correct |
23 |
Correct |
466 ms |
56008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
16 ms |
24044 KB |
Output is correct |
3 |
Correct |
399 ms |
57988 KB |
Output is correct |
4 |
Correct |
398 ms |
59592 KB |
Output is correct |
5 |
Correct |
420 ms |
58652 KB |
Output is correct |
6 |
Correct |
387 ms |
59336 KB |
Output is correct |
7 |
Correct |
424 ms |
59232 KB |
Output is correct |
8 |
Correct |
405 ms |
57744 KB |
Output is correct |
9 |
Correct |
412 ms |
58976 KB |
Output is correct |
10 |
Correct |
394 ms |
57500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
16 ms |
24044 KB |
Output is correct |
5 |
Correct |
16 ms |
24172 KB |
Output is correct |
6 |
Correct |
17 ms |
24300 KB |
Output is correct |
7 |
Correct |
17 ms |
24300 KB |
Output is correct |
8 |
Correct |
17 ms |
24300 KB |
Output is correct |
9 |
Correct |
17 ms |
24428 KB |
Output is correct |
10 |
Correct |
17 ms |
24300 KB |
Output is correct |
11 |
Correct |
17 ms |
24300 KB |
Output is correct |
12 |
Correct |
17 ms |
24300 KB |
Output is correct |
13 |
Correct |
17 ms |
24172 KB |
Output is correct |
14 |
Correct |
17 ms |
24172 KB |
Output is correct |
15 |
Correct |
17 ms |
24300 KB |
Output is correct |
16 |
Correct |
17 ms |
24300 KB |
Output is correct |
17 |
Correct |
17 ms |
24300 KB |
Output is correct |
18 |
Correct |
17 ms |
24300 KB |
Output is correct |
19 |
Correct |
17 ms |
24172 KB |
Output is correct |
20 |
Correct |
18 ms |
24300 KB |
Output is correct |
21 |
Correct |
18 ms |
24428 KB |
Output is correct |
22 |
Correct |
18 ms |
24172 KB |
Output is correct |
23 |
Correct |
18 ms |
24172 KB |
Output is correct |
24 |
Correct |
18 ms |
24320 KB |
Output is correct |
25 |
Correct |
17 ms |
24300 KB |
Output is correct |
26 |
Correct |
18 ms |
24300 KB |
Output is correct |
27 |
Correct |
17 ms |
24300 KB |
Output is correct |
28 |
Correct |
18 ms |
24336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
16 ms |
24044 KB |
Output is correct |
5 |
Correct |
16 ms |
24172 KB |
Output is correct |
6 |
Correct |
17 ms |
24300 KB |
Output is correct |
7 |
Correct |
17 ms |
24300 KB |
Output is correct |
8 |
Correct |
17 ms |
24300 KB |
Output is correct |
9 |
Correct |
17 ms |
24428 KB |
Output is correct |
10 |
Correct |
140 ms |
43616 KB |
Output is correct |
11 |
Correct |
171 ms |
47840 KB |
Output is correct |
12 |
Correct |
166 ms |
47496 KB |
Output is correct |
13 |
Correct |
173 ms |
48992 KB |
Output is correct |
14 |
Correct |
149 ms |
52832 KB |
Output is correct |
15 |
Correct |
17 ms |
24300 KB |
Output is correct |
16 |
Correct |
17 ms |
24300 KB |
Output is correct |
17 |
Correct |
17 ms |
24300 KB |
Output is correct |
18 |
Correct |
17 ms |
24172 KB |
Output is correct |
19 |
Correct |
17 ms |
24172 KB |
Output is correct |
20 |
Correct |
17 ms |
24300 KB |
Output is correct |
21 |
Correct |
17 ms |
24300 KB |
Output is correct |
22 |
Correct |
17 ms |
24300 KB |
Output is correct |
23 |
Correct |
17 ms |
24300 KB |
Output is correct |
24 |
Correct |
29 ms |
27436 KB |
Output is correct |
25 |
Correct |
173 ms |
48864 KB |
Output is correct |
26 |
Correct |
171 ms |
48864 KB |
Output is correct |
27 |
Correct |
176 ms |
48864 KB |
Output is correct |
28 |
Correct |
172 ms |
48480 KB |
Output is correct |
29 |
Correct |
170 ms |
48512 KB |
Output is correct |
30 |
Correct |
157 ms |
46432 KB |
Output is correct |
31 |
Correct |
177 ms |
48852 KB |
Output is correct |
32 |
Correct |
173 ms |
48992 KB |
Output is correct |
33 |
Correct |
144 ms |
53984 KB |
Output is correct |
34 |
Correct |
178 ms |
48900 KB |
Output is correct |
35 |
Correct |
17 ms |
24172 KB |
Output is correct |
36 |
Correct |
18 ms |
24300 KB |
Output is correct |
37 |
Correct |
18 ms |
24428 KB |
Output is correct |
38 |
Correct |
18 ms |
24172 KB |
Output is correct |
39 |
Correct |
18 ms |
24172 KB |
Output is correct |
40 |
Correct |
18 ms |
24320 KB |
Output is correct |
41 |
Correct |
17 ms |
24300 KB |
Output is correct |
42 |
Correct |
18 ms |
24300 KB |
Output is correct |
43 |
Correct |
17 ms |
24300 KB |
Output is correct |
44 |
Correct |
18 ms |
24336 KB |
Output is correct |
45 |
Correct |
171 ms |
47836 KB |
Output is correct |
46 |
Correct |
172 ms |
48900 KB |
Output is correct |
47 |
Correct |
174 ms |
48896 KB |
Output is correct |
48 |
Correct |
169 ms |
50784 KB |
Output is correct |
49 |
Correct |
86 ms |
30816 KB |
Output is correct |
50 |
Correct |
74 ms |
30048 KB |
Output is correct |
51 |
Correct |
144 ms |
43744 KB |
Output is correct |
52 |
Correct |
208 ms |
51036 KB |
Output is correct |
53 |
Correct |
239 ms |
56540 KB |
Output is correct |
54 |
Correct |
223 ms |
52440 KB |
Output is correct |
55 |
Correct |
144 ms |
53600 KB |
Output is correct |
56 |
Correct |
210 ms |
53724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
16 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
16 ms |
24172 KB |
Output is correct |
5 |
Correct |
17 ms |
24300 KB |
Output is correct |
6 |
Correct |
17 ms |
24300 KB |
Output is correct |
7 |
Correct |
17 ms |
24300 KB |
Output is correct |
8 |
Correct |
17 ms |
24428 KB |
Output is correct |
9 |
Correct |
140 ms |
43616 KB |
Output is correct |
10 |
Correct |
171 ms |
47840 KB |
Output is correct |
11 |
Correct |
166 ms |
47496 KB |
Output is correct |
12 |
Correct |
173 ms |
48992 KB |
Output is correct |
13 |
Correct |
149 ms |
52832 KB |
Output is correct |
14 |
Correct |
145 ms |
43744 KB |
Output is correct |
15 |
Correct |
231 ms |
49900 KB |
Output is correct |
16 |
Correct |
229 ms |
49296 KB |
Output is correct |
17 |
Correct |
240 ms |
50632 KB |
Output is correct |
18 |
Correct |
213 ms |
54696 KB |
Output is correct |
19 |
Correct |
399 ms |
57988 KB |
Output is correct |
20 |
Correct |
398 ms |
59592 KB |
Output is correct |
21 |
Correct |
420 ms |
58652 KB |
Output is correct |
22 |
Correct |
387 ms |
59336 KB |
Output is correct |
23 |
Correct |
424 ms |
59232 KB |
Output is correct |
24 |
Correct |
405 ms |
57744 KB |
Output is correct |
25 |
Correct |
412 ms |
58976 KB |
Output is correct |
26 |
Correct |
394 ms |
57500 KB |
Output is correct |
27 |
Correct |
17 ms |
24300 KB |
Output is correct |
28 |
Correct |
17 ms |
24300 KB |
Output is correct |
29 |
Correct |
17 ms |
24300 KB |
Output is correct |
30 |
Correct |
17 ms |
24172 KB |
Output is correct |
31 |
Correct |
17 ms |
24172 KB |
Output is correct |
32 |
Correct |
17 ms |
24300 KB |
Output is correct |
33 |
Correct |
17 ms |
24300 KB |
Output is correct |
34 |
Correct |
17 ms |
24300 KB |
Output is correct |
35 |
Correct |
17 ms |
24300 KB |
Output is correct |
36 |
Correct |
29 ms |
27436 KB |
Output is correct |
37 |
Correct |
173 ms |
48864 KB |
Output is correct |
38 |
Correct |
171 ms |
48864 KB |
Output is correct |
39 |
Correct |
176 ms |
48864 KB |
Output is correct |
40 |
Correct |
172 ms |
48480 KB |
Output is correct |
41 |
Correct |
170 ms |
48512 KB |
Output is correct |
42 |
Correct |
157 ms |
46432 KB |
Output is correct |
43 |
Correct |
177 ms |
48852 KB |
Output is correct |
44 |
Correct |
173 ms |
48992 KB |
Output is correct |
45 |
Correct |
144 ms |
53984 KB |
Output is correct |
46 |
Correct |
178 ms |
48900 KB |
Output is correct |
47 |
Correct |
38 ms |
27436 KB |
Output is correct |
48 |
Correct |
371 ms |
51528 KB |
Output is correct |
49 |
Correct |
355 ms |
51400 KB |
Output is correct |
50 |
Correct |
342 ms |
51528 KB |
Output is correct |
51 |
Correct |
340 ms |
51284 KB |
Output is correct |
52 |
Correct |
318 ms |
49936 KB |
Output is correct |
53 |
Correct |
263 ms |
44116 KB |
Output is correct |
54 |
Correct |
368 ms |
52168 KB |
Output is correct |
55 |
Correct |
377 ms |
51528 KB |
Output is correct |
56 |
Correct |
426 ms |
55496 KB |
Output is correct |
57 |
Correct |
342 ms |
52168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
16 ms |
24044 KB |
Output is correct |
5 |
Correct |
16 ms |
24172 KB |
Output is correct |
6 |
Correct |
17 ms |
24300 KB |
Output is correct |
7 |
Correct |
17 ms |
24300 KB |
Output is correct |
8 |
Correct |
17 ms |
24300 KB |
Output is correct |
9 |
Correct |
17 ms |
24428 KB |
Output is correct |
10 |
Correct |
140 ms |
43616 KB |
Output is correct |
11 |
Correct |
171 ms |
47840 KB |
Output is correct |
12 |
Correct |
166 ms |
47496 KB |
Output is correct |
13 |
Correct |
173 ms |
48992 KB |
Output is correct |
14 |
Correct |
149 ms |
52832 KB |
Output is correct |
15 |
Correct |
145 ms |
43744 KB |
Output is correct |
16 |
Correct |
231 ms |
49900 KB |
Output is correct |
17 |
Correct |
229 ms |
49296 KB |
Output is correct |
18 |
Correct |
240 ms |
50632 KB |
Output is correct |
19 |
Correct |
213 ms |
54696 KB |
Output is correct |
20 |
Correct |
399 ms |
57988 KB |
Output is correct |
21 |
Correct |
398 ms |
59592 KB |
Output is correct |
22 |
Correct |
420 ms |
58652 KB |
Output is correct |
23 |
Correct |
387 ms |
59336 KB |
Output is correct |
24 |
Correct |
424 ms |
59232 KB |
Output is correct |
25 |
Correct |
405 ms |
57744 KB |
Output is correct |
26 |
Correct |
412 ms |
58976 KB |
Output is correct |
27 |
Correct |
394 ms |
57500 KB |
Output is correct |
28 |
Correct |
17 ms |
24300 KB |
Output is correct |
29 |
Correct |
17 ms |
24300 KB |
Output is correct |
30 |
Correct |
17 ms |
24300 KB |
Output is correct |
31 |
Correct |
17 ms |
24172 KB |
Output is correct |
32 |
Correct |
17 ms |
24172 KB |
Output is correct |
33 |
Correct |
17 ms |
24300 KB |
Output is correct |
34 |
Correct |
17 ms |
24300 KB |
Output is correct |
35 |
Correct |
17 ms |
24300 KB |
Output is correct |
36 |
Correct |
17 ms |
24300 KB |
Output is correct |
37 |
Correct |
29 ms |
27436 KB |
Output is correct |
38 |
Correct |
173 ms |
48864 KB |
Output is correct |
39 |
Correct |
171 ms |
48864 KB |
Output is correct |
40 |
Correct |
176 ms |
48864 KB |
Output is correct |
41 |
Correct |
172 ms |
48480 KB |
Output is correct |
42 |
Correct |
170 ms |
48512 KB |
Output is correct |
43 |
Correct |
157 ms |
46432 KB |
Output is correct |
44 |
Correct |
177 ms |
48852 KB |
Output is correct |
45 |
Correct |
173 ms |
48992 KB |
Output is correct |
46 |
Correct |
144 ms |
53984 KB |
Output is correct |
47 |
Correct |
178 ms |
48900 KB |
Output is correct |
48 |
Correct |
38 ms |
27436 KB |
Output is correct |
49 |
Correct |
371 ms |
51528 KB |
Output is correct |
50 |
Correct |
355 ms |
51400 KB |
Output is correct |
51 |
Correct |
342 ms |
51528 KB |
Output is correct |
52 |
Correct |
340 ms |
51284 KB |
Output is correct |
53 |
Correct |
318 ms |
49936 KB |
Output is correct |
54 |
Correct |
263 ms |
44116 KB |
Output is correct |
55 |
Correct |
368 ms |
52168 KB |
Output is correct |
56 |
Correct |
377 ms |
51528 KB |
Output is correct |
57 |
Correct |
426 ms |
55496 KB |
Output is correct |
58 |
Correct |
342 ms |
52168 KB |
Output is correct |
59 |
Correct |
124 ms |
31532 KB |
Output is correct |
60 |
Correct |
376 ms |
51168 KB |
Output is correct |
61 |
Correct |
369 ms |
50588 KB |
Output is correct |
62 |
Correct |
393 ms |
52048 KB |
Output is correct |
63 |
Correct |
466 ms |
56008 KB |
Output is correct |
64 |
Correct |
17 ms |
24172 KB |
Output is correct |
65 |
Correct |
18 ms |
24300 KB |
Output is correct |
66 |
Correct |
18 ms |
24428 KB |
Output is correct |
67 |
Correct |
18 ms |
24172 KB |
Output is correct |
68 |
Correct |
18 ms |
24172 KB |
Output is correct |
69 |
Correct |
18 ms |
24320 KB |
Output is correct |
70 |
Correct |
17 ms |
24300 KB |
Output is correct |
71 |
Correct |
18 ms |
24300 KB |
Output is correct |
72 |
Correct |
17 ms |
24300 KB |
Output is correct |
73 |
Correct |
18 ms |
24336 KB |
Output is correct |
74 |
Correct |
171 ms |
47836 KB |
Output is correct |
75 |
Correct |
172 ms |
48900 KB |
Output is correct |
76 |
Correct |
174 ms |
48896 KB |
Output is correct |
77 |
Correct |
169 ms |
50784 KB |
Output is correct |
78 |
Correct |
86 ms |
30816 KB |
Output is correct |
79 |
Correct |
74 ms |
30048 KB |
Output is correct |
80 |
Correct |
144 ms |
43744 KB |
Output is correct |
81 |
Correct |
208 ms |
51036 KB |
Output is correct |
82 |
Correct |
239 ms |
56540 KB |
Output is correct |
83 |
Correct |
223 ms |
52440 KB |
Output is correct |
84 |
Correct |
144 ms |
53600 KB |
Output is correct |
85 |
Correct |
210 ms |
53724 KB |
Output is correct |
86 |
Correct |
98 ms |
33688 KB |
Output is correct |
87 |
Correct |
341 ms |
51656 KB |
Output is correct |
88 |
Correct |
343 ms |
51400 KB |
Output is correct |
89 |
Correct |
396 ms |
51712 KB |
Output is correct |
90 |
Correct |
187 ms |
32480 KB |
Output is correct |
91 |
Correct |
204 ms |
34556 KB |
Output is correct |
92 |
Correct |
336 ms |
46524 KB |
Output is correct |
93 |
Correct |
383 ms |
53652 KB |
Output is correct |
94 |
Correct |
493 ms |
57532 KB |
Output is correct |
95 |
Correct |
430 ms |
53980 KB |
Output is correct |
96 |
Correct |
431 ms |
55880 KB |
Output is correct |
97 |
Correct |
428 ms |
54936 KB |
Output is correct |