#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define f first
#define s second
const int mxN = 200000;
const int mxK = 1000000;
int n, k;
vector< pair<int, int> > adj[mxN + 5];
int n_child[mxN + 5];
int cnt[mxK + 5];
bool used[mxN + 5];
vector<int> order;
int mx_depth;
int result;
int get_subtree_size(int u, int p = -1) {
n_child[u] = 1;
for (auto& to: adj[u]) {
if (!used[to.f] && to.f != p) {
n_child[u] += get_subtree_size(to.f, u);
}
}
return n_child[u];
}
int get_centroid(int desired, int u, int p = -1) {
for (auto& to: adj[u]) {
if (!used[to.f] && to.f != p && n_child[to.f] >= desired) {
return get_centroid(desired, to.f, u);
}
}
return u;
}
void get_cnt(int u, int p, bool e, int w, int depth) {
if (w > k) {
return ;
}
order.push_back(w);
if (e) {
cnt[w] = min(cnt[w], depth);
}
else {
result = min(result, cnt[k - w] + depth);
}
for (auto& to: adj[u]) {
if (!used[to.f] && to.f != p) {
get_cnt(to.f, u, e, w + to.s, depth + 1);
}
}
}
void centroid_decomp(int u) {
int centroid = get_centroid(get_subtree_size(u) >> 1, u);
used[centroid] = true;
mx_depth = 0;
for (auto& to: adj[centroid]) {
if (!used[to.f]) {
get_cnt(to.f, centroid, false, to.s, 1);
get_cnt(to.f, centroid, true, to.s, 1);
}
}
for (auto& v: order) {
cnt[v] = n + 5;
}
order.clear();
for (auto& to: adj[centroid]) {
if (!used[to.f]) {
centroid_decomp(to.f);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for (int i = 0; i < n - 1; i++) {
//H[i][0]--, H[i][1]--;
adj[H[i][0]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
cnt[0] = 0;
for (int i = 1; i <= mxK; i++) {
cnt[i] = n + 5;
}
result = n + 5;
centroid_decomp(0);
return (result >= n ? -1: result);
}
/*int main() {
cin.tie(0)->sync_with_stdio(0);
int N, K; cin >> N >> K;
int H[N][2], L[N];
for (int i = 0, x, y; i < N - 1; i++) {
cin >> H[i][0] >> H[i][1] >> L[i];
}
cout << best_path(N, K, H, L) << endl;
return 0;
}*/
/*
4 3
0 1 1
1 2 2
1 3 4
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
5 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8908 KB |
Output is correct |
4 |
Correct |
6 ms |
8848 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8844 KB |
Output is correct |
10 |
Correct |
5 ms |
8848 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8844 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8860 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8844 KB |
Output is correct |
17 |
Correct |
6 ms |
8840 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
5 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8908 KB |
Output is correct |
4 |
Correct |
6 ms |
8848 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8844 KB |
Output is correct |
10 |
Correct |
5 ms |
8848 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8844 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8860 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8844 KB |
Output is correct |
17 |
Correct |
6 ms |
8840 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
5 ms |
8908 KB |
Output is correct |
20 |
Correct |
6 ms |
8908 KB |
Output is correct |
21 |
Correct |
6 ms |
8908 KB |
Output is correct |
22 |
Correct |
7 ms |
8928 KB |
Output is correct |
23 |
Correct |
6 ms |
8908 KB |
Output is correct |
24 |
Correct |
6 ms |
8908 KB |
Output is correct |
25 |
Correct |
6 ms |
8908 KB |
Output is correct |
26 |
Correct |
6 ms |
8908 KB |
Output is correct |
27 |
Correct |
6 ms |
8996 KB |
Output is correct |
28 |
Correct |
7 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8972 KB |
Output is correct |
30 |
Correct |
8 ms |
8908 KB |
Output is correct |
31 |
Correct |
6 ms |
8908 KB |
Output is correct |
32 |
Correct |
7 ms |
9020 KB |
Output is correct |
33 |
Correct |
7 ms |
8908 KB |
Output is correct |
34 |
Correct |
7 ms |
8912 KB |
Output is correct |
35 |
Correct |
7 ms |
8908 KB |
Output is correct |
36 |
Correct |
7 ms |
8908 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
6 ms |
8980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
5 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8908 KB |
Output is correct |
4 |
Correct |
6 ms |
8848 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8844 KB |
Output is correct |
10 |
Correct |
5 ms |
8848 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8844 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8860 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8844 KB |
Output is correct |
17 |
Correct |
6 ms |
8840 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
152 ms |
15772 KB |
Output is correct |
20 |
Correct |
166 ms |
15752 KB |
Output is correct |
21 |
Correct |
189 ms |
16120 KB |
Output is correct |
22 |
Correct |
145 ms |
16700 KB |
Output is correct |
23 |
Correct |
84 ms |
15860 KB |
Output is correct |
24 |
Correct |
73 ms |
15892 KB |
Output is correct |
25 |
Correct |
142 ms |
18464 KB |
Output is correct |
26 |
Correct |
115 ms |
22308 KB |
Output is correct |
27 |
Correct |
195 ms |
22808 KB |
Output is correct |
28 |
Correct |
311 ms |
34076 KB |
Output is correct |
29 |
Correct |
284 ms |
33036 KB |
Output is correct |
30 |
Correct |
221 ms |
22812 KB |
Output is correct |
31 |
Correct |
200 ms |
22808 KB |
Output is correct |
32 |
Correct |
236 ms |
22820 KB |
Output is correct |
33 |
Correct |
260 ms |
21576 KB |
Output is correct |
34 |
Correct |
228 ms |
22444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
5 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8908 KB |
Output is correct |
4 |
Correct |
6 ms |
8848 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8844 KB |
Output is correct |
10 |
Correct |
5 ms |
8848 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8844 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8860 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8844 KB |
Output is correct |
17 |
Correct |
6 ms |
8840 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
5 ms |
8908 KB |
Output is correct |
20 |
Correct |
6 ms |
8908 KB |
Output is correct |
21 |
Correct |
6 ms |
8908 KB |
Output is correct |
22 |
Correct |
7 ms |
8928 KB |
Output is correct |
23 |
Correct |
6 ms |
8908 KB |
Output is correct |
24 |
Correct |
6 ms |
8908 KB |
Output is correct |
25 |
Correct |
6 ms |
8908 KB |
Output is correct |
26 |
Correct |
6 ms |
8908 KB |
Output is correct |
27 |
Correct |
6 ms |
8996 KB |
Output is correct |
28 |
Correct |
7 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8972 KB |
Output is correct |
30 |
Correct |
8 ms |
8908 KB |
Output is correct |
31 |
Correct |
6 ms |
8908 KB |
Output is correct |
32 |
Correct |
7 ms |
9020 KB |
Output is correct |
33 |
Correct |
7 ms |
8908 KB |
Output is correct |
34 |
Correct |
7 ms |
8912 KB |
Output is correct |
35 |
Correct |
7 ms |
8908 KB |
Output is correct |
36 |
Correct |
7 ms |
8908 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
6 ms |
8980 KB |
Output is correct |
39 |
Correct |
152 ms |
15772 KB |
Output is correct |
40 |
Correct |
166 ms |
15752 KB |
Output is correct |
41 |
Correct |
189 ms |
16120 KB |
Output is correct |
42 |
Correct |
145 ms |
16700 KB |
Output is correct |
43 |
Correct |
84 ms |
15860 KB |
Output is correct |
44 |
Correct |
73 ms |
15892 KB |
Output is correct |
45 |
Correct |
142 ms |
18464 KB |
Output is correct |
46 |
Correct |
115 ms |
22308 KB |
Output is correct |
47 |
Correct |
195 ms |
22808 KB |
Output is correct |
48 |
Correct |
311 ms |
34076 KB |
Output is correct |
49 |
Correct |
284 ms |
33036 KB |
Output is correct |
50 |
Correct |
221 ms |
22812 KB |
Output is correct |
51 |
Correct |
200 ms |
22808 KB |
Output is correct |
52 |
Correct |
236 ms |
22820 KB |
Output is correct |
53 |
Correct |
260 ms |
21576 KB |
Output is correct |
54 |
Correct |
228 ms |
22444 KB |
Output is correct |
55 |
Correct |
15 ms |
9676 KB |
Output is correct |
56 |
Correct |
16 ms |
9676 KB |
Output is correct |
57 |
Correct |
91 ms |
16836 KB |
Output is correct |
58 |
Correct |
50 ms |
17076 KB |
Output is correct |
59 |
Correct |
119 ms |
22356 KB |
Output is correct |
60 |
Correct |
517 ms |
35352 KB |
Output is correct |
61 |
Correct |
227 ms |
23200 KB |
Output is correct |
62 |
Correct |
243 ms |
24776 KB |
Output is correct |
63 |
Correct |
323 ms |
24772 KB |
Output is correct |
64 |
Correct |
614 ms |
25020 KB |
Output is correct |
65 |
Correct |
313 ms |
23616 KB |
Output is correct |
66 |
Correct |
432 ms |
31356 KB |
Output is correct |
67 |
Correct |
138 ms |
26284 KB |
Output is correct |
68 |
Correct |
316 ms |
24512 KB |
Output is correct |
69 |
Correct |
296 ms |
24640 KB |
Output is correct |
70 |
Correct |
308 ms |
23932 KB |
Output is correct |