/**
* author: MaGnsi0
* created: 29.06.2023 15:00:05
**/
#include <bits/stdc++.h>
using namespace std;
int K, ans;
map<int, int> cnt;
const int N = 2e5 + 5;
vector<int> length(N), is_centroid(N);
vector<vector<pair<int, int>>> adj(N);
void dfs(int v, int p) {
length[v] = 1;
for (auto [u, _] : adj[v]) {
if (u == p || is_centroid[u]) { continue; }
dfs(u, v);
length[v] += length[u];
}
}
int get_centroid(int v, int p, int m) {
for (auto [u, _] : adj[v]) {
if (u == p || is_centroid[u]) { continue; }
if (2 * length[u] > m) {
return get_centroid(u, v, m);
}
}
return v;
}
void count(int v, int p, int j, int x) {
if (x > K) { return; }
if (cnt.count(K - x)) {
ans = min(ans, cnt[K - x] + j);
}
for (auto [u, w] : adj[v]) {
if (u == p || is_centroid[u]) { continue; }
count(u, v, j + 1, x + w);
}
}
void add(int v, int p, int j, int x) {
if (x > K) { return; }
if (cnt.count(x)) {
cnt[x] = min(cnt[x], j);
} else {
cnt[x] = j;
}
for (auto [u, w] : adj[v]) {
if (u == p || is_centroid[u]) { continue; }
add(u, v, j + 1, x + w);
}
}
void solve(int v) {
dfs(v, -1);
v = get_centroid(v, -1, length[v]);
is_centroid[v] = 1;
cnt.clear(); cnt[0] = 0;
for (auto [u, w] : adj[v]) {
if (is_centroid[u]) { continue; }
count(u, v, 1, w);
add(u, v, 1, w);
}
for (auto [u, w] : adj[v]) {
if (is_centroid[u]) { continue; }
solve(u);
}
}
int best_path(int n, int k, int H[][2], int L[]) {
ans = 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]);
}
solve(0);
return (ans == n ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
3 ms |
6484 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
3 ms |
6484 KB |
Output is correct |
16 |
Correct |
3 ms |
6484 KB |
Output is correct |
17 |
Correct |
3 ms |
6484 KB |
Output is correct |
18 |
Correct |
3 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
3 ms |
6484 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
3 ms |
6484 KB |
Output is correct |
16 |
Correct |
3 ms |
6484 KB |
Output is correct |
17 |
Correct |
3 ms |
6484 KB |
Output is correct |
18 |
Correct |
3 ms |
6484 KB |
Output is correct |
19 |
Correct |
3 ms |
6484 KB |
Output is correct |
20 |
Correct |
3 ms |
6484 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
3 ms |
6520 KB |
Output is correct |
23 |
Correct |
3 ms |
6612 KB |
Output is correct |
24 |
Correct |
3 ms |
6612 KB |
Output is correct |
25 |
Correct |
4 ms |
6688 KB |
Output is correct |
26 |
Correct |
3 ms |
6612 KB |
Output is correct |
27 |
Correct |
3 ms |
6612 KB |
Output is correct |
28 |
Correct |
4 ms |
6612 KB |
Output is correct |
29 |
Correct |
4 ms |
6592 KB |
Output is correct |
30 |
Correct |
4 ms |
6612 KB |
Output is correct |
31 |
Correct |
4 ms |
6612 KB |
Output is correct |
32 |
Correct |
4 ms |
6612 KB |
Output is correct |
33 |
Correct |
4 ms |
6612 KB |
Output is correct |
34 |
Correct |
4 ms |
6612 KB |
Output is correct |
35 |
Correct |
3 ms |
6612 KB |
Output is correct |
36 |
Correct |
4 ms |
6612 KB |
Output is correct |
37 |
Correct |
4 ms |
6612 KB |
Output is correct |
38 |
Correct |
4 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
3 ms |
6484 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
3 ms |
6484 KB |
Output is correct |
16 |
Correct |
3 ms |
6484 KB |
Output is correct |
17 |
Correct |
3 ms |
6484 KB |
Output is correct |
18 |
Correct |
3 ms |
6484 KB |
Output is correct |
19 |
Correct |
140 ms |
11388 KB |
Output is correct |
20 |
Correct |
141 ms |
11320 KB |
Output is correct |
21 |
Correct |
160 ms |
11312 KB |
Output is correct |
22 |
Correct |
149 ms |
11532 KB |
Output is correct |
23 |
Correct |
63 ms |
11592 KB |
Output is correct |
24 |
Correct |
57 ms |
11560 KB |
Output is correct |
25 |
Correct |
112 ms |
14176 KB |
Output is correct |
26 |
Correct |
80 ms |
18472 KB |
Output is correct |
27 |
Correct |
139 ms |
19292 KB |
Output is correct |
28 |
Correct |
182 ms |
30684 KB |
Output is correct |
29 |
Correct |
195 ms |
29844 KB |
Output is correct |
30 |
Correct |
140 ms |
19388 KB |
Output is correct |
31 |
Correct |
160 ms |
19516 KB |
Output is correct |
32 |
Correct |
156 ms |
19492 KB |
Output is correct |
33 |
Correct |
187 ms |
18124 KB |
Output is correct |
34 |
Correct |
120 ms |
19024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
3 ms |
6484 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
3 ms |
6484 KB |
Output is correct |
16 |
Correct |
3 ms |
6484 KB |
Output is correct |
17 |
Correct |
3 ms |
6484 KB |
Output is correct |
18 |
Correct |
3 ms |
6484 KB |
Output is correct |
19 |
Correct |
3 ms |
6484 KB |
Output is correct |
20 |
Correct |
3 ms |
6484 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
3 ms |
6520 KB |
Output is correct |
23 |
Correct |
3 ms |
6612 KB |
Output is correct |
24 |
Correct |
3 ms |
6612 KB |
Output is correct |
25 |
Correct |
4 ms |
6688 KB |
Output is correct |
26 |
Correct |
3 ms |
6612 KB |
Output is correct |
27 |
Correct |
3 ms |
6612 KB |
Output is correct |
28 |
Correct |
4 ms |
6612 KB |
Output is correct |
29 |
Correct |
4 ms |
6592 KB |
Output is correct |
30 |
Correct |
4 ms |
6612 KB |
Output is correct |
31 |
Correct |
4 ms |
6612 KB |
Output is correct |
32 |
Correct |
4 ms |
6612 KB |
Output is correct |
33 |
Correct |
4 ms |
6612 KB |
Output is correct |
34 |
Correct |
4 ms |
6612 KB |
Output is correct |
35 |
Correct |
3 ms |
6612 KB |
Output is correct |
36 |
Correct |
4 ms |
6612 KB |
Output is correct |
37 |
Correct |
4 ms |
6612 KB |
Output is correct |
38 |
Correct |
4 ms |
6612 KB |
Output is correct |
39 |
Correct |
140 ms |
11388 KB |
Output is correct |
40 |
Correct |
141 ms |
11320 KB |
Output is correct |
41 |
Correct |
160 ms |
11312 KB |
Output is correct |
42 |
Correct |
149 ms |
11532 KB |
Output is correct |
43 |
Correct |
63 ms |
11592 KB |
Output is correct |
44 |
Correct |
57 ms |
11560 KB |
Output is correct |
45 |
Correct |
112 ms |
14176 KB |
Output is correct |
46 |
Correct |
80 ms |
18472 KB |
Output is correct |
47 |
Correct |
139 ms |
19292 KB |
Output is correct |
48 |
Correct |
182 ms |
30684 KB |
Output is correct |
49 |
Correct |
195 ms |
29844 KB |
Output is correct |
50 |
Correct |
140 ms |
19388 KB |
Output is correct |
51 |
Correct |
160 ms |
19516 KB |
Output is correct |
52 |
Correct |
156 ms |
19492 KB |
Output is correct |
53 |
Correct |
187 ms |
18124 KB |
Output is correct |
54 |
Correct |
120 ms |
19024 KB |
Output is correct |
55 |
Correct |
16 ms |
7252 KB |
Output is correct |
56 |
Correct |
13 ms |
7196 KB |
Output is correct |
57 |
Correct |
118 ms |
13004 KB |
Output is correct |
58 |
Correct |
48 ms |
12712 KB |
Output is correct |
59 |
Correct |
233 ms |
22004 KB |
Output is correct |
60 |
Correct |
687 ms |
38608 KB |
Output is correct |
61 |
Correct |
199 ms |
19712 KB |
Output is correct |
62 |
Correct |
268 ms |
19624 KB |
Output is correct |
63 |
Correct |
258 ms |
19648 KB |
Output is correct |
64 |
Correct |
767 ms |
24284 KB |
Output is correct |
65 |
Correct |
137 ms |
20284 KB |
Output is correct |
66 |
Correct |
415 ms |
28056 KB |
Output is correct |
67 |
Correct |
95 ms |
20084 KB |
Output is correct |
68 |
Correct |
301 ms |
23832 KB |
Output is correct |
69 |
Correct |
317 ms |
23856 KB |
Output is correct |
70 |
Correct |
274 ms |
23152 KB |
Output is correct |