#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 2e5 + 7;
const int K = 1e6 + 7;
const int LG = 20;
const int inf = 1e9 + 7;
int n, k;
int sz[N];
bool used[N];
vector <pair <int, int> > g[N];
vector <int> dp(K, inf);
vector <int> kek(K, inf);
vector <int> go;
int ans = inf;
void calc(int v, int pr) {
sz[v] = 1;
for (auto to : g[v]) {
if (to.first != pr && !used[to.first]) {
calc(to.first, v);
sz[v] += sz[to.first];
}
}
}
int centroid(int v, int pr, int need) {
for (auto to : g[v]) {
if (to.first != pr && !used[to.first] && sz[to.first] > need / 2) {
return centroid(to.first, v, need);
}
}
return v;
}
void dfs(int v, int pr, int h, int cur) {
if (cur > k) {
return;
}
go.push_back(cur);
kek[cur] = min(kek[cur], h);
for (auto to : g[v]) {
if (to.first != pr && !used[to.first]) {
dfs(to.first, v, h + 1, cur + to.second);
}
}
}
void build(int v, int lg) {
calc(v, -1);
v = centroid(v, -1, sz[v]);
dp[0] = 0;
kek[0] = 0;
vector <int> kekos;
for (auto to : g[v]) {
if (!used[to.first]) {
dfs(to.first, v, 1, to.second);
go.push_back(0);
for (int x : go) {
ans = min(ans, kek[x] + dp[k - x]);
}
for (int x : go) {
dp[x] = min(dp[x], kek[x]);
kek[x] = inf;
}
while (!go.empty()) {
kekos.push_back(go.back());
go.pop_back();
}
}
}
for (int x : kekos) {
dp[x] = inf;
}
used[v] = true;
for (auto to : g[v]) {
if (!used[to.first]) {
build(to.first, lg + 1);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for (int i = 0; i < n - 1; i++) {
int u, v, l;
u = H[i][0];
v = H[i][1];
l = L[i];
g[u].push_back({v, l});
g[v].push_back({u, l});
}
build(0, 0);
return (ans == inf ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12928 KB |
Output is correct |
2 |
Correct |
16 ms |
12928 KB |
Output is correct |
3 |
Correct |
13 ms |
12928 KB |
Output is correct |
4 |
Correct |
14 ms |
12928 KB |
Output is correct |
5 |
Correct |
10 ms |
12920 KB |
Output is correct |
6 |
Correct |
18 ms |
12928 KB |
Output is correct |
7 |
Correct |
16 ms |
12928 KB |
Output is correct |
8 |
Correct |
14 ms |
12928 KB |
Output is correct |
9 |
Correct |
15 ms |
12928 KB |
Output is correct |
10 |
Correct |
14 ms |
12928 KB |
Output is correct |
11 |
Correct |
17 ms |
12920 KB |
Output is correct |
12 |
Correct |
16 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
15 ms |
12928 KB |
Output is correct |
15 |
Correct |
14 ms |
12928 KB |
Output is correct |
16 |
Correct |
14 ms |
12928 KB |
Output is correct |
17 |
Correct |
13 ms |
12928 KB |
Output is correct |
18 |
Correct |
15 ms |
12928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12928 KB |
Output is correct |
2 |
Correct |
16 ms |
12928 KB |
Output is correct |
3 |
Correct |
13 ms |
12928 KB |
Output is correct |
4 |
Correct |
14 ms |
12928 KB |
Output is correct |
5 |
Correct |
10 ms |
12920 KB |
Output is correct |
6 |
Correct |
18 ms |
12928 KB |
Output is correct |
7 |
Correct |
16 ms |
12928 KB |
Output is correct |
8 |
Correct |
14 ms |
12928 KB |
Output is correct |
9 |
Correct |
15 ms |
12928 KB |
Output is correct |
10 |
Correct |
14 ms |
12928 KB |
Output is correct |
11 |
Correct |
17 ms |
12920 KB |
Output is correct |
12 |
Correct |
16 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
15 ms |
12928 KB |
Output is correct |
15 |
Correct |
14 ms |
12928 KB |
Output is correct |
16 |
Correct |
14 ms |
12928 KB |
Output is correct |
17 |
Correct |
13 ms |
12928 KB |
Output is correct |
18 |
Correct |
15 ms |
12928 KB |
Output is correct |
19 |
Correct |
14 ms |
12928 KB |
Output is correct |
20 |
Correct |
14 ms |
12928 KB |
Output is correct |
21 |
Correct |
14 ms |
12928 KB |
Output is correct |
22 |
Correct |
14 ms |
12928 KB |
Output is correct |
23 |
Correct |
17 ms |
12928 KB |
Output is correct |
24 |
Correct |
16 ms |
13056 KB |
Output is correct |
25 |
Correct |
16 ms |
13056 KB |
Output is correct |
26 |
Correct |
14 ms |
12928 KB |
Output is correct |
27 |
Correct |
15 ms |
12928 KB |
Output is correct |
28 |
Correct |
17 ms |
12928 KB |
Output is correct |
29 |
Correct |
15 ms |
13056 KB |
Output is correct |
30 |
Correct |
17 ms |
12976 KB |
Output is correct |
31 |
Correct |
14 ms |
12928 KB |
Output is correct |
32 |
Correct |
17 ms |
12928 KB |
Output is correct |
33 |
Correct |
14 ms |
12928 KB |
Output is correct |
34 |
Correct |
17 ms |
12928 KB |
Output is correct |
35 |
Correct |
16 ms |
12928 KB |
Output is correct |
36 |
Correct |
15 ms |
12928 KB |
Output is correct |
37 |
Correct |
14 ms |
12928 KB |
Output is correct |
38 |
Correct |
18 ms |
12928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12928 KB |
Output is correct |
2 |
Correct |
16 ms |
12928 KB |
Output is correct |
3 |
Correct |
13 ms |
12928 KB |
Output is correct |
4 |
Correct |
14 ms |
12928 KB |
Output is correct |
5 |
Correct |
10 ms |
12920 KB |
Output is correct |
6 |
Correct |
18 ms |
12928 KB |
Output is correct |
7 |
Correct |
16 ms |
12928 KB |
Output is correct |
8 |
Correct |
14 ms |
12928 KB |
Output is correct |
9 |
Correct |
15 ms |
12928 KB |
Output is correct |
10 |
Correct |
14 ms |
12928 KB |
Output is correct |
11 |
Correct |
17 ms |
12920 KB |
Output is correct |
12 |
Correct |
16 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
15 ms |
12928 KB |
Output is correct |
15 |
Correct |
14 ms |
12928 KB |
Output is correct |
16 |
Correct |
14 ms |
12928 KB |
Output is correct |
17 |
Correct |
13 ms |
12928 KB |
Output is correct |
18 |
Correct |
15 ms |
12928 KB |
Output is correct |
19 |
Correct |
236 ms |
18440 KB |
Output is correct |
20 |
Correct |
196 ms |
18412 KB |
Output is correct |
21 |
Correct |
227 ms |
18804 KB |
Output is correct |
22 |
Correct |
204 ms |
19184 KB |
Output is correct |
23 |
Correct |
159 ms |
18488 KB |
Output is correct |
24 |
Correct |
103 ms |
18348 KB |
Output is correct |
25 |
Correct |
293 ms |
21112 KB |
Output is correct |
26 |
Correct |
158 ms |
25128 KB |
Output is correct |
27 |
Correct |
293 ms |
23852 KB |
Output is correct |
28 |
Correct |
559 ms |
34936 KB |
Output is correct |
29 |
Correct |
616 ms |
34040 KB |
Output is correct |
30 |
Correct |
273 ms |
23936 KB |
Output is correct |
31 |
Correct |
255 ms |
23708 KB |
Output is correct |
32 |
Correct |
296 ms |
23928 KB |
Output is correct |
33 |
Correct |
493 ms |
22816 KB |
Output is correct |
34 |
Correct |
427 ms |
22648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12928 KB |
Output is correct |
2 |
Correct |
16 ms |
12928 KB |
Output is correct |
3 |
Correct |
13 ms |
12928 KB |
Output is correct |
4 |
Correct |
14 ms |
12928 KB |
Output is correct |
5 |
Correct |
10 ms |
12920 KB |
Output is correct |
6 |
Correct |
18 ms |
12928 KB |
Output is correct |
7 |
Correct |
16 ms |
12928 KB |
Output is correct |
8 |
Correct |
14 ms |
12928 KB |
Output is correct |
9 |
Correct |
15 ms |
12928 KB |
Output is correct |
10 |
Correct |
14 ms |
12928 KB |
Output is correct |
11 |
Correct |
17 ms |
12920 KB |
Output is correct |
12 |
Correct |
16 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
15 ms |
12928 KB |
Output is correct |
15 |
Correct |
14 ms |
12928 KB |
Output is correct |
16 |
Correct |
14 ms |
12928 KB |
Output is correct |
17 |
Correct |
13 ms |
12928 KB |
Output is correct |
18 |
Correct |
15 ms |
12928 KB |
Output is correct |
19 |
Correct |
14 ms |
12928 KB |
Output is correct |
20 |
Correct |
14 ms |
12928 KB |
Output is correct |
21 |
Correct |
14 ms |
12928 KB |
Output is correct |
22 |
Correct |
14 ms |
12928 KB |
Output is correct |
23 |
Correct |
17 ms |
12928 KB |
Output is correct |
24 |
Correct |
16 ms |
13056 KB |
Output is correct |
25 |
Correct |
16 ms |
13056 KB |
Output is correct |
26 |
Correct |
14 ms |
12928 KB |
Output is correct |
27 |
Correct |
15 ms |
12928 KB |
Output is correct |
28 |
Correct |
17 ms |
12928 KB |
Output is correct |
29 |
Correct |
15 ms |
13056 KB |
Output is correct |
30 |
Correct |
17 ms |
12976 KB |
Output is correct |
31 |
Correct |
14 ms |
12928 KB |
Output is correct |
32 |
Correct |
17 ms |
12928 KB |
Output is correct |
33 |
Correct |
14 ms |
12928 KB |
Output is correct |
34 |
Correct |
17 ms |
12928 KB |
Output is correct |
35 |
Correct |
16 ms |
12928 KB |
Output is correct |
36 |
Correct |
15 ms |
12928 KB |
Output is correct |
37 |
Correct |
14 ms |
12928 KB |
Output is correct |
38 |
Correct |
18 ms |
12928 KB |
Output is correct |
39 |
Correct |
236 ms |
18440 KB |
Output is correct |
40 |
Correct |
196 ms |
18412 KB |
Output is correct |
41 |
Correct |
227 ms |
18804 KB |
Output is correct |
42 |
Correct |
204 ms |
19184 KB |
Output is correct |
43 |
Correct |
159 ms |
18488 KB |
Output is correct |
44 |
Correct |
103 ms |
18348 KB |
Output is correct |
45 |
Correct |
293 ms |
21112 KB |
Output is correct |
46 |
Correct |
158 ms |
25128 KB |
Output is correct |
47 |
Correct |
293 ms |
23852 KB |
Output is correct |
48 |
Correct |
559 ms |
34936 KB |
Output is correct |
49 |
Correct |
616 ms |
34040 KB |
Output is correct |
50 |
Correct |
273 ms |
23936 KB |
Output is correct |
51 |
Correct |
255 ms |
23708 KB |
Output is correct |
52 |
Correct |
296 ms |
23928 KB |
Output is correct |
53 |
Correct |
493 ms |
22816 KB |
Output is correct |
54 |
Correct |
427 ms |
22648 KB |
Output is correct |
55 |
Correct |
25 ms |
13668 KB |
Output is correct |
56 |
Correct |
27 ms |
13568 KB |
Output is correct |
57 |
Correct |
109 ms |
19312 KB |
Output is correct |
58 |
Correct |
55 ms |
19948 KB |
Output is correct |
59 |
Correct |
123 ms |
25196 KB |
Output is correct |
60 |
Correct |
897 ms |
39476 KB |
Output is correct |
61 |
Correct |
306 ms |
27200 KB |
Output is correct |
62 |
Correct |
304 ms |
27640 KB |
Output is correct |
63 |
Correct |
432 ms |
27640 KB |
Output is correct |
64 |
Correct |
791 ms |
29004 KB |
Output is correct |
65 |
Correct |
366 ms |
27512 KB |
Output is correct |
66 |
Correct |
718 ms |
35324 KB |
Output is correct |
67 |
Correct |
228 ms |
30412 KB |
Output is correct |
68 |
Correct |
432 ms |
28628 KB |
Output is correct |
69 |
Correct |
429 ms |
28928 KB |
Output is correct |
70 |
Correct |
378 ms |
28024 KB |
Output is correct |