#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define int long long
struct edge {
int v;
int w;
};
const int MAX_N = 2e5 + 10;
const int mxK = 1e6 + 10;
const int inf = 1e18;
vector<edge> g[MAX_N];
int sz[MAX_N];
bool mark[MAX_N];
int cnt[mxK];
int mn[mxK];
int res = LLONG_MAX;
void get_sz(int u, int par) {
if(mark[u]) return;
sz[u] = 1;
for(edge v : g[u]) {
if(v.v == par || mark[v.v]) continue;
get_sz(v.v, u);
sz[u] += sz[v.v];
}
}
void Add(int u, int par, int w, int k, int edges_count) {
if(mark[u]) return;
if(w > k) return;
cnt[w]++;
mn[w] = min(mn[w], edges_count);
for(edge v : g[u]) {
if(v.v == par || mark[v.v]) continue;
Add(v.v, u, w + v.w, k, edges_count + 1);
}
}
void Remove(int u, int par, int w, int k) {
if(mark[u]) return;
if(w > k) return;
mn[w] = inf;
cnt[w] = 0;
for(edge v : g[u]) {
if(v.v == par || mark[v.v]) continue;
Remove(v.v, u, w + v.w, k);
}
}
void dfs(int u, int par, int w, int k, int edges_count) {
if(mark[u]) return;
if(w >= k) return;
if(cnt[k - w] > 0) res = min(res, edges_count + mn[k - w]);
for(edge v : g[u]) {
if(v.v == par || mark[v.v]) continue;
dfs(v.v, u, w + v.w, k, edges_count + 1);
}
}
int find_centroid(int u, int par, int n, int usize) {
for(edge v : g[u]) {
if(v.v == par || mark[v.v]) continue;
if(sz[v.v] * 2 > usize) return find_centroid(v.v, u, n, usize);
}
return u;
}
void solve(int u, int n, int k) {
get_sz(u, 0);
int centroid = find_centroid(u, 0, n, sz[u]);
mark[centroid] = true;
for(edge v : g[centroid]) {
if(mark[v.v]) continue;
dfs(v.v, centroid, v.w, k, 1);
Add(v.v, centroid, v.w, k, 1);
}
if(cnt[k] > 0) {
res = min(res, mn[k]);
assert(mn[k] > 0);
}
for(edge v : g[centroid]) {
if(mark[v.v]) continue;
Remove(v.v, centroid, v.w, k);
}
for(edge v : g[centroid]) {
if(mark[v.v]) continue;
solve(v.v, n, k);
}
}
#undef int
int best_path(int n, int k, int h[][2], int l[]) {
for(int i = 0; i < n - 1; i++) {
g[h[i][0] + 1].push_back({h[i][1] + 1, l[i]});
g[h[i][1] + 1].push_back({h[i][0] + 1, l[i]});
}
for(int i = 0; i < mxK; i++) mn[i] = inf;
solve(1, n, k);
int ret = res;
if(ret == LLONG_MAX) return -1;
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12756 KB |
Output is correct |
3 |
Correct |
6 ms |
12756 KB |
Output is correct |
4 |
Correct |
9 ms |
12756 KB |
Output is correct |
5 |
Correct |
6 ms |
12816 KB |
Output is correct |
6 |
Correct |
9 ms |
12816 KB |
Output is correct |
7 |
Correct |
7 ms |
12816 KB |
Output is correct |
8 |
Correct |
7 ms |
12844 KB |
Output is correct |
9 |
Correct |
9 ms |
12812 KB |
Output is correct |
10 |
Correct |
7 ms |
12756 KB |
Output is correct |
11 |
Correct |
7 ms |
12812 KB |
Output is correct |
12 |
Correct |
6 ms |
12756 KB |
Output is correct |
13 |
Correct |
8 ms |
12756 KB |
Output is correct |
14 |
Correct |
7 ms |
12756 KB |
Output is correct |
15 |
Correct |
7 ms |
12756 KB |
Output is correct |
16 |
Correct |
8 ms |
12820 KB |
Output is correct |
17 |
Correct |
7 ms |
12808 KB |
Output is correct |
18 |
Correct |
8 ms |
12820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12756 KB |
Output is correct |
3 |
Correct |
6 ms |
12756 KB |
Output is correct |
4 |
Correct |
9 ms |
12756 KB |
Output is correct |
5 |
Correct |
6 ms |
12816 KB |
Output is correct |
6 |
Correct |
9 ms |
12816 KB |
Output is correct |
7 |
Correct |
7 ms |
12816 KB |
Output is correct |
8 |
Correct |
7 ms |
12844 KB |
Output is correct |
9 |
Correct |
9 ms |
12812 KB |
Output is correct |
10 |
Correct |
7 ms |
12756 KB |
Output is correct |
11 |
Correct |
7 ms |
12812 KB |
Output is correct |
12 |
Correct |
6 ms |
12756 KB |
Output is correct |
13 |
Correct |
8 ms |
12756 KB |
Output is correct |
14 |
Correct |
7 ms |
12756 KB |
Output is correct |
15 |
Correct |
7 ms |
12756 KB |
Output is correct |
16 |
Correct |
8 ms |
12820 KB |
Output is correct |
17 |
Correct |
7 ms |
12808 KB |
Output is correct |
18 |
Correct |
8 ms |
12820 KB |
Output is correct |
19 |
Correct |
9 ms |
12804 KB |
Output is correct |
20 |
Correct |
8 ms |
12756 KB |
Output is correct |
21 |
Correct |
8 ms |
12884 KB |
Output is correct |
22 |
Correct |
11 ms |
16252 KB |
Output is correct |
23 |
Correct |
9 ms |
15828 KB |
Output is correct |
24 |
Correct |
9 ms |
16964 KB |
Output is correct |
25 |
Correct |
12 ms |
16108 KB |
Output is correct |
26 |
Correct |
8 ms |
15272 KB |
Output is correct |
27 |
Correct |
9 ms |
12952 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
15316 KB |
Output is correct |
30 |
Correct |
9 ms |
15572 KB |
Output is correct |
31 |
Correct |
11 ms |
16204 KB |
Output is correct |
32 |
Correct |
11 ms |
16224 KB |
Output is correct |
33 |
Correct |
13 ms |
17620 KB |
Output is correct |
34 |
Correct |
12 ms |
16920 KB |
Output is correct |
35 |
Correct |
13 ms |
17812 KB |
Output is correct |
36 |
Correct |
9 ms |
18260 KB |
Output is correct |
37 |
Correct |
10 ms |
16084 KB |
Output is correct |
38 |
Correct |
8 ms |
14992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12756 KB |
Output is correct |
3 |
Correct |
6 ms |
12756 KB |
Output is correct |
4 |
Correct |
9 ms |
12756 KB |
Output is correct |
5 |
Correct |
6 ms |
12816 KB |
Output is correct |
6 |
Correct |
9 ms |
12816 KB |
Output is correct |
7 |
Correct |
7 ms |
12816 KB |
Output is correct |
8 |
Correct |
7 ms |
12844 KB |
Output is correct |
9 |
Correct |
9 ms |
12812 KB |
Output is correct |
10 |
Correct |
7 ms |
12756 KB |
Output is correct |
11 |
Correct |
7 ms |
12812 KB |
Output is correct |
12 |
Correct |
6 ms |
12756 KB |
Output is correct |
13 |
Correct |
8 ms |
12756 KB |
Output is correct |
14 |
Correct |
7 ms |
12756 KB |
Output is correct |
15 |
Correct |
7 ms |
12756 KB |
Output is correct |
16 |
Correct |
8 ms |
12820 KB |
Output is correct |
17 |
Correct |
7 ms |
12808 KB |
Output is correct |
18 |
Correct |
8 ms |
12820 KB |
Output is correct |
19 |
Correct |
172 ms |
21340 KB |
Output is correct |
20 |
Correct |
164 ms |
21240 KB |
Output is correct |
21 |
Correct |
209 ms |
21352 KB |
Output is correct |
22 |
Correct |
167 ms |
21308 KB |
Output is correct |
23 |
Correct |
81 ms |
21740 KB |
Output is correct |
24 |
Correct |
65 ms |
20996 KB |
Output is correct |
25 |
Correct |
151 ms |
24200 KB |
Output is correct |
26 |
Correct |
100 ms |
27096 KB |
Output is correct |
27 |
Correct |
180 ms |
30524 KB |
Output is correct |
28 |
Correct |
270 ms |
41876 KB |
Output is correct |
29 |
Correct |
267 ms |
40980 KB |
Output is correct |
30 |
Correct |
186 ms |
30568 KB |
Output is correct |
31 |
Correct |
157 ms |
30532 KB |
Output is correct |
32 |
Correct |
207 ms |
30672 KB |
Output is correct |
33 |
Correct |
270 ms |
29240 KB |
Output is correct |
34 |
Correct |
180 ms |
30156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12756 KB |
Output is correct |
3 |
Correct |
6 ms |
12756 KB |
Output is correct |
4 |
Correct |
9 ms |
12756 KB |
Output is correct |
5 |
Correct |
6 ms |
12816 KB |
Output is correct |
6 |
Correct |
9 ms |
12816 KB |
Output is correct |
7 |
Correct |
7 ms |
12816 KB |
Output is correct |
8 |
Correct |
7 ms |
12844 KB |
Output is correct |
9 |
Correct |
9 ms |
12812 KB |
Output is correct |
10 |
Correct |
7 ms |
12756 KB |
Output is correct |
11 |
Correct |
7 ms |
12812 KB |
Output is correct |
12 |
Correct |
6 ms |
12756 KB |
Output is correct |
13 |
Correct |
8 ms |
12756 KB |
Output is correct |
14 |
Correct |
7 ms |
12756 KB |
Output is correct |
15 |
Correct |
7 ms |
12756 KB |
Output is correct |
16 |
Correct |
8 ms |
12820 KB |
Output is correct |
17 |
Correct |
7 ms |
12808 KB |
Output is correct |
18 |
Correct |
8 ms |
12820 KB |
Output is correct |
19 |
Correct |
9 ms |
12804 KB |
Output is correct |
20 |
Correct |
8 ms |
12756 KB |
Output is correct |
21 |
Correct |
8 ms |
12884 KB |
Output is correct |
22 |
Correct |
11 ms |
16252 KB |
Output is correct |
23 |
Correct |
9 ms |
15828 KB |
Output is correct |
24 |
Correct |
9 ms |
16964 KB |
Output is correct |
25 |
Correct |
12 ms |
16108 KB |
Output is correct |
26 |
Correct |
8 ms |
15272 KB |
Output is correct |
27 |
Correct |
9 ms |
12952 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
29 |
Correct |
8 ms |
15316 KB |
Output is correct |
30 |
Correct |
9 ms |
15572 KB |
Output is correct |
31 |
Correct |
11 ms |
16204 KB |
Output is correct |
32 |
Correct |
11 ms |
16224 KB |
Output is correct |
33 |
Correct |
13 ms |
17620 KB |
Output is correct |
34 |
Correct |
12 ms |
16920 KB |
Output is correct |
35 |
Correct |
13 ms |
17812 KB |
Output is correct |
36 |
Correct |
9 ms |
18260 KB |
Output is correct |
37 |
Correct |
10 ms |
16084 KB |
Output is correct |
38 |
Correct |
8 ms |
14992 KB |
Output is correct |
39 |
Correct |
172 ms |
21340 KB |
Output is correct |
40 |
Correct |
164 ms |
21240 KB |
Output is correct |
41 |
Correct |
209 ms |
21352 KB |
Output is correct |
42 |
Correct |
167 ms |
21308 KB |
Output is correct |
43 |
Correct |
81 ms |
21740 KB |
Output is correct |
44 |
Correct |
65 ms |
20996 KB |
Output is correct |
45 |
Correct |
151 ms |
24200 KB |
Output is correct |
46 |
Correct |
100 ms |
27096 KB |
Output is correct |
47 |
Correct |
180 ms |
30524 KB |
Output is correct |
48 |
Correct |
270 ms |
41876 KB |
Output is correct |
49 |
Correct |
267 ms |
40980 KB |
Output is correct |
50 |
Correct |
186 ms |
30568 KB |
Output is correct |
51 |
Correct |
157 ms |
30532 KB |
Output is correct |
52 |
Correct |
207 ms |
30672 KB |
Output is correct |
53 |
Correct |
270 ms |
29240 KB |
Output is correct |
54 |
Correct |
180 ms |
30156 KB |
Output is correct |
55 |
Correct |
15 ms |
13652 KB |
Output is correct |
56 |
Correct |
17 ms |
13652 KB |
Output is correct |
57 |
Correct |
77 ms |
21668 KB |
Output is correct |
58 |
Correct |
41 ms |
20612 KB |
Output is correct |
59 |
Correct |
100 ms |
28048 KB |
Output is correct |
60 |
Correct |
487 ms |
45752 KB |
Output is correct |
61 |
Correct |
195 ms |
30660 KB |
Output is correct |
62 |
Correct |
205 ms |
30668 KB |
Output is correct |
63 |
Correct |
265 ms |
30720 KB |
Output is correct |
64 |
Correct |
579 ms |
32848 KB |
Output is correct |
65 |
Correct |
213 ms |
31084 KB |
Output is correct |
66 |
Correct |
424 ms |
46888 KB |
Output is correct |
67 |
Correct |
120 ms |
29744 KB |
Output is correct |
68 |
Correct |
247 ms |
36044 KB |
Output is correct |
69 |
Correct |
298 ms |
36048 KB |
Output is correct |
70 |
Correct |
255 ms |
35096 KB |
Output is correct |