#include <bits/stdc++.h>
using namespace std;
int best_path(int n, int k, int h[][2], int l[]) {
int ans = INT_MAX;
vector<vector<pair<int, int>>> G(n + 1);
vector<int> seen(n + 1), s(n + 1);
map<int, int> m;
for (int i = 0; i < n - 1; ++i) {
G[1 + h[i][0]].emplace_back(1 + h[i][1], l[i]);
G[1 + h[i][1]].emplace_back(1 + h[i][0], l[i]);
}
function<void(int, int)> dfs = [&](int node, int last) {
s[node] = 1;
for (auto x : G[node])
if (x.first != last && !seen[x.first]) {
dfs(x.first, node);
s[node] += s[x.first];
}
};
function<int(int, int, int)> centroid = [&](int node, int last, int n) {
for (auto x : G[node])
if (x.first != last && !seen[x.first] && s[x.first] > n / 2)
return centroid(x.first, node, n);
return node;
};
function<void(int, int, int, int, int)> dfs2 = [&](int node, int last, int length1, int length2, int mode) {
if (mode) {
if (m.count(k - length1))
ans = min(ans, m[k - length1] + length2);
}
else {
if (m.count(length1))
m[length1] = min(m[length1], length2);
else
m[length1] = length2;
}
for (auto x : G[node])
if (x.first != last && !seen[x.first])
dfs2(x.first, node, length1 + x.second, length2 + 1, mode);
};
function<void(int)> Build = [&](int node) {
dfs(node, 0);
int c = centroid(node, 0, s[node]);
seen[c] = 1;
m.clear();
m[0] = 0;
for (auto x : G[c])
if (!seen[x.first])
dfs2(x.first, c, x.second, 1, 1), dfs2(x.first, c, x.second, 1, 0);
for (auto x : G[c])
if (!seen[x.first])
Build(x.first);
};
Build(1);
return ans != INT_MAX ? ans : -1;
};
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
2 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
2 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
440 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
444 KB |
Output is correct |
27 |
Correct |
2 ms |
316 KB |
Output is correct |
28 |
Correct |
3 ms |
444 KB |
Output is correct |
29 |
Correct |
4 ms |
332 KB |
Output is correct |
30 |
Correct |
3 ms |
332 KB |
Output is correct |
31 |
Correct |
4 ms |
436 KB |
Output is correct |
32 |
Correct |
3 ms |
332 KB |
Output is correct |
33 |
Correct |
3 ms |
332 KB |
Output is correct |
34 |
Correct |
2 ms |
332 KB |
Output is correct |
35 |
Correct |
3 ms |
332 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
332 KB |
Output is correct |
38 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
2 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
340 ms |
9712 KB |
Output is correct |
20 |
Correct |
326 ms |
9712 KB |
Output is correct |
21 |
Correct |
346 ms |
9648 KB |
Output is correct |
22 |
Correct |
280 ms |
9840 KB |
Output is correct |
23 |
Correct |
385 ms |
9988 KB |
Output is correct |
24 |
Correct |
175 ms |
9796 KB |
Output is correct |
25 |
Correct |
309 ms |
17172 KB |
Output is correct |
26 |
Correct |
156 ms |
18372 KB |
Output is correct |
27 |
Correct |
389 ms |
19560 KB |
Output is correct |
28 |
Correct |
1311 ms |
46276 KB |
Output is correct |
29 |
Correct |
1250 ms |
44968 KB |
Output is correct |
30 |
Correct |
387 ms |
19620 KB |
Output is correct |
31 |
Correct |
398 ms |
19524 KB |
Output is correct |
32 |
Correct |
487 ms |
19636 KB |
Output is correct |
33 |
Correct |
694 ms |
18456 KB |
Output is correct |
34 |
Correct |
1141 ms |
28508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
2 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
440 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
444 KB |
Output is correct |
27 |
Correct |
2 ms |
316 KB |
Output is correct |
28 |
Correct |
3 ms |
444 KB |
Output is correct |
29 |
Correct |
4 ms |
332 KB |
Output is correct |
30 |
Correct |
3 ms |
332 KB |
Output is correct |
31 |
Correct |
4 ms |
436 KB |
Output is correct |
32 |
Correct |
3 ms |
332 KB |
Output is correct |
33 |
Correct |
3 ms |
332 KB |
Output is correct |
34 |
Correct |
2 ms |
332 KB |
Output is correct |
35 |
Correct |
3 ms |
332 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
332 KB |
Output is correct |
38 |
Correct |
3 ms |
460 KB |
Output is correct |
39 |
Correct |
340 ms |
9712 KB |
Output is correct |
40 |
Correct |
326 ms |
9712 KB |
Output is correct |
41 |
Correct |
346 ms |
9648 KB |
Output is correct |
42 |
Correct |
280 ms |
9840 KB |
Output is correct |
43 |
Correct |
385 ms |
9988 KB |
Output is correct |
44 |
Correct |
175 ms |
9796 KB |
Output is correct |
45 |
Correct |
309 ms |
17172 KB |
Output is correct |
46 |
Correct |
156 ms |
18372 KB |
Output is correct |
47 |
Correct |
389 ms |
19560 KB |
Output is correct |
48 |
Correct |
1311 ms |
46276 KB |
Output is correct |
49 |
Correct |
1250 ms |
44968 KB |
Output is correct |
50 |
Correct |
387 ms |
19620 KB |
Output is correct |
51 |
Correct |
398 ms |
19524 KB |
Output is correct |
52 |
Correct |
487 ms |
19636 KB |
Output is correct |
53 |
Correct |
694 ms |
18456 KB |
Output is correct |
54 |
Correct |
1141 ms |
28508 KB |
Output is correct |
55 |
Correct |
26 ms |
1408 KB |
Output is correct |
56 |
Correct |
19 ms |
1228 KB |
Output is correct |
57 |
Correct |
198 ms |
9868 KB |
Output is correct |
58 |
Correct |
69 ms |
9636 KB |
Output is correct |
59 |
Correct |
385 ms |
22040 KB |
Output is correct |
60 |
Correct |
1315 ms |
44484 KB |
Output is correct |
61 |
Correct |
453 ms |
20980 KB |
Output is correct |
62 |
Correct |
448 ms |
19616 KB |
Output is correct |
63 |
Correct |
517 ms |
19660 KB |
Output is correct |
64 |
Correct |
1357 ms |
24376 KB |
Output is correct |
65 |
Correct |
1222 ms |
29528 KB |
Output is correct |
66 |
Correct |
1307 ms |
43436 KB |
Output is correct |
67 |
Correct |
181 ms |
20292 KB |
Output is correct |
68 |
Correct |
662 ms |
28072 KB |
Output is correct |
69 |
Correct |
677 ms |
28420 KB |
Output is correct |
70 |
Correct |
632 ms |
26960 KB |
Output is correct |