#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAXK = 1e6 +5, MAXN = 2e5+ 5;
vector<pii> v[MAXN];
vector<int> limpa;
int dp[MAXK], vis[MAXN], sz[MAXN], ans = MAXK, k;
int subtree(int node, int pai = 0) {
sz[node] = 1;
for (pii x : v[node]) {
if (!vis[x.first] && x.first != pai) {
sz[node] += subtree(x.first, node);
}
}
return sz[node];
}
int centroid(int node, int desired, int pai = 0) {
for (pii x : v[node]) {
if (!vis[x.first] && x.first != pai && sz[x.first] > desired) {
return centroid(x.first, desired, node);
}
}
return node;
}
void calcDP(int node, int pai, bool filling, int dep, int dist) {
if (dist > k) return;
if (filling) {
dp[dist] = min(dp[dist], dep);
if (dist)
limpa.push_back(dist);
}
else {
ans = min(ans, dep + dp[k - dist]);
}
for (pii x : v[node]) {
if (x.first != pai && !vis[x.first]) {
calcDP(x.first, node, filling, dep + 1, dist + x.second);
}
}
}
void solve(int node) {
int c = centroid(node, subtree(node) / 2);
vis[c] = true;
for(int i = 0; i < 2; i++) {
// do it once normally and then again reversed
for (pii x : v[c]) {
if (!vis[x.first]) {
calcDP(x.first, c, false, 1, x.second);
calcDP(x.first, c, true, 1, x.second);
}
}
for (int x : limpa) dp[x] = MAXK;
limpa.clear();
reverse(v[c].begin(), v[c].end());
}
for (pii x : v[c]) {
if (!vis[x.first]) {
solve(x.first);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
for (int i = 0; i < N - 1; i++) {
v[H[i][0]].emplace_back(H[i][1], L[i]);
v[H[i][1]].emplace_back(H[i][0], L[i]);
}
for (int i = 1; i < MAXK; i++) dp[i] = MAXK;
solve(1);
return (ans == MAXK ? -1 : ans);
return N;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8848 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8936 KB |
Output is correct |
5 |
Correct |
4 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8828 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
4 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
9004 KB |
Output is correct |
14 |
Correct |
4 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
4 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8848 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8936 KB |
Output is correct |
5 |
Correct |
4 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8828 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
4 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
9004 KB |
Output is correct |
14 |
Correct |
4 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
4 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
4 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
7 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8996 KB |
Output is correct |
25 |
Correct |
6 ms |
8916 KB |
Output is correct |
26 |
Correct |
6 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8916 KB |
Output is correct |
28 |
Correct |
6 ms |
8916 KB |
Output is correct |
29 |
Correct |
5 ms |
8984 KB |
Output is correct |
30 |
Correct |
5 ms |
8916 KB |
Output is correct |
31 |
Correct |
5 ms |
9004 KB |
Output is correct |
32 |
Correct |
6 ms |
8916 KB |
Output is correct |
33 |
Correct |
6 ms |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
5 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8940 KB |
Output is correct |
38 |
Correct |
5 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8848 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8936 KB |
Output is correct |
5 |
Correct |
4 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8828 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
4 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
9004 KB |
Output is correct |
14 |
Correct |
4 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
4 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
138 ms |
14644 KB |
Output is correct |
20 |
Correct |
137 ms |
15944 KB |
Output is correct |
21 |
Correct |
162 ms |
16180 KB |
Output is correct |
22 |
Correct |
155 ms |
16340 KB |
Output is correct |
23 |
Correct |
72 ms |
16216 KB |
Output is correct |
24 |
Correct |
56 ms |
16048 KB |
Output is correct |
25 |
Correct |
140 ms |
21092 KB |
Output is correct |
26 |
Correct |
113 ms |
22180 KB |
Output is correct |
27 |
Correct |
162 ms |
23304 KB |
Output is correct |
28 |
Correct |
236 ms |
33728 KB |
Output is correct |
29 |
Correct |
237 ms |
29664 KB |
Output is correct |
30 |
Correct |
163 ms |
23252 KB |
Output is correct |
31 |
Correct |
157 ms |
23212 KB |
Output is correct |
32 |
Correct |
193 ms |
23300 KB |
Output is correct |
33 |
Correct |
211 ms |
22096 KB |
Output is correct |
34 |
Correct |
172 ms |
22912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8848 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8936 KB |
Output is correct |
5 |
Correct |
4 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8828 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
4 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
9004 KB |
Output is correct |
14 |
Correct |
4 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
4 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
4 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
7 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8996 KB |
Output is correct |
25 |
Correct |
6 ms |
8916 KB |
Output is correct |
26 |
Correct |
6 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8916 KB |
Output is correct |
28 |
Correct |
6 ms |
8916 KB |
Output is correct |
29 |
Correct |
5 ms |
8984 KB |
Output is correct |
30 |
Correct |
5 ms |
8916 KB |
Output is correct |
31 |
Correct |
5 ms |
9004 KB |
Output is correct |
32 |
Correct |
6 ms |
8916 KB |
Output is correct |
33 |
Correct |
6 ms |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
5 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8940 KB |
Output is correct |
38 |
Correct |
5 ms |
8916 KB |
Output is correct |
39 |
Correct |
138 ms |
14644 KB |
Output is correct |
40 |
Correct |
137 ms |
15944 KB |
Output is correct |
41 |
Correct |
162 ms |
16180 KB |
Output is correct |
42 |
Correct |
155 ms |
16340 KB |
Output is correct |
43 |
Correct |
72 ms |
16216 KB |
Output is correct |
44 |
Correct |
56 ms |
16048 KB |
Output is correct |
45 |
Correct |
140 ms |
21092 KB |
Output is correct |
46 |
Correct |
113 ms |
22180 KB |
Output is correct |
47 |
Correct |
162 ms |
23304 KB |
Output is correct |
48 |
Correct |
236 ms |
33728 KB |
Output is correct |
49 |
Correct |
237 ms |
29664 KB |
Output is correct |
50 |
Correct |
163 ms |
23252 KB |
Output is correct |
51 |
Correct |
157 ms |
23212 KB |
Output is correct |
52 |
Correct |
193 ms |
23300 KB |
Output is correct |
53 |
Correct |
211 ms |
22096 KB |
Output is correct |
54 |
Correct |
172 ms |
22912 KB |
Output is correct |
55 |
Correct |
14 ms |
9620 KB |
Output is correct |
56 |
Correct |
16 ms |
9620 KB |
Output is correct |
57 |
Correct |
104 ms |
16652 KB |
Output is correct |
58 |
Correct |
40 ms |
16696 KB |
Output is correct |
59 |
Correct |
122 ms |
22096 KB |
Output is correct |
60 |
Correct |
523 ms |
30624 KB |
Output is correct |
61 |
Correct |
215 ms |
23744 KB |
Output is correct |
62 |
Correct |
248 ms |
24268 KB |
Output is correct |
63 |
Correct |
292 ms |
24160 KB |
Output is correct |
64 |
Correct |
565 ms |
24504 KB |
Output is correct |
65 |
Correct |
201 ms |
24268 KB |
Output is correct |
66 |
Correct |
390 ms |
32816 KB |
Output is correct |
67 |
Correct |
105 ms |
25712 KB |
Output is correct |
68 |
Correct |
274 ms |
24700 KB |
Output is correct |
69 |
Correct |
258 ms |
24648 KB |
Output is correct |
70 |
Correct |
255 ms |
23852 KB |
Output is correct |