#include "swap.h"
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
using namespace std;
typedef pair<int,int> ii;
const int inf = 1023456789;
int n, newNode;
namespace ufds{
int p[200005];
void init(){
for(int i = 0;i < 2*n;i++) p[i] = i;
}
int findSet(int u){
if(u == p[u]) return u;
else return p[u] = findSet(p[u]);
}
bool unionSet(int u, int v){
u = findSet(u), v = findSet(v);
if(u == v) return false;
if(u > v) swap(u, v);
p[u] = v;
return true;
}
}
struct edge{ int u, v, w; };
vector<edge> edges;
int maxEdge[200005];
int minWork[200005];
int L[200005];
int R[200005];
int deg[200005];
int p[200005][19];
int depth[200005];
void dfs0(int u){
if(L[u] == R[u] && L[u] == 0) return;
depth[L[u]] = depth[u] + 1;
depth[R[u]] = depth[u] + 1;
dfs0(L[u]); dfs0(R[u]);
}
void dfs1(int u){
if(L[u] == R[u] && L[u] == 0) return;
dfs1(L[u]); dfs1(R[u]);
minWork[u] = min(minWork[u], minWork[L[u]]); ///pass up to parent
minWork[u] = min(minWork[u], minWork[R[u]]);
}
void dfs2(int u){
if(L[u] == R[u] && L[u] == 0) return;
minWork[L[u]] = min(minWork[u], minWork[L[u]]); ///pass down to children
minWork[R[u]] = min(minWork[u], minWork[R[u]]);
dfs2(L[u]); dfs2(R[u]);
}
int LCA(int u, int v){
if(depth[u] < depth[v]) swap(u, v);
for(int k = 18;k >= 0;k--){
int newU = p[u][k];
if(depth[newU] >= depth[v]) u = newU;
}
for(int k = 18;k >= 0;k--){
int newU = p[u][k], newV = p[v][k];
if(newU != newV){
u = newU;
v = newV;
}
}
return p[u][0];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N; newNode = n;
for(int i = 0;i < M;i++) edges.push_back({U[i], V[i], W[i]});
ufds::init();
fill(minWork, minWork+2*n, inf);
sort(all(edges), [&](edge a, edge b){ return a.w < b.w; });
vector<edge> cycle;
for(edge e : edges){
deg[e.u]++;
deg[e.v]++;
if(deg[e.u] >= 3) minWork[e.u] = min(e.w, minWork[e.u]);
if(deg[e.v] >= 3) minWork[e.v] = min(e.w, minWork[e.v]);
int u = ufds::findSet(e.u), v = ufds::findSet(e.v);
if(u != v){
L[newNode] = u;
R[newNode] = v;
ufds::unionSet(newNode, u);
ufds::unionSet(newNode, v);
maxEdge[newNode] = e.w;
p[u][0] = newNode;
p[v][0] = newNode;
newNode++;
}
else cycle.push_back(e);
}
n = newNode;
p[n-1][0] = -1;
for(int k = 1;k <= 18;k++){
for(int i = 0;i < n;i++){
if(p[i][k-1] == -1) p[i][k] = -1;
else p[i][k] = p[p[i][k-1]][k-1];
}
}
dfs0(n-1);
for(edge e : cycle){
int u = e.u, v = e.v;
int lca = LCA(u, v);
minWork[lca] = min(e.w, minWork[lca]);
}
dfs1(n-1);
for(int i = 0;i < n;i++) minWork[i] = max(minWork[i], maxEdge[i]);
dfs2(n-1);
}
int getMinimumFuelCapacity(int X, int Y){
int lca = LCA(X, Y);
//cout << X << " " << Y << " " << lca << "\n";
int ans = max(minWork[lca], maxEdge[lca]);
if(ans == inf) ans = -1;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
96 ms |
18028 KB |
Output is correct |
10 |
Correct |
123 ms |
21868 KB |
Output is correct |
11 |
Correct |
118 ms |
21612 KB |
Output is correct |
12 |
Correct |
125 ms |
22764 KB |
Output is correct |
13 |
Correct |
122 ms |
23536 KB |
Output is correct |
14 |
Correct |
109 ms |
18028 KB |
Output is correct |
15 |
Correct |
306 ms |
23848 KB |
Output is correct |
16 |
Correct |
335 ms |
23324 KB |
Output is correct |
17 |
Correct |
304 ms |
24532 KB |
Output is correct |
18 |
Correct |
471 ms |
25412 KB |
Output is correct |
19 |
Correct |
119 ms |
7292 KB |
Output is correct |
20 |
Correct |
313 ms |
25068 KB |
Output is correct |
21 |
Correct |
343 ms |
24488 KB |
Output is correct |
22 |
Correct |
348 ms |
26004 KB |
Output is correct |
23 |
Correct |
524 ms |
26704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
406 ms |
25996 KB |
Output is correct |
4 |
Correct |
420 ms |
27220 KB |
Output is correct |
5 |
Correct |
427 ms |
26620 KB |
Output is correct |
6 |
Correct |
417 ms |
26848 KB |
Output is correct |
7 |
Correct |
420 ms |
26860 KB |
Output is correct |
8 |
Correct |
414 ms |
25868 KB |
Output is correct |
9 |
Correct |
428 ms |
26528 KB |
Output is correct |
10 |
Correct |
416 ms |
25640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
96 ms |
18028 KB |
Output is correct |
11 |
Correct |
123 ms |
21868 KB |
Output is correct |
12 |
Correct |
118 ms |
21612 KB |
Output is correct |
13 |
Correct |
125 ms |
22764 KB |
Output is correct |
14 |
Correct |
122 ms |
23536 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
12 ms |
3520 KB |
Output is correct |
25 |
Correct |
129 ms |
22764 KB |
Output is correct |
26 |
Correct |
129 ms |
22752 KB |
Output is correct |
27 |
Correct |
118 ms |
22764 KB |
Output is correct |
28 |
Correct |
117 ms |
22508 KB |
Output is correct |
29 |
Correct |
114 ms |
22380 KB |
Output is correct |
30 |
Correct |
108 ms |
20496 KB |
Output is correct |
31 |
Correct |
124 ms |
22764 KB |
Output is correct |
32 |
Correct |
131 ms |
22920 KB |
Output is correct |
33 |
Correct |
133 ms |
23824 KB |
Output is correct |
34 |
Correct |
122 ms |
22892 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
640 KB |
Output is correct |
37 |
Correct |
1 ms |
640 KB |
Output is correct |
38 |
Correct |
2 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
640 KB |
Output is correct |
42 |
Correct |
2 ms |
640 KB |
Output is correct |
43 |
Correct |
1 ms |
640 KB |
Output is correct |
44 |
Correct |
2 ms |
640 KB |
Output is correct |
45 |
Correct |
200 ms |
22956 KB |
Output is correct |
46 |
Correct |
114 ms |
22760 KB |
Output is correct |
47 |
Correct |
134 ms |
22736 KB |
Output is correct |
48 |
Correct |
143 ms |
23276 KB |
Output is correct |
49 |
Correct |
128 ms |
10736 KB |
Output is correct |
50 |
Correct |
103 ms |
8300 KB |
Output is correct |
51 |
Correct |
156 ms |
18668 KB |
Output is correct |
52 |
Correct |
211 ms |
26328 KB |
Output is correct |
53 |
Correct |
259 ms |
28132 KB |
Output is correct |
54 |
Correct |
240 ms |
28904 KB |
Output is correct |
55 |
Correct |
120 ms |
23660 KB |
Output is correct |
56 |
Correct |
247 ms |
27976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
96 ms |
18028 KB |
Output is correct |
10 |
Correct |
123 ms |
21868 KB |
Output is correct |
11 |
Correct |
118 ms |
21612 KB |
Output is correct |
12 |
Correct |
125 ms |
22764 KB |
Output is correct |
13 |
Correct |
122 ms |
23536 KB |
Output is correct |
14 |
Correct |
109 ms |
18028 KB |
Output is correct |
15 |
Correct |
306 ms |
23848 KB |
Output is correct |
16 |
Correct |
335 ms |
23324 KB |
Output is correct |
17 |
Correct |
304 ms |
24532 KB |
Output is correct |
18 |
Correct |
471 ms |
25412 KB |
Output is correct |
19 |
Correct |
406 ms |
25996 KB |
Output is correct |
20 |
Correct |
420 ms |
27220 KB |
Output is correct |
21 |
Correct |
427 ms |
26620 KB |
Output is correct |
22 |
Correct |
417 ms |
26848 KB |
Output is correct |
23 |
Correct |
420 ms |
26860 KB |
Output is correct |
24 |
Correct |
414 ms |
25868 KB |
Output is correct |
25 |
Correct |
428 ms |
26528 KB |
Output is correct |
26 |
Correct |
416 ms |
25640 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
640 KB |
Output is correct |
33 |
Correct |
1 ms |
640 KB |
Output is correct |
34 |
Correct |
1 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
640 KB |
Output is correct |
36 |
Correct |
12 ms |
3520 KB |
Output is correct |
37 |
Correct |
129 ms |
22764 KB |
Output is correct |
38 |
Correct |
129 ms |
22752 KB |
Output is correct |
39 |
Correct |
118 ms |
22764 KB |
Output is correct |
40 |
Correct |
117 ms |
22508 KB |
Output is correct |
41 |
Correct |
114 ms |
22380 KB |
Output is correct |
42 |
Correct |
108 ms |
20496 KB |
Output is correct |
43 |
Correct |
124 ms |
22764 KB |
Output is correct |
44 |
Correct |
131 ms |
22920 KB |
Output is correct |
45 |
Correct |
133 ms |
23824 KB |
Output is correct |
46 |
Correct |
122 ms |
22892 KB |
Output is correct |
47 |
Correct |
25 ms |
3504 KB |
Output is correct |
48 |
Correct |
313 ms |
25428 KB |
Output is correct |
49 |
Correct |
308 ms |
25412 KB |
Output is correct |
50 |
Correct |
290 ms |
25428 KB |
Output is correct |
51 |
Correct |
288 ms |
25184 KB |
Output is correct |
52 |
Correct |
283 ms |
23964 KB |
Output is correct |
53 |
Correct |
240 ms |
18784 KB |
Output is correct |
54 |
Correct |
318 ms |
26064 KB |
Output is correct |
55 |
Correct |
302 ms |
25300 KB |
Output is correct |
56 |
Correct |
464 ms |
26324 KB |
Output is correct |
57 |
Correct |
305 ms |
25940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
96 ms |
18028 KB |
Output is correct |
11 |
Correct |
123 ms |
21868 KB |
Output is correct |
12 |
Correct |
118 ms |
21612 KB |
Output is correct |
13 |
Correct |
125 ms |
22764 KB |
Output is correct |
14 |
Correct |
122 ms |
23536 KB |
Output is correct |
15 |
Correct |
109 ms |
18028 KB |
Output is correct |
16 |
Correct |
306 ms |
23848 KB |
Output is correct |
17 |
Correct |
335 ms |
23324 KB |
Output is correct |
18 |
Correct |
304 ms |
24532 KB |
Output is correct |
19 |
Correct |
471 ms |
25412 KB |
Output is correct |
20 |
Correct |
406 ms |
25996 KB |
Output is correct |
21 |
Correct |
420 ms |
27220 KB |
Output is correct |
22 |
Correct |
427 ms |
26620 KB |
Output is correct |
23 |
Correct |
417 ms |
26848 KB |
Output is correct |
24 |
Correct |
420 ms |
26860 KB |
Output is correct |
25 |
Correct |
414 ms |
25868 KB |
Output is correct |
26 |
Correct |
428 ms |
26528 KB |
Output is correct |
27 |
Correct |
416 ms |
25640 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
640 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
640 KB |
Output is correct |
34 |
Correct |
1 ms |
640 KB |
Output is correct |
35 |
Correct |
1 ms |
640 KB |
Output is correct |
36 |
Correct |
2 ms |
640 KB |
Output is correct |
37 |
Correct |
12 ms |
3520 KB |
Output is correct |
38 |
Correct |
129 ms |
22764 KB |
Output is correct |
39 |
Correct |
129 ms |
22752 KB |
Output is correct |
40 |
Correct |
118 ms |
22764 KB |
Output is correct |
41 |
Correct |
117 ms |
22508 KB |
Output is correct |
42 |
Correct |
114 ms |
22380 KB |
Output is correct |
43 |
Correct |
108 ms |
20496 KB |
Output is correct |
44 |
Correct |
124 ms |
22764 KB |
Output is correct |
45 |
Correct |
131 ms |
22920 KB |
Output is correct |
46 |
Correct |
133 ms |
23824 KB |
Output is correct |
47 |
Correct |
122 ms |
22892 KB |
Output is correct |
48 |
Correct |
25 ms |
3504 KB |
Output is correct |
49 |
Correct |
313 ms |
25428 KB |
Output is correct |
50 |
Correct |
308 ms |
25412 KB |
Output is correct |
51 |
Correct |
290 ms |
25428 KB |
Output is correct |
52 |
Correct |
288 ms |
25184 KB |
Output is correct |
53 |
Correct |
283 ms |
23964 KB |
Output is correct |
54 |
Correct |
240 ms |
18784 KB |
Output is correct |
55 |
Correct |
318 ms |
26064 KB |
Output is correct |
56 |
Correct |
302 ms |
25300 KB |
Output is correct |
57 |
Correct |
464 ms |
26324 KB |
Output is correct |
58 |
Correct |
305 ms |
25940 KB |
Output is correct |
59 |
Correct |
119 ms |
7292 KB |
Output is correct |
60 |
Correct |
313 ms |
25068 KB |
Output is correct |
61 |
Correct |
343 ms |
24488 KB |
Output is correct |
62 |
Correct |
348 ms |
26004 KB |
Output is correct |
63 |
Correct |
524 ms |
26704 KB |
Output is correct |
64 |
Correct |
1 ms |
512 KB |
Output is correct |
65 |
Correct |
1 ms |
640 KB |
Output is correct |
66 |
Correct |
1 ms |
640 KB |
Output is correct |
67 |
Correct |
2 ms |
512 KB |
Output is correct |
68 |
Correct |
1 ms |
512 KB |
Output is correct |
69 |
Correct |
2 ms |
640 KB |
Output is correct |
70 |
Correct |
2 ms |
640 KB |
Output is correct |
71 |
Correct |
2 ms |
640 KB |
Output is correct |
72 |
Correct |
1 ms |
640 KB |
Output is correct |
73 |
Correct |
2 ms |
640 KB |
Output is correct |
74 |
Correct |
200 ms |
22956 KB |
Output is correct |
75 |
Correct |
114 ms |
22760 KB |
Output is correct |
76 |
Correct |
134 ms |
22736 KB |
Output is correct |
77 |
Correct |
143 ms |
23276 KB |
Output is correct |
78 |
Correct |
128 ms |
10736 KB |
Output is correct |
79 |
Correct |
103 ms |
8300 KB |
Output is correct |
80 |
Correct |
156 ms |
18668 KB |
Output is correct |
81 |
Correct |
211 ms |
26328 KB |
Output is correct |
82 |
Correct |
259 ms |
28132 KB |
Output is correct |
83 |
Correct |
240 ms |
28904 KB |
Output is correct |
84 |
Correct |
120 ms |
23660 KB |
Output is correct |
85 |
Correct |
247 ms |
27976 KB |
Output is correct |
86 |
Correct |
95 ms |
9072 KB |
Output is correct |
87 |
Correct |
309 ms |
25300 KB |
Output is correct |
88 |
Correct |
311 ms |
25300 KB |
Output is correct |
89 |
Correct |
392 ms |
24460 KB |
Output is correct |
90 |
Correct |
248 ms |
12772 KB |
Output is correct |
91 |
Correct |
274 ms |
13800 KB |
Output is correct |
92 |
Correct |
371 ms |
20128 KB |
Output is correct |
93 |
Correct |
372 ms |
27888 KB |
Output is correct |
94 |
Correct |
430 ms |
29544 KB |
Output is correct |
95 |
Correct |
471 ms |
30436 KB |
Output is correct |
96 |
Correct |
483 ms |
26324 KB |
Output is correct |
97 |
Correct |
468 ms |
27432 KB |
Output is correct |