#include "race.h"
#include "bits/stdc++.h"
using namespace std;
const int mxN = 2e5L + 10;
const int mxK = 1e6L + 10;
int n, k;
vector<pair<int, int>> adj[mxN];
int ans = mxN;
int sz[mxN];
bool proc[mxN] = {};
int dp[mxK];
int get_sz(int x, int p) {
sz[x] = 1;
for(auto [y, w] : adj[x]) {
if(proc[y] || y == p) continue;
sz[x] += get_sz(y, x);
}
return sz[x];
}
int get_cen(int x, int p, int tot) {
for(auto [y, w] : adj[x]) {
if(proc[y] || y == p || sz[y] * 2 <= tot) continue;
return get_cen(y, x, tot);
}
return x;
}
void dfs(int x, int p, int dep, int dis, int flag) {
if(dis > k) return;
if(flag == 0) {
if(dp[k - dis] != mxN) ans = min(ans, dep + dp[k - dis]);
}
else if(flag == 1) {
dp[dis] = min(dp[dis], dep);
}
else if(flag == 2) {
dp[dis] = mxN;
}
for(auto [y, w] : adj[x]) {
if(proc[y] || y == p) continue;
dfs(y, x, dep + 1, dis + w, flag);
}
}
void solve(int x = 1, int p = -1) {
int c = get_cen(x, p, get_sz(x, p));
dp[0] = 0;
for(auto [d, w] : adj[c]) {
if(proc[d]) continue;
dfs(d, c, 1, w, 0);
dfs(d, c, 1, w, 1);
}
for(auto [d, w] : adj[c]) {
if(proc[d]) continue;
dfs(d, c, 1, w, 2);
}
dp[0] = mxN;
proc[c] = 1;
for(auto [d, w] : adj[c]) {
if(proc[d]) continue;
solve(d, c);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
for(int i = 0; i < mxK; ++i) dp[i] = mxN;
n = N; k = K;
for(int i = 0; i < n - 1; ++i) {
adj[H[i][0] + 1].emplace_back(H[i][1] + 1, L[i]);
adj[H[i][1] + 1].emplace_back(H[i][0] + 1, L[i]);
}
solve();
return (ans > n - 1 ? -1 : ans);
}
Compilation message
race.cpp: In function 'int get_sz(int, int)':
race.cpp:16:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
16 | for(auto [y, w] : adj[x]) {
| ^
race.cpp: In function 'int get_cen(int, int, int)':
race.cpp:24:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
24 | for(auto [y, w] : adj[x]) {
| ^
race.cpp: In function 'void dfs(int, int, int, int, int)':
race.cpp:43:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
43 | for(auto [y, w] : adj[x]) {
| ^
race.cpp: In function 'void solve(int, int)':
race.cpp:52:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for(auto [d, w] : adj[c]) {
| ^
race.cpp:57:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | for(auto [d, w] : adj[c]) {
| ^
race.cpp:63:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
63 | for(auto [d, w] : adj[c]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
2 ms |
13660 KB |
Output is correct |
3 |
Correct |
2 ms |
13660 KB |
Output is correct |
4 |
Correct |
2 ms |
13660 KB |
Output is correct |
5 |
Correct |
2 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
2 ms |
13660 KB |
Output is correct |
8 |
Correct |
2 ms |
13660 KB |
Output is correct |
9 |
Correct |
2 ms |
13660 KB |
Output is correct |
10 |
Correct |
2 ms |
13656 KB |
Output is correct |
11 |
Correct |
2 ms |
13656 KB |
Output is correct |
12 |
Correct |
2 ms |
13668 KB |
Output is correct |
13 |
Correct |
2 ms |
13660 KB |
Output is correct |
14 |
Correct |
2 ms |
13656 KB |
Output is correct |
15 |
Correct |
2 ms |
13660 KB |
Output is correct |
16 |
Correct |
2 ms |
13660 KB |
Output is correct |
17 |
Correct |
2 ms |
13660 KB |
Output is correct |
18 |
Correct |
2 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
2 ms |
13660 KB |
Output is correct |
3 |
Correct |
2 ms |
13660 KB |
Output is correct |
4 |
Correct |
2 ms |
13660 KB |
Output is correct |
5 |
Correct |
2 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
2 ms |
13660 KB |
Output is correct |
8 |
Correct |
2 ms |
13660 KB |
Output is correct |
9 |
Correct |
2 ms |
13660 KB |
Output is correct |
10 |
Correct |
2 ms |
13656 KB |
Output is correct |
11 |
Correct |
2 ms |
13656 KB |
Output is correct |
12 |
Correct |
2 ms |
13668 KB |
Output is correct |
13 |
Correct |
2 ms |
13660 KB |
Output is correct |
14 |
Correct |
2 ms |
13656 KB |
Output is correct |
15 |
Correct |
2 ms |
13660 KB |
Output is correct |
16 |
Correct |
2 ms |
13660 KB |
Output is correct |
17 |
Correct |
2 ms |
13660 KB |
Output is correct |
18 |
Correct |
2 ms |
13660 KB |
Output is correct |
19 |
Correct |
2 ms |
13660 KB |
Output is correct |
20 |
Correct |
3 ms |
13660 KB |
Output is correct |
21 |
Correct |
3 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13660 KB |
Output is correct |
26 |
Correct |
2 ms |
13660 KB |
Output is correct |
27 |
Correct |
2 ms |
13660 KB |
Output is correct |
28 |
Correct |
3 ms |
13676 KB |
Output is correct |
29 |
Correct |
2 ms |
13660 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
2 ms |
13912 KB |
Output is correct |
32 |
Correct |
3 ms |
13660 KB |
Output is correct |
33 |
Correct |
2 ms |
13660 KB |
Output is correct |
34 |
Correct |
3 ms |
13660 KB |
Output is correct |
35 |
Correct |
2 ms |
13660 KB |
Output is correct |
36 |
Correct |
3 ms |
13660 KB |
Output is correct |
37 |
Correct |
3 ms |
13656 KB |
Output is correct |
38 |
Correct |
3 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
2 ms |
13660 KB |
Output is correct |
3 |
Correct |
2 ms |
13660 KB |
Output is correct |
4 |
Correct |
2 ms |
13660 KB |
Output is correct |
5 |
Correct |
2 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
2 ms |
13660 KB |
Output is correct |
8 |
Correct |
2 ms |
13660 KB |
Output is correct |
9 |
Correct |
2 ms |
13660 KB |
Output is correct |
10 |
Correct |
2 ms |
13656 KB |
Output is correct |
11 |
Correct |
2 ms |
13656 KB |
Output is correct |
12 |
Correct |
2 ms |
13668 KB |
Output is correct |
13 |
Correct |
2 ms |
13660 KB |
Output is correct |
14 |
Correct |
2 ms |
13656 KB |
Output is correct |
15 |
Correct |
2 ms |
13660 KB |
Output is correct |
16 |
Correct |
2 ms |
13660 KB |
Output is correct |
17 |
Correct |
2 ms |
13660 KB |
Output is correct |
18 |
Correct |
2 ms |
13660 KB |
Output is correct |
19 |
Correct |
81 ms |
19300 KB |
Output is correct |
20 |
Correct |
79 ms |
19544 KB |
Output is correct |
21 |
Correct |
88 ms |
19556 KB |
Output is correct |
22 |
Correct |
99 ms |
19536 KB |
Output is correct |
23 |
Correct |
43 ms |
19804 KB |
Output is correct |
24 |
Correct |
32 ms |
19700 KB |
Output is correct |
25 |
Correct |
80 ms |
22300 KB |
Output is correct |
26 |
Correct |
67 ms |
25168 KB |
Output is correct |
27 |
Correct |
96 ms |
23220 KB |
Output is correct |
28 |
Correct |
134 ms |
34388 KB |
Output is correct |
29 |
Correct |
129 ms |
33692 KB |
Output is correct |
30 |
Correct |
92 ms |
23436 KB |
Output is correct |
31 |
Correct |
96 ms |
23440 KB |
Output is correct |
32 |
Correct |
116 ms |
23376 KB |
Output is correct |
33 |
Correct |
123 ms |
22100 KB |
Output is correct |
34 |
Correct |
98 ms |
22096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13660 KB |
Output is correct |
2 |
Correct |
2 ms |
13660 KB |
Output is correct |
3 |
Correct |
2 ms |
13660 KB |
Output is correct |
4 |
Correct |
2 ms |
13660 KB |
Output is correct |
5 |
Correct |
2 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
2 ms |
13660 KB |
Output is correct |
8 |
Correct |
2 ms |
13660 KB |
Output is correct |
9 |
Correct |
2 ms |
13660 KB |
Output is correct |
10 |
Correct |
2 ms |
13656 KB |
Output is correct |
11 |
Correct |
2 ms |
13656 KB |
Output is correct |
12 |
Correct |
2 ms |
13668 KB |
Output is correct |
13 |
Correct |
2 ms |
13660 KB |
Output is correct |
14 |
Correct |
2 ms |
13656 KB |
Output is correct |
15 |
Correct |
2 ms |
13660 KB |
Output is correct |
16 |
Correct |
2 ms |
13660 KB |
Output is correct |
17 |
Correct |
2 ms |
13660 KB |
Output is correct |
18 |
Correct |
2 ms |
13660 KB |
Output is correct |
19 |
Correct |
2 ms |
13660 KB |
Output is correct |
20 |
Correct |
3 ms |
13660 KB |
Output is correct |
21 |
Correct |
3 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13660 KB |
Output is correct |
26 |
Correct |
2 ms |
13660 KB |
Output is correct |
27 |
Correct |
2 ms |
13660 KB |
Output is correct |
28 |
Correct |
3 ms |
13676 KB |
Output is correct |
29 |
Correct |
2 ms |
13660 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
2 ms |
13912 KB |
Output is correct |
32 |
Correct |
3 ms |
13660 KB |
Output is correct |
33 |
Correct |
2 ms |
13660 KB |
Output is correct |
34 |
Correct |
3 ms |
13660 KB |
Output is correct |
35 |
Correct |
2 ms |
13660 KB |
Output is correct |
36 |
Correct |
3 ms |
13660 KB |
Output is correct |
37 |
Correct |
3 ms |
13656 KB |
Output is correct |
38 |
Correct |
3 ms |
13660 KB |
Output is correct |
39 |
Correct |
81 ms |
19300 KB |
Output is correct |
40 |
Correct |
79 ms |
19544 KB |
Output is correct |
41 |
Correct |
88 ms |
19556 KB |
Output is correct |
42 |
Correct |
99 ms |
19536 KB |
Output is correct |
43 |
Correct |
43 ms |
19804 KB |
Output is correct |
44 |
Correct |
32 ms |
19700 KB |
Output is correct |
45 |
Correct |
80 ms |
22300 KB |
Output is correct |
46 |
Correct |
67 ms |
25168 KB |
Output is correct |
47 |
Correct |
96 ms |
23220 KB |
Output is correct |
48 |
Correct |
134 ms |
34388 KB |
Output is correct |
49 |
Correct |
129 ms |
33692 KB |
Output is correct |
50 |
Correct |
92 ms |
23436 KB |
Output is correct |
51 |
Correct |
96 ms |
23440 KB |
Output is correct |
52 |
Correct |
116 ms |
23376 KB |
Output is correct |
53 |
Correct |
123 ms |
22100 KB |
Output is correct |
54 |
Correct |
98 ms |
22096 KB |
Output is correct |
55 |
Correct |
8 ms |
13912 KB |
Output is correct |
56 |
Correct |
8 ms |
13916 KB |
Output is correct |
57 |
Correct |
49 ms |
19724 KB |
Output is correct |
58 |
Correct |
22 ms |
19816 KB |
Output is correct |
59 |
Correct |
66 ms |
25180 KB |
Output is correct |
60 |
Correct |
289 ms |
33884 KB |
Output is correct |
61 |
Correct |
114 ms |
23376 KB |
Output is correct |
62 |
Correct |
123 ms |
23376 KB |
Output is correct |
63 |
Correct |
127 ms |
23380 KB |
Output is correct |
64 |
Correct |
239 ms |
23048 KB |
Output is correct |
65 |
Correct |
93 ms |
23120 KB |
Output is correct |
66 |
Correct |
205 ms |
31824 KB |
Output is correct |
67 |
Correct |
59 ms |
23492 KB |
Output is correct |
68 |
Correct |
124 ms |
23636 KB |
Output is correct |
69 |
Correct |
114 ms |
23636 KB |
Output is correct |
70 |
Correct |
122 ms |
23380 KB |
Output is correct |