#include<bits/stdc++.h>
using namespace std;
#include "swap.h"
#include <cassert>
#include <cstdio>
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int mxn = 3e5 + 5;
struct E{
int u, v, w;
bool operator <(const E &b){
return(w < b.w);
}
};
int p[mxn + 1], best[mxn + 1], in[mxn + 1], id = 0, val[mxn + 1], dep[mxn + 1], up[mxn + 1][19];
bool ok[mxn + 1];
vt<int>adj[mxn + 1];
vt<E>edge;
int fp(int u){
if(p[u] == u)return(u);
return(p[u] = fp(p[u]));
}
void unon(int a, int b, int w){
bool valid = (in[a] >= 3 || in[b] >= 3);
a = fp(a); b = fp(b);
valid |= (ok[a] || ok[b]);
int pos = ++id; p[pos] = pos; val[pos] = w;
if(a == b){
// cycle
ok[pos] = 1;
adj[pos].pb(a); p[a] = pos;
return;
}
ok[pos] = valid;
adj[pos].pb(a); adj[pos].pb(b); p[a] = p[b] = pos;
val[pos] = w;
}
void dfs(int s, int mn){
if(ok[s])mn = min(mn, val[s]);
best[s] = mn;
for(auto i: adj[s]){
dep[i] = dep[s] + 1;
up[i][0] = s;
dfs(i, mn);
}
}
int lca(int u, int v){
if(dep[u] < dep[v])swap(u, v);
int k = dep[u] - dep[v];
for(int i = 0; i < 19; i++){
if(k & (1 << i)){
u = up[u][i];
}
}
if(u == v)return(u);
for(int i = 18; i >= 0; i--){
if(up[u][i] != up[v][i]){
u = up[u][i]; v = up[v][i];
}
}
return(up[u][0]);
}
void init(int n, int m, vt<int>u, vt<int>v, vt<int>w){
for(int i = 0; i < m; i++){
edge.pb({u[i], v[i], w[i]});
}
id = n - 1;
sort(edge.begin(), edge.end());
for(int i = 0; i < n; i++){
p[i] = i;
}
for(auto [u, v, w]: edge){
in[u]++; in[v]++;
unon(u, v, w);
}
dfs(id, 1e9 + 1);
for(int i = 1; i < 19; i++){
for(int j = 0; j <= id; j++){
up[j][i] = up[up[j][i - 1]][i - 1];
}
}
}
int getMinimumFuelCapacity(int x, int y){
int l = lca(x, y);
if(best[l] == 1e9 + 1)return(-1);
return(best[l]);
}
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:83:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | for(auto [u, v, w]: edge){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7508 KB |
Output is correct |
5 |
Correct |
5 ms |
7636 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
5 ms |
7624 KB |
Output is correct |
8 |
Correct |
5 ms |
7636 KB |
Output is correct |
9 |
Correct |
83 ms |
28480 KB |
Output is correct |
10 |
Correct |
105 ms |
33108 KB |
Output is correct |
11 |
Correct |
106 ms |
32772 KB |
Output is correct |
12 |
Correct |
112 ms |
34320 KB |
Output is correct |
13 |
Correct |
105 ms |
37436 KB |
Output is correct |
14 |
Correct |
98 ms |
28680 KB |
Output is correct |
15 |
Correct |
208 ms |
37408 KB |
Output is correct |
16 |
Correct |
195 ms |
36704 KB |
Output is correct |
17 |
Correct |
209 ms |
38380 KB |
Output is correct |
18 |
Correct |
332 ms |
41384 KB |
Output is correct |
19 |
Correct |
73 ms |
16764 KB |
Output is correct |
20 |
Correct |
208 ms |
38528 KB |
Output is correct |
21 |
Correct |
196 ms |
37912 KB |
Output is correct |
22 |
Correct |
211 ms |
39684 KB |
Output is correct |
23 |
Correct |
288 ms |
42900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
274 ms |
43484 KB |
Output is correct |
4 |
Correct |
239 ms |
45120 KB |
Output is correct |
5 |
Correct |
247 ms |
44304 KB |
Output is correct |
6 |
Correct |
258 ms |
44796 KB |
Output is correct |
7 |
Correct |
255 ms |
44732 KB |
Output is correct |
8 |
Correct |
247 ms |
43360 KB |
Output is correct |
9 |
Correct |
240 ms |
44520 KB |
Output is correct |
10 |
Correct |
224 ms |
43044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7508 KB |
Output is correct |
5 |
Correct |
5 ms |
7636 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
5 ms |
7624 KB |
Output is correct |
8 |
Correct |
5 ms |
7636 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7636 KB |
Output is correct |
11 |
Correct |
5 ms |
7636 KB |
Output is correct |
12 |
Correct |
5 ms |
7624 KB |
Output is correct |
13 |
Correct |
6 ms |
7508 KB |
Output is correct |
14 |
Correct |
5 ms |
7508 KB |
Output is correct |
15 |
Correct |
5 ms |
7636 KB |
Output is correct |
16 |
Correct |
5 ms |
7636 KB |
Output is correct |
17 |
Correct |
5 ms |
7636 KB |
Output is correct |
18 |
Correct |
5 ms |
7648 KB |
Output is correct |
19 |
Correct |
5 ms |
7636 KB |
Output is correct |
20 |
Correct |
5 ms |
7628 KB |
Output is correct |
21 |
Correct |
5 ms |
7632 KB |
Output is correct |
22 |
Correct |
6 ms |
7816 KB |
Output is correct |
23 |
Correct |
5 ms |
7508 KB |
Output is correct |
24 |
Correct |
5 ms |
7756 KB |
Output is correct |
25 |
Correct |
6 ms |
7892 KB |
Output is correct |
26 |
Correct |
5 ms |
7796 KB |
Output is correct |
27 |
Correct |
5 ms |
7636 KB |
Output is correct |
28 |
Correct |
6 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7508 KB |
Output is correct |
6 |
Correct |
5 ms |
7636 KB |
Output is correct |
7 |
Correct |
5 ms |
7508 KB |
Output is correct |
8 |
Correct |
5 ms |
7624 KB |
Output is correct |
9 |
Correct |
5 ms |
7636 KB |
Output is correct |
10 |
Correct |
83 ms |
28480 KB |
Output is correct |
11 |
Correct |
105 ms |
33108 KB |
Output is correct |
12 |
Correct |
106 ms |
32772 KB |
Output is correct |
13 |
Correct |
112 ms |
34320 KB |
Output is correct |
14 |
Correct |
105 ms |
37436 KB |
Output is correct |
15 |
Correct |
4 ms |
7636 KB |
Output is correct |
16 |
Correct |
5 ms |
7636 KB |
Output is correct |
17 |
Correct |
5 ms |
7624 KB |
Output is correct |
18 |
Correct |
6 ms |
7508 KB |
Output is correct |
19 |
Correct |
5 ms |
7508 KB |
Output is correct |
20 |
Correct |
5 ms |
7636 KB |
Output is correct |
21 |
Correct |
5 ms |
7636 KB |
Output is correct |
22 |
Correct |
5 ms |
7636 KB |
Output is correct |
23 |
Correct |
5 ms |
7648 KB |
Output is correct |
24 |
Correct |
5 ms |
7636 KB |
Output is correct |
25 |
Correct |
5 ms |
7628 KB |
Output is correct |
26 |
Correct |
5 ms |
7632 KB |
Output is correct |
27 |
Correct |
6 ms |
7816 KB |
Output is correct |
28 |
Correct |
5 ms |
7508 KB |
Output is correct |
29 |
Correct |
5 ms |
7756 KB |
Output is correct |
30 |
Correct |
6 ms |
7892 KB |
Output is correct |
31 |
Correct |
5 ms |
7796 KB |
Output is correct |
32 |
Correct |
5 ms |
7636 KB |
Output is correct |
33 |
Correct |
6 ms |
7892 KB |
Output is correct |
34 |
Correct |
13 ms |
11056 KB |
Output is correct |
35 |
Correct |
105 ms |
33888 KB |
Output is correct |
36 |
Correct |
102 ms |
33856 KB |
Output is correct |
37 |
Correct |
101 ms |
33856 KB |
Output is correct |
38 |
Correct |
103 ms |
33608 KB |
Output is correct |
39 |
Correct |
103 ms |
33476 KB |
Output is correct |
40 |
Correct |
93 ms |
31320 KB |
Output is correct |
41 |
Correct |
108 ms |
34232 KB |
Output is correct |
42 |
Correct |
103 ms |
34112 KB |
Output is correct |
43 |
Correct |
95 ms |
38204 KB |
Output is correct |
44 |
Correct |
111 ms |
34248 KB |
Output is correct |
45 |
Correct |
143 ms |
46412 KB |
Output is correct |
46 |
Correct |
109 ms |
33984 KB |
Output is correct |
47 |
Correct |
108 ms |
34092 KB |
Output is correct |
48 |
Correct |
117 ms |
38080 KB |
Output is correct |
49 |
Correct |
111 ms |
44988 KB |
Output is correct |
50 |
Correct |
88 ms |
36524 KB |
Output is correct |
51 |
Correct |
125 ms |
41348 KB |
Output is correct |
52 |
Correct |
155 ms |
49048 KB |
Output is correct |
53 |
Correct |
173 ms |
51936 KB |
Output is correct |
54 |
Correct |
187 ms |
58052 KB |
Output is correct |
55 |
Correct |
96 ms |
37676 KB |
Output is correct |
56 |
Correct |
169 ms |
53500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7508 KB |
Output is correct |
5 |
Correct |
5 ms |
7636 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
5 ms |
7624 KB |
Output is correct |
8 |
Correct |
5 ms |
7636 KB |
Output is correct |
9 |
Correct |
83 ms |
28480 KB |
Output is correct |
10 |
Correct |
105 ms |
33108 KB |
Output is correct |
11 |
Correct |
106 ms |
32772 KB |
Output is correct |
12 |
Correct |
112 ms |
34320 KB |
Output is correct |
13 |
Correct |
105 ms |
37436 KB |
Output is correct |
14 |
Correct |
98 ms |
28680 KB |
Output is correct |
15 |
Correct |
208 ms |
37408 KB |
Output is correct |
16 |
Correct |
195 ms |
36704 KB |
Output is correct |
17 |
Correct |
209 ms |
38380 KB |
Output is correct |
18 |
Correct |
332 ms |
41384 KB |
Output is correct |
19 |
Correct |
274 ms |
43484 KB |
Output is correct |
20 |
Correct |
239 ms |
45120 KB |
Output is correct |
21 |
Correct |
247 ms |
44304 KB |
Output is correct |
22 |
Correct |
258 ms |
44796 KB |
Output is correct |
23 |
Correct |
255 ms |
44732 KB |
Output is correct |
24 |
Correct |
247 ms |
43360 KB |
Output is correct |
25 |
Correct |
240 ms |
44520 KB |
Output is correct |
26 |
Correct |
224 ms |
43044 KB |
Output is correct |
27 |
Correct |
4 ms |
7636 KB |
Output is correct |
28 |
Correct |
5 ms |
7636 KB |
Output is correct |
29 |
Correct |
5 ms |
7624 KB |
Output is correct |
30 |
Correct |
6 ms |
7508 KB |
Output is correct |
31 |
Correct |
5 ms |
7508 KB |
Output is correct |
32 |
Correct |
5 ms |
7636 KB |
Output is correct |
33 |
Correct |
5 ms |
7636 KB |
Output is correct |
34 |
Correct |
5 ms |
7636 KB |
Output is correct |
35 |
Correct |
5 ms |
7648 KB |
Output is correct |
36 |
Correct |
13 ms |
11056 KB |
Output is correct |
37 |
Correct |
105 ms |
33888 KB |
Output is correct |
38 |
Correct |
102 ms |
33856 KB |
Output is correct |
39 |
Correct |
101 ms |
33856 KB |
Output is correct |
40 |
Correct |
103 ms |
33608 KB |
Output is correct |
41 |
Correct |
103 ms |
33476 KB |
Output is correct |
42 |
Correct |
93 ms |
31320 KB |
Output is correct |
43 |
Correct |
108 ms |
34232 KB |
Output is correct |
44 |
Correct |
103 ms |
34112 KB |
Output is correct |
45 |
Correct |
95 ms |
38204 KB |
Output is correct |
46 |
Correct |
111 ms |
34248 KB |
Output is correct |
47 |
Correct |
20 ms |
11192 KB |
Output is correct |
48 |
Correct |
217 ms |
38748 KB |
Output is correct |
49 |
Correct |
209 ms |
38944 KB |
Output is correct |
50 |
Correct |
222 ms |
38716 KB |
Output is correct |
51 |
Correct |
217 ms |
38552 KB |
Output is correct |
52 |
Correct |
202 ms |
37060 KB |
Output is correct |
53 |
Correct |
170 ms |
31040 KB |
Output is correct |
54 |
Correct |
216 ms |
39688 KB |
Output is correct |
55 |
Correct |
213 ms |
38856 KB |
Output is correct |
56 |
Correct |
286 ms |
42132 KB |
Output is correct |
57 |
Correct |
231 ms |
39884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7508 KB |
Output is correct |
6 |
Correct |
5 ms |
7636 KB |
Output is correct |
7 |
Correct |
5 ms |
7508 KB |
Output is correct |
8 |
Correct |
5 ms |
7624 KB |
Output is correct |
9 |
Correct |
5 ms |
7636 KB |
Output is correct |
10 |
Correct |
83 ms |
28480 KB |
Output is correct |
11 |
Correct |
105 ms |
33108 KB |
Output is correct |
12 |
Correct |
106 ms |
32772 KB |
Output is correct |
13 |
Correct |
112 ms |
34320 KB |
Output is correct |
14 |
Correct |
105 ms |
37436 KB |
Output is correct |
15 |
Correct |
98 ms |
28680 KB |
Output is correct |
16 |
Correct |
208 ms |
37408 KB |
Output is correct |
17 |
Correct |
195 ms |
36704 KB |
Output is correct |
18 |
Correct |
209 ms |
38380 KB |
Output is correct |
19 |
Correct |
332 ms |
41384 KB |
Output is correct |
20 |
Correct |
274 ms |
43484 KB |
Output is correct |
21 |
Correct |
239 ms |
45120 KB |
Output is correct |
22 |
Correct |
247 ms |
44304 KB |
Output is correct |
23 |
Correct |
258 ms |
44796 KB |
Output is correct |
24 |
Correct |
255 ms |
44732 KB |
Output is correct |
25 |
Correct |
247 ms |
43360 KB |
Output is correct |
26 |
Correct |
240 ms |
44520 KB |
Output is correct |
27 |
Correct |
224 ms |
43044 KB |
Output is correct |
28 |
Correct |
4 ms |
7636 KB |
Output is correct |
29 |
Correct |
5 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7624 KB |
Output is correct |
31 |
Correct |
6 ms |
7508 KB |
Output is correct |
32 |
Correct |
5 ms |
7508 KB |
Output is correct |
33 |
Correct |
5 ms |
7636 KB |
Output is correct |
34 |
Correct |
5 ms |
7636 KB |
Output is correct |
35 |
Correct |
5 ms |
7636 KB |
Output is correct |
36 |
Correct |
5 ms |
7648 KB |
Output is correct |
37 |
Correct |
13 ms |
11056 KB |
Output is correct |
38 |
Correct |
105 ms |
33888 KB |
Output is correct |
39 |
Correct |
102 ms |
33856 KB |
Output is correct |
40 |
Correct |
101 ms |
33856 KB |
Output is correct |
41 |
Correct |
103 ms |
33608 KB |
Output is correct |
42 |
Correct |
103 ms |
33476 KB |
Output is correct |
43 |
Correct |
93 ms |
31320 KB |
Output is correct |
44 |
Correct |
108 ms |
34232 KB |
Output is correct |
45 |
Correct |
103 ms |
34112 KB |
Output is correct |
46 |
Correct |
95 ms |
38204 KB |
Output is correct |
47 |
Correct |
111 ms |
34248 KB |
Output is correct |
48 |
Correct |
20 ms |
11192 KB |
Output is correct |
49 |
Correct |
217 ms |
38748 KB |
Output is correct |
50 |
Correct |
209 ms |
38944 KB |
Output is correct |
51 |
Correct |
222 ms |
38716 KB |
Output is correct |
52 |
Correct |
217 ms |
38552 KB |
Output is correct |
53 |
Correct |
202 ms |
37060 KB |
Output is correct |
54 |
Correct |
170 ms |
31040 KB |
Output is correct |
55 |
Correct |
216 ms |
39688 KB |
Output is correct |
56 |
Correct |
213 ms |
38856 KB |
Output is correct |
57 |
Correct |
286 ms |
42132 KB |
Output is correct |
58 |
Correct |
231 ms |
39884 KB |
Output is correct |
59 |
Correct |
73 ms |
16764 KB |
Output is correct |
60 |
Correct |
208 ms |
38528 KB |
Output is correct |
61 |
Correct |
196 ms |
37912 KB |
Output is correct |
62 |
Correct |
211 ms |
39684 KB |
Output is correct |
63 |
Correct |
288 ms |
42900 KB |
Output is correct |
64 |
Correct |
5 ms |
7636 KB |
Output is correct |
65 |
Correct |
5 ms |
7628 KB |
Output is correct |
66 |
Correct |
5 ms |
7632 KB |
Output is correct |
67 |
Correct |
6 ms |
7816 KB |
Output is correct |
68 |
Correct |
5 ms |
7508 KB |
Output is correct |
69 |
Correct |
5 ms |
7756 KB |
Output is correct |
70 |
Correct |
6 ms |
7892 KB |
Output is correct |
71 |
Correct |
5 ms |
7796 KB |
Output is correct |
72 |
Correct |
5 ms |
7636 KB |
Output is correct |
73 |
Correct |
6 ms |
7892 KB |
Output is correct |
74 |
Correct |
143 ms |
46412 KB |
Output is correct |
75 |
Correct |
109 ms |
33984 KB |
Output is correct |
76 |
Correct |
108 ms |
34092 KB |
Output is correct |
77 |
Correct |
117 ms |
38080 KB |
Output is correct |
78 |
Correct |
111 ms |
44988 KB |
Output is correct |
79 |
Correct |
88 ms |
36524 KB |
Output is correct |
80 |
Correct |
125 ms |
41348 KB |
Output is correct |
81 |
Correct |
155 ms |
49048 KB |
Output is correct |
82 |
Correct |
173 ms |
51936 KB |
Output is correct |
83 |
Correct |
187 ms |
58052 KB |
Output is correct |
84 |
Correct |
96 ms |
37676 KB |
Output is correct |
85 |
Correct |
169 ms |
53500 KB |
Output is correct |
86 |
Correct |
66 ms |
21924 KB |
Output is correct |
87 |
Correct |
212 ms |
38868 KB |
Output is correct |
88 |
Correct |
206 ms |
38824 KB |
Output is correct |
89 |
Correct |
238 ms |
41496 KB |
Output is correct |
90 |
Correct |
185 ms |
51100 KB |
Output is correct |
91 |
Correct |
189 ms |
47952 KB |
Output is correct |
92 |
Correct |
278 ms |
45076 KB |
Output is correct |
93 |
Correct |
287 ms |
53136 KB |
Output is correct |
94 |
Correct |
347 ms |
56396 KB |
Output is correct |
95 |
Correct |
286 ms |
61876 KB |
Output is correct |
96 |
Correct |
333 ms |
42436 KB |
Output is correct |
97 |
Correct |
336 ms |
49460 KB |
Output is correct |