#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+10;
const int MAX = 1e6+10;
int n, k;
vector< pair<int,int> > g[200010];
int sub[N];
bool Deleted[N];
void dfs(int u, int f) {
sub[u] = 1;
for(auto it : g[u]) if(it.first != f && !Deleted[it.first]) {
dfs(it.first, u);
sub[u] += sub[it.first];
}
}
int get_centroid(int u, int f, int lim) {
int ret = -1;
for(auto it : g[u]) if(it.first != f && !Deleted[it.first]) {
if(sub[it.first] * 2 > lim) {
ret = get_centroid(it.first, u, lim);
break;
}
} if(ret == -1) return u;
return ret;
}
int F[MAX];
int get_subtree(int u, int f, int cost, int cnt) {
int ret = N + N;
if(cost <= k) ret = F[k - cost] + cnt;
if(cost == k) ret = min(ret, cnt);
for(auto it : g[u]) if(it.first != f && !Deleted[it.first] && cost + it.second <= k)
ret = min(ret, get_subtree(it.first, u, cost + it.second, cnt + 1));
return ret;
}
void upd_subtree(int u, int f, int cost, int cnt) {
if(cost < MAX) F[cost] = min(F[cost], cnt);
for(auto it : g[u]) if(it.first != f && !Deleted[it.first] && cost + it.second <= k)
upd_subtree(it.first, u, cost + it.second, cnt + 1);
}
int calc(int u) {
for(int i = 0; i <= k; i++) F[i] = N+N;
int ans = N + N;
for(auto it : g[u]) if(!Deleted[it.first]) {
ans = min(ans, get_subtree(it.first, u, it.second, 1));
upd_subtree(it.first, u, it.second, 1);
} return ans;
}
int solve(int s) {
dfs(s,-1);
int u = get_centroid(s, -1, sub[s]);
int ret = calc(u);
Deleted[u] = true;
for(auto it : g[u]) if(!Deleted[it.first])
ret = min(ret, solve(it.first));
return ret;
}
int best_path(int N_, int K, int H[][2], int L[]) {
n = N_; k = K;
for(int i = 0; i < n-1; i++) {
g[H[i][0]].push_back({H[i][1], L[i]});
g[H[i][1]].push_back({H[i][0], L[i]});
}
int ans = solve(0);
if(ans >= N+N) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
4992 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
4992 KB |
Output is correct |
15 |
Correct |
8 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
4992 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
4992 KB |
Output is correct |
15 |
Correct |
8 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
480 ms |
8908 KB |
Output is correct |
23 |
Correct |
474 ms |
8824 KB |
Output is correct |
24 |
Correct |
464 ms |
8512 KB |
Output is correct |
25 |
Correct |
414 ms |
8440 KB |
Output is correct |
26 |
Correct |
166 ms |
6520 KB |
Output is correct |
27 |
Correct |
440 ms |
8284 KB |
Output is correct |
28 |
Correct |
110 ms |
5888 KB |
Output is correct |
29 |
Correct |
167 ms |
6416 KB |
Output is correct |
30 |
Correct |
175 ms |
6528 KB |
Output is correct |
31 |
Correct |
306 ms |
7680 KB |
Output is correct |
32 |
Correct |
346 ms |
7928 KB |
Output is correct |
33 |
Correct |
382 ms |
8204 KB |
Output is correct |
34 |
Correct |
255 ms |
7424 KB |
Output is correct |
35 |
Correct |
320 ms |
8320 KB |
Output is correct |
36 |
Correct |
406 ms |
8824 KB |
Output is correct |
37 |
Correct |
385 ms |
8192 KB |
Output is correct |
38 |
Correct |
231 ms |
7040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
4992 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
4992 KB |
Output is correct |
15 |
Correct |
8 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
205 ms |
10436 KB |
Output is correct |
20 |
Correct |
187 ms |
10488 KB |
Output is correct |
21 |
Correct |
170 ms |
10360 KB |
Output is correct |
22 |
Correct |
187 ms |
10460 KB |
Output is correct |
23 |
Correct |
129 ms |
10616 KB |
Output is correct |
24 |
Correct |
108 ms |
10488 KB |
Output is correct |
25 |
Correct |
195 ms |
13204 KB |
Output is correct |
26 |
Correct |
151 ms |
16220 KB |
Output is correct |
27 |
Correct |
272 ms |
15836 KB |
Output is correct |
28 |
Correct |
515 ms |
27100 KB |
Output is correct |
29 |
Correct |
552 ms |
26232 KB |
Output is correct |
30 |
Correct |
318 ms |
15856 KB |
Output is correct |
31 |
Correct |
279 ms |
15880 KB |
Output is correct |
32 |
Correct |
329 ms |
16124 KB |
Output is correct |
33 |
Correct |
408 ms |
14840 KB |
Output is correct |
34 |
Correct |
414 ms |
18680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
4992 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
4992 KB |
Output is correct |
15 |
Correct |
8 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
480 ms |
8908 KB |
Output is correct |
23 |
Correct |
474 ms |
8824 KB |
Output is correct |
24 |
Correct |
464 ms |
8512 KB |
Output is correct |
25 |
Correct |
414 ms |
8440 KB |
Output is correct |
26 |
Correct |
166 ms |
6520 KB |
Output is correct |
27 |
Correct |
440 ms |
8284 KB |
Output is correct |
28 |
Correct |
110 ms |
5888 KB |
Output is correct |
29 |
Correct |
167 ms |
6416 KB |
Output is correct |
30 |
Correct |
175 ms |
6528 KB |
Output is correct |
31 |
Correct |
306 ms |
7680 KB |
Output is correct |
32 |
Correct |
346 ms |
7928 KB |
Output is correct |
33 |
Correct |
382 ms |
8204 KB |
Output is correct |
34 |
Correct |
255 ms |
7424 KB |
Output is correct |
35 |
Correct |
320 ms |
8320 KB |
Output is correct |
36 |
Correct |
406 ms |
8824 KB |
Output is correct |
37 |
Correct |
385 ms |
8192 KB |
Output is correct |
38 |
Correct |
231 ms |
7040 KB |
Output is correct |
39 |
Correct |
205 ms |
10436 KB |
Output is correct |
40 |
Correct |
187 ms |
10488 KB |
Output is correct |
41 |
Correct |
170 ms |
10360 KB |
Output is correct |
42 |
Correct |
187 ms |
10460 KB |
Output is correct |
43 |
Correct |
129 ms |
10616 KB |
Output is correct |
44 |
Correct |
108 ms |
10488 KB |
Output is correct |
45 |
Correct |
195 ms |
13204 KB |
Output is correct |
46 |
Correct |
151 ms |
16220 KB |
Output is correct |
47 |
Correct |
272 ms |
15836 KB |
Output is correct |
48 |
Correct |
515 ms |
27100 KB |
Output is correct |
49 |
Correct |
552 ms |
26232 KB |
Output is correct |
50 |
Correct |
318 ms |
15856 KB |
Output is correct |
51 |
Correct |
279 ms |
15880 KB |
Output is correct |
52 |
Correct |
329 ms |
16124 KB |
Output is correct |
53 |
Correct |
408 ms |
14840 KB |
Output is correct |
54 |
Correct |
414 ms |
18680 KB |
Output is correct |
55 |
Correct |
42 ms |
5616 KB |
Output is correct |
56 |
Correct |
17 ms |
5504 KB |
Output is correct |
57 |
Correct |
104 ms |
10616 KB |
Output is correct |
58 |
Correct |
54 ms |
10732 KB |
Output is correct |
59 |
Execution timed out |
3099 ms |
16960 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |