#include <bits/stdc++.h>
using namespace std;
int ans = INT_MAX;
int sz[200005];
vector<pair<int, int>> adj[200005];
bool removed[200005];
void dfssz(int v, int par = -1) {
sz[v] = 1;
for (auto u : adj[v]) {
if (u.first == par) continue;
if (removed[u.first]) continue;
dfssz(u.first, v);
sz[v] += sz[u.first];
}
}
int centroid(int v, int full, int par = -1) {
for (auto u : adj[v]) {
if (u.first == par) continue;
if (removed[u.first]) continue;
if (sz[u.first] * 2 > full) {
return centroid(u.first, full, v);
}
}
return v;
}
void update(map<int, int>& mp, map<int, int>& mp2, int len, int val, int K) {
if (mp2.find(len) == mp2.end()) mp2[len] = val;
else mp2[len] = min(mp2[len], val);
if (mp.find(K - len) != mp.end()) ans = min(ans, mp2[len] + mp[K - len]);
}
void solve(int v, map<int, int> &mp, map<int, int> &mp2, int len, int K, int depth, int par) {
for (auto u : adj[v]) {
if (u.first == par) continue;
if (removed[u.first]) continue;
if (len + u.second <= K) {
update(mp, mp2, len + u.second, 1 + depth, K);
solve(u.first, mp, mp2, len + u.second, K, depth + 1, v);
}
}
}
void decompose(int v, int K) {
dfssz(v);
int c = centroid(v, sz[v]);
map<int, int> mp;
for (auto u : adj[c]) {
if (removed[u.first]) continue;
if (u.second > K) continue;
map<int, int> mp2;
update(mp, mp2, u.second, 1, K);
solve(u.first, mp, mp2, u.second, K, 1, c);
for (auto it : mp2) {
if (mp.find(it.first) != mp.end()) mp[it.first] = min(mp[it.first], it.second);
else mp[it.first] = it.second;
}
}
if (mp.find(K) != mp.end()) ans = min(ans, mp[K]);
removed[c] = 1;
for (auto u : adj[c]) {
if (removed[u.first]) continue;
decompose(u.first, K);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
for (int i = 0; i < N - 1; i++) {
int u = H[i][0], v = H[i][1];
adj[u].push_back({v, L[i]});
adj[v].push_back({u, L[i]});
}
decompose(0, K);
if (ans != INT_MAX) return ans;
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
4 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
5000 KB |
Output is correct |
8 |
Correct |
5 ms |
5092 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5000 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
4 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
5000 KB |
Output is correct |
8 |
Correct |
5 ms |
5092 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5000 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
6 ms |
5008 KB |
Output is correct |
22 |
Correct |
5 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
5 ms |
5068 KB |
Output is correct |
25 |
Correct |
6 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
6 ms |
5068 KB |
Output is correct |
30 |
Correct |
6 ms |
5068 KB |
Output is correct |
31 |
Correct |
6 ms |
5136 KB |
Output is correct |
32 |
Correct |
6 ms |
5152 KB |
Output is correct |
33 |
Correct |
6 ms |
5068 KB |
Output is correct |
34 |
Correct |
5 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5004 KB |
Output is correct |
36 |
Correct |
5 ms |
5068 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
6 ms |
5256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
4 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
5000 KB |
Output is correct |
8 |
Correct |
5 ms |
5092 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5000 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
263 ms |
11716 KB |
Output is correct |
20 |
Correct |
250 ms |
11584 KB |
Output is correct |
21 |
Correct |
271 ms |
11716 KB |
Output is correct |
22 |
Correct |
260 ms |
11840 KB |
Output is correct |
23 |
Correct |
104 ms |
12016 KB |
Output is correct |
24 |
Correct |
95 ms |
11832 KB |
Output is correct |
25 |
Correct |
180 ms |
15292 KB |
Output is correct |
26 |
Correct |
126 ms |
18888 KB |
Output is correct |
27 |
Correct |
259 ms |
18676 KB |
Output is correct |
28 |
Correct |
300 ms |
33220 KB |
Output is correct |
29 |
Correct |
323 ms |
32104 KB |
Output is correct |
30 |
Correct |
261 ms |
18792 KB |
Output is correct |
31 |
Correct |
258 ms |
18760 KB |
Output is correct |
32 |
Correct |
269 ms |
18904 KB |
Output is correct |
33 |
Correct |
430 ms |
17636 KB |
Output is correct |
34 |
Correct |
190 ms |
18564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
4 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
5000 KB |
Output is correct |
8 |
Correct |
5 ms |
5092 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5000 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
6 ms |
5008 KB |
Output is correct |
22 |
Correct |
5 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
5 ms |
5068 KB |
Output is correct |
25 |
Correct |
6 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
6 ms |
5068 KB |
Output is correct |
30 |
Correct |
6 ms |
5068 KB |
Output is correct |
31 |
Correct |
6 ms |
5136 KB |
Output is correct |
32 |
Correct |
6 ms |
5152 KB |
Output is correct |
33 |
Correct |
6 ms |
5068 KB |
Output is correct |
34 |
Correct |
5 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5004 KB |
Output is correct |
36 |
Correct |
5 ms |
5068 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
6 ms |
5256 KB |
Output is correct |
39 |
Correct |
263 ms |
11716 KB |
Output is correct |
40 |
Correct |
250 ms |
11584 KB |
Output is correct |
41 |
Correct |
271 ms |
11716 KB |
Output is correct |
42 |
Correct |
260 ms |
11840 KB |
Output is correct |
43 |
Correct |
104 ms |
12016 KB |
Output is correct |
44 |
Correct |
95 ms |
11832 KB |
Output is correct |
45 |
Correct |
180 ms |
15292 KB |
Output is correct |
46 |
Correct |
126 ms |
18888 KB |
Output is correct |
47 |
Correct |
259 ms |
18676 KB |
Output is correct |
48 |
Correct |
300 ms |
33220 KB |
Output is correct |
49 |
Correct |
323 ms |
32104 KB |
Output is correct |
50 |
Correct |
261 ms |
18792 KB |
Output is correct |
51 |
Correct |
258 ms |
18760 KB |
Output is correct |
52 |
Correct |
269 ms |
18904 KB |
Output is correct |
53 |
Correct |
430 ms |
17636 KB |
Output is correct |
54 |
Correct |
190 ms |
18564 KB |
Output is correct |
55 |
Correct |
30 ms |
5908 KB |
Output is correct |
56 |
Correct |
20 ms |
5580 KB |
Output is correct |
57 |
Correct |
194 ms |
11888 KB |
Output is correct |
58 |
Correct |
57 ms |
11576 KB |
Output is correct |
59 |
Correct |
543 ms |
25988 KB |
Output is correct |
60 |
Correct |
1437 ms |
49752 KB |
Output is correct |
61 |
Correct |
376 ms |
19196 KB |
Output is correct |
62 |
Correct |
430 ms |
19108 KB |
Output is correct |
63 |
Correct |
478 ms |
19012 KB |
Output is correct |
64 |
Correct |
1363 ms |
30116 KB |
Output is correct |
65 |
Correct |
227 ms |
19652 KB |
Output is correct |
66 |
Correct |
918 ms |
31108 KB |
Output is correct |
67 |
Correct |
160 ms |
19628 KB |
Output is correct |
68 |
Correct |
532 ms |
26452 KB |
Output is correct |
69 |
Correct |
542 ms |
26948 KB |
Output is correct |
70 |
Correct |
493 ms |
25712 KB |
Output is correct |