#ifndef KienVu
#include "race.h"
#endif // KienVu
#include <bits/stdc++.h>
using namespace std;
int N, K;
vector<pair<int, int>> adj[200200];
int dist[200200];
int distw[200200];
int sz[200200];
bool delt[200200];
int best[1000100];
int calc_sz(int v, int p) {
sz[v] = 1;
for (auto e : adj[v]) {
int u, w; tie(u, w) = e;
if (u != p && !delt[u]) {
sz[v] += calc_sz(u, v);
}
}
return sz[v];
}
int centroid(int v, int p, int r) {
for (auto e : adj[v]) {
int u, w; tie(u, w) = e;
if (u != p && !delt[u] && 2 * sz[u] >= sz[r]) {
return centroid(u, v, r);
}
}
return v;
}
void prepare(int v, int p, int d, int dw) {
dw = min(dw, K + 1);
dist[v] = d;
distw[v] = dw;
for (auto e : adj[v]) {
int u, w; tie(u, w) = e;
if (u != p && !delt[u]) {
prepare(u, v, d + 1, dw + w);
}
}
}
int ans = 1e9;
void query(int v, int p) {
if (distw[v] <= K) {
ans = min(ans, dist[v] + best[K - distw[v]]);
}
for (auto e : adj[v]) {
int u, w; tie(u, w) = e;
if (u != p && !delt[u]) {
query(u, v);
}
}
}
void modify(int v, int p) {
if (distw[v] <= K) best[distw[v]] = min(best[distw[v]], dist[v]);
for (auto e : adj[v]) {
int u, w; tie(u, w) = e;
if (u != p && !delt[u]) {
modify(u, v);
}
}
}
void reset(int v, int p) {
if (distw[v] <= K) best[distw[v]] = 1e9;
for (auto e : adj[v]) {
int u, w; tie(u, w) = e;
if (u != p && !delt[u]) {
reset(u, v);
}
}
}
void solve(int v) {
calc_sz(v, -1);
v = centroid(v, -1, v);
prepare(v, -1, 0, 0);
best[0] = 0;
for (auto e : adj[v]) {
int u, w; tie(u, w) = e;
if (!delt[u]) {
query(u, v);
modify(u, v);
}
}
reset(v, -1);
delt[v] = true;
for (auto e : adj[v]) if (!delt[e.first]) solve(e.first);
}
int best_path(int N, int K, int H[][2], int L[]) {
::N = N;
::K = K;
for (int i = 0; i < N - 1; ++i) {
adj[H[i][0]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
for (int i = 0; i < 1000100; ++i) best[i] = 1e9;
solve(0);
if (ans > N) ans = -1;
return ans;
}
#ifdef KienVu
int main() {
int N, K;
while (cin >> N >> K) {
int H[N - 1][2], L[N - 1];
for (int i = 0; i < N - 1; ++i) {
cin >> H[i][0] >> H[i][1] >> L[i];
}
cout << best_path(N, K, H, L) << "\n";
}
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
10 ms |
8960 KB |
Output is correct |
4 |
Correct |
9 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
10 ms |
9088 KB |
Output is correct |
11 |
Correct |
10 ms |
8960 KB |
Output is correct |
12 |
Correct |
9 ms |
8960 KB |
Output is correct |
13 |
Correct |
10 ms |
8960 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
10 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
8960 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
10 ms |
8960 KB |
Output is correct |
4 |
Correct |
9 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
10 ms |
9088 KB |
Output is correct |
11 |
Correct |
10 ms |
8960 KB |
Output is correct |
12 |
Correct |
9 ms |
8960 KB |
Output is correct |
13 |
Correct |
10 ms |
8960 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
10 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
8960 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
10 ms |
8960 KB |
Output is correct |
20 |
Correct |
10 ms |
8960 KB |
Output is correct |
21 |
Correct |
10 ms |
9088 KB |
Output is correct |
22 |
Correct |
11 ms |
9088 KB |
Output is correct |
23 |
Correct |
10 ms |
9088 KB |
Output is correct |
24 |
Correct |
11 ms |
9088 KB |
Output is correct |
25 |
Correct |
10 ms |
8960 KB |
Output is correct |
26 |
Correct |
11 ms |
9088 KB |
Output is correct |
27 |
Correct |
10 ms |
9088 KB |
Output is correct |
28 |
Correct |
10 ms |
8960 KB |
Output is correct |
29 |
Correct |
10 ms |
9088 KB |
Output is correct |
30 |
Correct |
11 ms |
8960 KB |
Output is correct |
31 |
Correct |
11 ms |
9088 KB |
Output is correct |
32 |
Correct |
10 ms |
9088 KB |
Output is correct |
33 |
Correct |
11 ms |
9088 KB |
Output is correct |
34 |
Correct |
10 ms |
8960 KB |
Output is correct |
35 |
Correct |
11 ms |
9088 KB |
Output is correct |
36 |
Correct |
10 ms |
9088 KB |
Output is correct |
37 |
Correct |
10 ms |
8960 KB |
Output is correct |
38 |
Correct |
10 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
10 ms |
8960 KB |
Output is correct |
4 |
Correct |
9 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
10 ms |
9088 KB |
Output is correct |
11 |
Correct |
10 ms |
8960 KB |
Output is correct |
12 |
Correct |
9 ms |
8960 KB |
Output is correct |
13 |
Correct |
10 ms |
8960 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
10 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
8960 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
252 ms |
15096 KB |
Output is correct |
20 |
Correct |
275 ms |
16376 KB |
Output is correct |
21 |
Correct |
282 ms |
16492 KB |
Output is correct |
22 |
Correct |
235 ms |
16504 KB |
Output is correct |
23 |
Correct |
169 ms |
16760 KB |
Output is correct |
24 |
Correct |
94 ms |
16632 KB |
Output is correct |
25 |
Correct |
223 ms |
19056 KB |
Output is correct |
26 |
Correct |
129 ms |
20600 KB |
Output is correct |
27 |
Correct |
277 ms |
24312 KB |
Output is correct |
28 |
Correct |
693 ms |
32632 KB |
Output is correct |
29 |
Correct |
706 ms |
31864 KB |
Output is correct |
30 |
Correct |
274 ms |
24380 KB |
Output is correct |
31 |
Correct |
299 ms |
24312 KB |
Output is correct |
32 |
Correct |
410 ms |
24480 KB |
Output is correct |
33 |
Correct |
533 ms |
23160 KB |
Output is correct |
34 |
Correct |
555 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
10 ms |
8960 KB |
Output is correct |
4 |
Correct |
9 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
10 ms |
9088 KB |
Output is correct |
11 |
Correct |
10 ms |
8960 KB |
Output is correct |
12 |
Correct |
9 ms |
8960 KB |
Output is correct |
13 |
Correct |
10 ms |
8960 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
10 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
8960 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
10 ms |
8960 KB |
Output is correct |
20 |
Correct |
10 ms |
8960 KB |
Output is correct |
21 |
Correct |
10 ms |
9088 KB |
Output is correct |
22 |
Correct |
11 ms |
9088 KB |
Output is correct |
23 |
Correct |
10 ms |
9088 KB |
Output is correct |
24 |
Correct |
11 ms |
9088 KB |
Output is correct |
25 |
Correct |
10 ms |
8960 KB |
Output is correct |
26 |
Correct |
11 ms |
9088 KB |
Output is correct |
27 |
Correct |
10 ms |
9088 KB |
Output is correct |
28 |
Correct |
10 ms |
8960 KB |
Output is correct |
29 |
Correct |
10 ms |
9088 KB |
Output is correct |
30 |
Correct |
11 ms |
8960 KB |
Output is correct |
31 |
Correct |
11 ms |
9088 KB |
Output is correct |
32 |
Correct |
10 ms |
9088 KB |
Output is correct |
33 |
Correct |
11 ms |
9088 KB |
Output is correct |
34 |
Correct |
10 ms |
8960 KB |
Output is correct |
35 |
Correct |
11 ms |
9088 KB |
Output is correct |
36 |
Correct |
10 ms |
9088 KB |
Output is correct |
37 |
Correct |
10 ms |
8960 KB |
Output is correct |
38 |
Correct |
10 ms |
8960 KB |
Output is correct |
39 |
Correct |
252 ms |
15096 KB |
Output is correct |
40 |
Correct |
275 ms |
16376 KB |
Output is correct |
41 |
Correct |
282 ms |
16492 KB |
Output is correct |
42 |
Correct |
235 ms |
16504 KB |
Output is correct |
43 |
Correct |
169 ms |
16760 KB |
Output is correct |
44 |
Correct |
94 ms |
16632 KB |
Output is correct |
45 |
Correct |
223 ms |
19056 KB |
Output is correct |
46 |
Correct |
129 ms |
20600 KB |
Output is correct |
47 |
Correct |
277 ms |
24312 KB |
Output is correct |
48 |
Correct |
693 ms |
32632 KB |
Output is correct |
49 |
Correct |
706 ms |
31864 KB |
Output is correct |
50 |
Correct |
274 ms |
24380 KB |
Output is correct |
51 |
Correct |
299 ms |
24312 KB |
Output is correct |
52 |
Correct |
410 ms |
24480 KB |
Output is correct |
53 |
Correct |
533 ms |
23160 KB |
Output is correct |
54 |
Correct |
555 ms |
23928 KB |
Output is correct |
55 |
Correct |
21 ms |
9728 KB |
Output is correct |
56 |
Correct |
22 ms |
9728 KB |
Output is correct |
57 |
Correct |
120 ms |
16740 KB |
Output is correct |
58 |
Correct |
50 ms |
16368 KB |
Output is correct |
59 |
Correct |
130 ms |
20600 KB |
Output is correct |
60 |
Correct |
773 ms |
32124 KB |
Output is correct |
61 |
Correct |
268 ms |
24440 KB |
Output is correct |
62 |
Correct |
292 ms |
24568 KB |
Output is correct |
63 |
Correct |
417 ms |
24440 KB |
Output is correct |
64 |
Correct |
906 ms |
24776 KB |
Output is correct |
65 |
Correct |
585 ms |
24952 KB |
Output is correct |
66 |
Correct |
813 ms |
30584 KB |
Output is correct |
67 |
Correct |
157 ms |
25060 KB |
Output is correct |
68 |
Correct |
441 ms |
25324 KB |
Output is correct |
69 |
Correct |
412 ms |
25208 KB |
Output is correct |
70 |
Correct |
397 ms |
24612 KB |
Output is correct |