#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);
if (u==v) {
if (!good[indx[u]]) {
g[head].push_back(indx[u]);
good[head]=true;
cost[head]=w;
indx[u] = head++;
}
} else {
deg[U]++;
deg[V]++;
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 |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
17 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 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 |
18 ms |
24300 KB |
Output is correct |
8 |
Correct |
17 ms |
24300 KB |
Output is correct |
9 |
Correct |
142 ms |
43616 KB |
Output is correct |
10 |
Correct |
170 ms |
47840 KB |
Output is correct |
11 |
Correct |
170 ms |
47584 KB |
Output is correct |
12 |
Correct |
176 ms |
48864 KB |
Output is correct |
13 |
Correct |
147 ms |
52832 KB |
Output is correct |
14 |
Correct |
149 ms |
43616 KB |
Output is correct |
15 |
Correct |
232 ms |
49900 KB |
Output is correct |
16 |
Correct |
232 ms |
49296 KB |
Output is correct |
17 |
Correct |
242 ms |
50760 KB |
Output is correct |
18 |
Correct |
214 ms |
54600 KB |
Output is correct |
19 |
Correct |
120 ms |
31532 KB |
Output is correct |
20 |
Correct |
374 ms |
51168 KB |
Output is correct |
21 |
Correct |
382 ms |
50460 KB |
Output is correct |
22 |
Correct |
417 ms |
52168 KB |
Output is correct |
23 |
Correct |
486 ms |
56136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
393 ms |
58156 KB |
Output is correct |
4 |
Correct |
411 ms |
59592 KB |
Output is correct |
5 |
Correct |
415 ms |
58744 KB |
Output is correct |
6 |
Correct |
406 ms |
59336 KB |
Output is correct |
7 |
Correct |
415 ms |
59104 KB |
Output is correct |
8 |
Correct |
407 ms |
57872 KB |
Output is correct |
9 |
Correct |
410 ms |
58848 KB |
Output is correct |
10 |
Correct |
405 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 |
17 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 ms |
24044 KB |
Output is correct |
5 |
Correct |
17 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 |
18 ms |
24300 KB |
Output is correct |
9 |
Correct |
17 ms |
24300 KB |
Output is correct |
10 |
Correct |
18 ms |
24300 KB |
Output is correct |
11 |
Correct |
18 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 |
24300 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 |
17 ms |
24300 KB |
Output is correct |
21 |
Correct |
17 ms |
24300 KB |
Output is correct |
22 |
Correct |
17 ms |
24172 KB |
Output is correct |
23 |
Correct |
18 ms |
24192 KB |
Output is correct |
24 |
Correct |
17 ms |
24300 KB |
Output is correct |
25 |
Correct |
18 ms |
24428 KB |
Output is correct |
26 |
Correct |
18 ms |
24300 KB |
Output is correct |
27 |
Correct |
19 ms |
24300 KB |
Output is correct |
28 |
Correct |
18 ms |
24300 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 |
17 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 ms |
24044 KB |
Output is correct |
5 |
Correct |
17 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 |
18 ms |
24300 KB |
Output is correct |
9 |
Correct |
17 ms |
24300 KB |
Output is correct |
10 |
Correct |
142 ms |
43616 KB |
Output is correct |
11 |
Correct |
170 ms |
47840 KB |
Output is correct |
12 |
Correct |
170 ms |
47584 KB |
Output is correct |
13 |
Correct |
176 ms |
48864 KB |
Output is correct |
14 |
Correct |
147 ms |
52832 KB |
Output is correct |
15 |
Correct |
18 ms |
24300 KB |
Output is correct |
16 |
Correct |
18 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 |
24300 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 |
31 ms |
27692 KB |
Output is correct |
25 |
Correct |
178 ms |
50752 KB |
Output is correct |
26 |
Correct |
177 ms |
50784 KB |
Output is correct |
27 |
Correct |
178 ms |
50656 KB |
Output is correct |
28 |
Correct |
171 ms |
50344 KB |
Output is correct |
29 |
Correct |
172 ms |
50272 KB |
Output is correct |
30 |
Correct |
160 ms |
48084 KB |
Output is correct |
31 |
Correct |
179 ms |
51040 KB |
Output is correct |
32 |
Correct |
179 ms |
50656 KB |
Output is correct |
33 |
Correct |
149 ms |
55904 KB |
Output is correct |
34 |
Correct |
180 ms |
51040 KB |
Output is correct |
35 |
Correct |
17 ms |
24172 KB |
Output is correct |
36 |
Correct |
17 ms |
24300 KB |
Output is correct |
37 |
Correct |
17 ms |
24300 KB |
Output is correct |
38 |
Correct |
17 ms |
24172 KB |
Output is correct |
39 |
Correct |
18 ms |
24192 KB |
Output is correct |
40 |
Correct |
17 ms |
24300 KB |
Output is correct |
41 |
Correct |
18 ms |
24428 KB |
Output is correct |
42 |
Correct |
18 ms |
24300 KB |
Output is correct |
43 |
Correct |
19 ms |
24300 KB |
Output is correct |
44 |
Correct |
18 ms |
24300 KB |
Output is correct |
45 |
Correct |
169 ms |
51036 KB |
Output is correct |
46 |
Correct |
174 ms |
50784 KB |
Output is correct |
47 |
Correct |
174 ms |
50656 KB |
Output is correct |
48 |
Correct |
171 ms |
52704 KB |
Output is correct |
49 |
Correct |
86 ms |
33504 KB |
Output is correct |
50 |
Correct |
75 ms |
32352 KB |
Output is correct |
51 |
Correct |
142 ms |
46432 KB |
Output is correct |
52 |
Correct |
203 ms |
54492 KB |
Output is correct |
53 |
Correct |
240 ms |
60252 KB |
Output is correct |
54 |
Correct |
222 ms |
56408 KB |
Output is correct |
55 |
Correct |
145 ms |
55392 KB |
Output is correct |
56 |
Correct |
218 ms |
57436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24044 KB |
Output is correct |
2 |
Correct |
17 ms |
24044 KB |
Output is correct |
3 |
Correct |
17 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 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 |
18 ms |
24300 KB |
Output is correct |
8 |
Correct |
17 ms |
24300 KB |
Output is correct |
9 |
Correct |
142 ms |
43616 KB |
Output is correct |
10 |
Correct |
170 ms |
47840 KB |
Output is correct |
11 |
Correct |
170 ms |
47584 KB |
Output is correct |
12 |
Correct |
176 ms |
48864 KB |
Output is correct |
13 |
Correct |
147 ms |
52832 KB |
Output is correct |
14 |
Correct |
149 ms |
43616 KB |
Output is correct |
15 |
Correct |
232 ms |
49900 KB |
Output is correct |
16 |
Correct |
232 ms |
49296 KB |
Output is correct |
17 |
Correct |
242 ms |
50760 KB |
Output is correct |
18 |
Correct |
214 ms |
54600 KB |
Output is correct |
19 |
Correct |
393 ms |
58156 KB |
Output is correct |
20 |
Correct |
411 ms |
59592 KB |
Output is correct |
21 |
Correct |
415 ms |
58744 KB |
Output is correct |
22 |
Correct |
406 ms |
59336 KB |
Output is correct |
23 |
Correct |
415 ms |
59104 KB |
Output is correct |
24 |
Correct |
407 ms |
57872 KB |
Output is correct |
25 |
Correct |
410 ms |
58848 KB |
Output is correct |
26 |
Correct |
405 ms |
57500 KB |
Output is correct |
27 |
Correct |
18 ms |
24300 KB |
Output is correct |
28 |
Correct |
18 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 |
24300 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 |
31 ms |
27692 KB |
Output is correct |
37 |
Correct |
178 ms |
50752 KB |
Output is correct |
38 |
Correct |
177 ms |
50784 KB |
Output is correct |
39 |
Correct |
178 ms |
50656 KB |
Output is correct |
40 |
Correct |
171 ms |
50344 KB |
Output is correct |
41 |
Correct |
172 ms |
50272 KB |
Output is correct |
42 |
Correct |
160 ms |
48084 KB |
Output is correct |
43 |
Correct |
179 ms |
51040 KB |
Output is correct |
44 |
Correct |
179 ms |
50656 KB |
Output is correct |
45 |
Correct |
149 ms |
55904 KB |
Output is correct |
46 |
Correct |
180 ms |
51040 KB |
Output is correct |
47 |
Correct |
48 ms |
27948 KB |
Output is correct |
48 |
Correct |
370 ms |
55624 KB |
Output is correct |
49 |
Correct |
359 ms |
55624 KB |
Output is correct |
50 |
Correct |
353 ms |
55564 KB |
Output is correct |
51 |
Correct |
340 ms |
55508 KB |
Output is correct |
52 |
Correct |
326 ms |
53904 KB |
Output is correct |
53 |
Correct |
260 ms |
47828 KB |
Output is correct |
54 |
Correct |
379 ms |
56648 KB |
Output is correct |
55 |
Correct |
378 ms |
55752 KB |
Output is correct |
56 |
Correct |
435 ms |
59720 KB |
Output is correct |
57 |
Correct |
346 ms |
56648 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 |
17 ms |
24044 KB |
Output is correct |
4 |
Correct |
17 ms |
24044 KB |
Output is correct |
5 |
Correct |
17 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 |
18 ms |
24300 KB |
Output is correct |
9 |
Correct |
17 ms |
24300 KB |
Output is correct |
10 |
Correct |
142 ms |
43616 KB |
Output is correct |
11 |
Correct |
170 ms |
47840 KB |
Output is correct |
12 |
Correct |
170 ms |
47584 KB |
Output is correct |
13 |
Correct |
176 ms |
48864 KB |
Output is correct |
14 |
Correct |
147 ms |
52832 KB |
Output is correct |
15 |
Correct |
149 ms |
43616 KB |
Output is correct |
16 |
Correct |
232 ms |
49900 KB |
Output is correct |
17 |
Correct |
232 ms |
49296 KB |
Output is correct |
18 |
Correct |
242 ms |
50760 KB |
Output is correct |
19 |
Correct |
214 ms |
54600 KB |
Output is correct |
20 |
Correct |
393 ms |
58156 KB |
Output is correct |
21 |
Correct |
411 ms |
59592 KB |
Output is correct |
22 |
Correct |
415 ms |
58744 KB |
Output is correct |
23 |
Correct |
406 ms |
59336 KB |
Output is correct |
24 |
Correct |
415 ms |
59104 KB |
Output is correct |
25 |
Correct |
407 ms |
57872 KB |
Output is correct |
26 |
Correct |
410 ms |
58848 KB |
Output is correct |
27 |
Correct |
405 ms |
57500 KB |
Output is correct |
28 |
Correct |
18 ms |
24300 KB |
Output is correct |
29 |
Correct |
18 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 |
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 |
17 ms |
24300 KB |
Output is correct |
37 |
Correct |
31 ms |
27692 KB |
Output is correct |
38 |
Correct |
178 ms |
50752 KB |
Output is correct |
39 |
Correct |
177 ms |
50784 KB |
Output is correct |
40 |
Correct |
178 ms |
50656 KB |
Output is correct |
41 |
Correct |
171 ms |
50344 KB |
Output is correct |
42 |
Correct |
172 ms |
50272 KB |
Output is correct |
43 |
Correct |
160 ms |
48084 KB |
Output is correct |
44 |
Correct |
179 ms |
51040 KB |
Output is correct |
45 |
Correct |
179 ms |
50656 KB |
Output is correct |
46 |
Correct |
149 ms |
55904 KB |
Output is correct |
47 |
Correct |
180 ms |
51040 KB |
Output is correct |
48 |
Correct |
48 ms |
27948 KB |
Output is correct |
49 |
Correct |
370 ms |
55624 KB |
Output is correct |
50 |
Correct |
359 ms |
55624 KB |
Output is correct |
51 |
Correct |
353 ms |
55564 KB |
Output is correct |
52 |
Correct |
340 ms |
55508 KB |
Output is correct |
53 |
Correct |
326 ms |
53904 KB |
Output is correct |
54 |
Correct |
260 ms |
47828 KB |
Output is correct |
55 |
Correct |
379 ms |
56648 KB |
Output is correct |
56 |
Correct |
378 ms |
55752 KB |
Output is correct |
57 |
Correct |
435 ms |
59720 KB |
Output is correct |
58 |
Correct |
346 ms |
56648 KB |
Output is correct |
59 |
Correct |
120 ms |
31532 KB |
Output is correct |
60 |
Correct |
374 ms |
51168 KB |
Output is correct |
61 |
Correct |
382 ms |
50460 KB |
Output is correct |
62 |
Correct |
417 ms |
52168 KB |
Output is correct |
63 |
Correct |
486 ms |
56136 KB |
Output is correct |
64 |
Correct |
17 ms |
24172 KB |
Output is correct |
65 |
Correct |
17 ms |
24300 KB |
Output is correct |
66 |
Correct |
17 ms |
24300 KB |
Output is correct |
67 |
Correct |
17 ms |
24172 KB |
Output is correct |
68 |
Correct |
18 ms |
24192 KB |
Output is correct |
69 |
Correct |
17 ms |
24300 KB |
Output is correct |
70 |
Correct |
18 ms |
24428 KB |
Output is correct |
71 |
Correct |
18 ms |
24300 KB |
Output is correct |
72 |
Correct |
19 ms |
24300 KB |
Output is correct |
73 |
Correct |
18 ms |
24300 KB |
Output is correct |
74 |
Correct |
169 ms |
51036 KB |
Output is correct |
75 |
Correct |
174 ms |
50784 KB |
Output is correct |
76 |
Correct |
174 ms |
50656 KB |
Output is correct |
77 |
Correct |
171 ms |
52704 KB |
Output is correct |
78 |
Correct |
86 ms |
33504 KB |
Output is correct |
79 |
Correct |
75 ms |
32352 KB |
Output is correct |
80 |
Correct |
142 ms |
46432 KB |
Output is correct |
81 |
Correct |
203 ms |
54492 KB |
Output is correct |
82 |
Correct |
240 ms |
60252 KB |
Output is correct |
83 |
Correct |
222 ms |
56408 KB |
Output is correct |
84 |
Correct |
145 ms |
55392 KB |
Output is correct |
85 |
Correct |
218 ms |
57436 KB |
Output is correct |
86 |
Correct |
99 ms |
35352 KB |
Output is correct |
87 |
Correct |
339 ms |
55624 KB |
Output is correct |
88 |
Correct |
343 ms |
55624 KB |
Output is correct |
89 |
Correct |
396 ms |
55936 KB |
Output is correct |
90 |
Correct |
188 ms |
37344 KB |
Output is correct |
91 |
Correct |
199 ms |
39292 KB |
Output is correct |
92 |
Correct |
342 ms |
51516 KB |
Output is correct |
93 |
Correct |
373 ms |
59156 KB |
Output is correct |
94 |
Correct |
485 ms |
63676 KB |
Output is correct |
95 |
Correct |
421 ms |
60252 KB |
Output is correct |
96 |
Correct |
441 ms |
59976 KB |
Output is correct |
97 |
Correct |
426 ms |
60312 KB |
Output is correct |