#include "race.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 2e5 + 5;
const int M = 1e6 + 5;
int n, k;
int ans = -1, centroid, mxnode, cnt = 0;
int dep[N], path[M], dp[M];
vector<vector<pii> > g(N);
bitset<N> chk;
int subtree(int u, int p) {
dep[u] = 1;
for(pii v : g[u]) if(!chk[v.x] && v.x != p) {
dep[u] += subtree(v.x, u);
}
return dep[u];
}
void centdecomp(int u, int p, int all) {
int ret = all - dep[u];
for(pii v : g[u]) if(!chk[v.x] && v.x != p) {
centdecomp(v.x, u, all);
ret = max(ret, dep[v.x]);
}
if(ret < mxnode) centroid = u, mxnode = ret;
}
void dfs(int u, int p, int cost, int edge, bool fill) {
if(cost > k) return;
if(!fill) {
if(dp[k - cost] == cnt && (edge + path[k - cost] < ans || ans == -1)) ans = edge + path[k - cost];
if(cost == k && (edge < ans || ans == -1)) ans = edge;
} else if(dp[cost] < cnt || edge < path[cost]) dp[cost] = cnt, path[cost] = edge;
for(pii v : g[u]) if(!chk[v.x] && v.x != p) {
dfs(v.x, u, cost + v.y, edge + 1, fill);
}
}
void proc(int u) {
cnt++;
if(subtree(u, -1) <= 1) return;
mxnode = dep[u] + 1;
centdecomp(u, -1, dep[u]);
for(pii v : g[centroid]) if(!chk[v.x]) {
dfs(v.x, centroid, v.y, 1, false);
dfs(v.x, centroid, v.y, 1, true);
}
chk[centroid] = 1;
for(pii v : g[centroid]) if(!chk[v.x]) {
proc(v.x);
}
}
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]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
proc(0);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5200 KB |
Output is correct |
4 |
Correct |
6 ms |
5236 KB |
Output is correct |
5 |
Correct |
6 ms |
5264 KB |
Output is correct |
6 |
Correct |
6 ms |
5268 KB |
Output is correct |
7 |
Correct |
7 ms |
5272 KB |
Output is correct |
8 |
Correct |
6 ms |
5292 KB |
Output is correct |
9 |
Correct |
6 ms |
5344 KB |
Output is correct |
10 |
Correct |
6 ms |
5348 KB |
Output is correct |
11 |
Correct |
6 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5360 KB |
Output is correct |
13 |
Correct |
6 ms |
5364 KB |
Output is correct |
14 |
Correct |
6 ms |
5368 KB |
Output is correct |
15 |
Correct |
6 ms |
5372 KB |
Output is correct |
16 |
Correct |
6 ms |
5376 KB |
Output is correct |
17 |
Correct |
7 ms |
5380 KB |
Output is correct |
18 |
Correct |
6 ms |
5384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5200 KB |
Output is correct |
4 |
Correct |
6 ms |
5236 KB |
Output is correct |
5 |
Correct |
6 ms |
5264 KB |
Output is correct |
6 |
Correct |
6 ms |
5268 KB |
Output is correct |
7 |
Correct |
7 ms |
5272 KB |
Output is correct |
8 |
Correct |
6 ms |
5292 KB |
Output is correct |
9 |
Correct |
6 ms |
5344 KB |
Output is correct |
10 |
Correct |
6 ms |
5348 KB |
Output is correct |
11 |
Correct |
6 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5360 KB |
Output is correct |
13 |
Correct |
6 ms |
5364 KB |
Output is correct |
14 |
Correct |
6 ms |
5368 KB |
Output is correct |
15 |
Correct |
6 ms |
5372 KB |
Output is correct |
16 |
Correct |
6 ms |
5376 KB |
Output is correct |
17 |
Correct |
7 ms |
5380 KB |
Output is correct |
18 |
Correct |
6 ms |
5384 KB |
Output is correct |
19 |
Correct |
6 ms |
5384 KB |
Output is correct |
20 |
Correct |
6 ms |
5396 KB |
Output is correct |
21 |
Correct |
7 ms |
5396 KB |
Output is correct |
22 |
Correct |
15 ms |
10400 KB |
Output is correct |
23 |
Correct |
12 ms |
10400 KB |
Output is correct |
24 |
Correct |
13 ms |
11188 KB |
Output is correct |
25 |
Correct |
15 ms |
11188 KB |
Output is correct |
26 |
Correct |
10 ms |
11188 KB |
Output is correct |
27 |
Correct |
7 ms |
11188 KB |
Output is correct |
28 |
Correct |
9 ms |
11188 KB |
Output is correct |
29 |
Correct |
10 ms |
11188 KB |
Output is correct |
30 |
Correct |
10 ms |
11188 KB |
Output is correct |
31 |
Correct |
11 ms |
11188 KB |
Output is correct |
32 |
Correct |
12 ms |
11188 KB |
Output is correct |
33 |
Correct |
13 ms |
11188 KB |
Output is correct |
34 |
Correct |
12 ms |
11188 KB |
Output is correct |
35 |
Correct |
13 ms |
11564 KB |
Output is correct |
36 |
Correct |
17 ms |
12088 KB |
Output is correct |
37 |
Correct |
11 ms |
12088 KB |
Output is correct |
38 |
Correct |
9 ms |
12088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5200 KB |
Output is correct |
4 |
Correct |
6 ms |
5236 KB |
Output is correct |
5 |
Correct |
6 ms |
5264 KB |
Output is correct |
6 |
Correct |
6 ms |
5268 KB |
Output is correct |
7 |
Correct |
7 ms |
5272 KB |
Output is correct |
8 |
Correct |
6 ms |
5292 KB |
Output is correct |
9 |
Correct |
6 ms |
5344 KB |
Output is correct |
10 |
Correct |
6 ms |
5348 KB |
Output is correct |
11 |
Correct |
6 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5360 KB |
Output is correct |
13 |
Correct |
6 ms |
5364 KB |
Output is correct |
14 |
Correct |
6 ms |
5368 KB |
Output is correct |
15 |
Correct |
6 ms |
5372 KB |
Output is correct |
16 |
Correct |
6 ms |
5376 KB |
Output is correct |
17 |
Correct |
7 ms |
5380 KB |
Output is correct |
18 |
Correct |
6 ms |
5384 KB |
Output is correct |
19 |
Correct |
208 ms |
12304 KB |
Output is correct |
20 |
Correct |
219 ms |
13732 KB |
Output is correct |
21 |
Correct |
202 ms |
15084 KB |
Output is correct |
22 |
Correct |
193 ms |
16564 KB |
Output is correct |
23 |
Correct |
134 ms |
18080 KB |
Output is correct |
24 |
Correct |
88 ms |
19384 KB |
Output is correct |
25 |
Correct |
227 ms |
24276 KB |
Output is correct |
26 |
Correct |
140 ms |
29156 KB |
Output is correct |
27 |
Correct |
256 ms |
30396 KB |
Output is correct |
28 |
Correct |
439 ms |
47676 KB |
Output is correct |
29 |
Correct |
557 ms |
49736 KB |
Output is correct |
30 |
Correct |
273 ms |
49736 KB |
Output is correct |
31 |
Correct |
244 ms |
49736 KB |
Output is correct |
32 |
Correct |
284 ms |
49736 KB |
Output is correct |
33 |
Correct |
477 ms |
49736 KB |
Output is correct |
34 |
Correct |
418 ms |
51036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5200 KB |
Output is correct |
4 |
Correct |
6 ms |
5236 KB |
Output is correct |
5 |
Correct |
6 ms |
5264 KB |
Output is correct |
6 |
Correct |
6 ms |
5268 KB |
Output is correct |
7 |
Correct |
7 ms |
5272 KB |
Output is correct |
8 |
Correct |
6 ms |
5292 KB |
Output is correct |
9 |
Correct |
6 ms |
5344 KB |
Output is correct |
10 |
Correct |
6 ms |
5348 KB |
Output is correct |
11 |
Correct |
6 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5360 KB |
Output is correct |
13 |
Correct |
6 ms |
5364 KB |
Output is correct |
14 |
Correct |
6 ms |
5368 KB |
Output is correct |
15 |
Correct |
6 ms |
5372 KB |
Output is correct |
16 |
Correct |
6 ms |
5376 KB |
Output is correct |
17 |
Correct |
7 ms |
5380 KB |
Output is correct |
18 |
Correct |
6 ms |
5384 KB |
Output is correct |
19 |
Correct |
6 ms |
5384 KB |
Output is correct |
20 |
Correct |
6 ms |
5396 KB |
Output is correct |
21 |
Correct |
7 ms |
5396 KB |
Output is correct |
22 |
Correct |
15 ms |
10400 KB |
Output is correct |
23 |
Correct |
12 ms |
10400 KB |
Output is correct |
24 |
Correct |
13 ms |
11188 KB |
Output is correct |
25 |
Correct |
15 ms |
11188 KB |
Output is correct |
26 |
Correct |
10 ms |
11188 KB |
Output is correct |
27 |
Correct |
7 ms |
11188 KB |
Output is correct |
28 |
Correct |
9 ms |
11188 KB |
Output is correct |
29 |
Correct |
10 ms |
11188 KB |
Output is correct |
30 |
Correct |
10 ms |
11188 KB |
Output is correct |
31 |
Correct |
11 ms |
11188 KB |
Output is correct |
32 |
Correct |
12 ms |
11188 KB |
Output is correct |
33 |
Correct |
13 ms |
11188 KB |
Output is correct |
34 |
Correct |
12 ms |
11188 KB |
Output is correct |
35 |
Correct |
13 ms |
11564 KB |
Output is correct |
36 |
Correct |
17 ms |
12088 KB |
Output is correct |
37 |
Correct |
11 ms |
12088 KB |
Output is correct |
38 |
Correct |
9 ms |
12088 KB |
Output is correct |
39 |
Correct |
208 ms |
12304 KB |
Output is correct |
40 |
Correct |
219 ms |
13732 KB |
Output is correct |
41 |
Correct |
202 ms |
15084 KB |
Output is correct |
42 |
Correct |
193 ms |
16564 KB |
Output is correct |
43 |
Correct |
134 ms |
18080 KB |
Output is correct |
44 |
Correct |
88 ms |
19384 KB |
Output is correct |
45 |
Correct |
227 ms |
24276 KB |
Output is correct |
46 |
Correct |
140 ms |
29156 KB |
Output is correct |
47 |
Correct |
256 ms |
30396 KB |
Output is correct |
48 |
Correct |
439 ms |
47676 KB |
Output is correct |
49 |
Correct |
557 ms |
49736 KB |
Output is correct |
50 |
Correct |
273 ms |
49736 KB |
Output is correct |
51 |
Correct |
244 ms |
49736 KB |
Output is correct |
52 |
Correct |
284 ms |
49736 KB |
Output is correct |
53 |
Correct |
477 ms |
49736 KB |
Output is correct |
54 |
Correct |
418 ms |
51036 KB |
Output is correct |
55 |
Correct |
18 ms |
51036 KB |
Output is correct |
56 |
Correct |
20 ms |
51036 KB |
Output is correct |
57 |
Correct |
115 ms |
51036 KB |
Output is correct |
58 |
Correct |
47 ms |
51036 KB |
Output is correct |
59 |
Correct |
141 ms |
58640 KB |
Output is correct |
60 |
Correct |
700 ms |
77232 KB |
Output is correct |
61 |
Correct |
255 ms |
77232 KB |
Output is correct |
62 |
Correct |
267 ms |
77232 KB |
Output is correct |
63 |
Correct |
396 ms |
77232 KB |
Output is correct |
64 |
Correct |
729 ms |
77232 KB |
Output is correct |
65 |
Correct |
412 ms |
77232 KB |
Output is correct |
66 |
Correct |
728 ms |
85308 KB |
Output is correct |
67 |
Correct |
183 ms |
85308 KB |
Output is correct |
68 |
Correct |
426 ms |
85308 KB |
Output is correct |
69 |
Correct |
456 ms |
85308 KB |
Output is correct |
70 |
Correct |
435 ms |
85308 KB |
Output is correct |