#include "race.h"
#include "bits/stdc++.h"
using namespace std;
#define ssize(x) (int)size(x)
template <class A, class B>
auto &operator<<(ostream &o, pair<A, B> p)
{
return o << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &o, T x) -> decltype(x.end(), o)
{
o << '{';
int i = 0;
for (auto e : x)
o << (", ") + 2 * !i++ << e;
return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) { ((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) \
{ \
}
#endif
int best_path(int N, int K, int H[][2], int L[])
{
vector<vector<pair<int, int>>> graf(N);
for (int i = 0; i < N - 1; i++) {
int a = H[i][0], b = H[i][1], c = L[i];
graf[a].push_back({b, c});
graf[b].push_back({a, c});
}
int ans = N;
vector<int> subsz(N);
vector<bool> used(N);
function<void(int, int)> calc_sz = [&](int u, int p) {
subsz[u] = 1;
for (auto [to, _] : graf[u]) {
if (to == p || used[to]) continue;
calc_sz(to, u);
subsz[u] += subsz[to];
}
};
function<int(int, int, int)> calc_centro = [&](int u, int p, int need) {
for (auto [to, _] : graf[u]) {
if (to == p || used[to]) continue;
if (subsz[to] * 2 > need) return calc_centro(to, u, need);
}
return u;
};
vector<vector<pair<int, int>>> wek(K + 1);
vector<int> cos;
int pid = 0;
function<void(int, int, int, int, int)> gather = [&](int u, int p, int len, int path_id, int edges_count) {
if (len <= K) {
wek[len].push_back({edges_count, path_id});
cos.push_back(len);
}
else return; // xD?
for (auto [to, tlen] : graf[u]) {
if (to == p || used[to]) continue;
gather(to, u, len + tlen, path_id, edges_count + 1);
}
};
function<void(int)> rek = [&](int u) {
calc_sz(u, u);
u = calc_centro(u, u, subsz[u]);
for (auto [to, tlen] : graf[u]) {
if (used[to]) continue;
gather(to, u, tlen, ++pid, 1);
}
cos.push_back(0);
wek[0].push_back({0, -1});
sort(cos.begin(), cos.end());
cos.erase(unique(cos.begin(), cos.end()), cos.end());
for (auto x : cos) {
pair<int, int> min_1(N, 0);
for (auto d : wek[x])
min_1 = min(min_1, d);
pair<int, int> min_2(N, 0);
for (auto d : wek[x])
if (d.second != min_1.second) min_2 = min(min_2, d);
for (auto d : wek[K - x]) {
if (d.second != min_1.second) ans = min(ans, d.first + min_1.first);
if (d.second != min_2.second) ans = min(ans, d.first + min_2.first);
}
}
used[u] = 1;
for (auto x : cos)
wek[x].clear();
cos.clear();
for (auto [to, _] : graf[u]) {
if (used[to]) continue;
rek(to);
}
};
rek(0);
return (ans == N ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
248 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
248 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
16 ms |
22064 KB |
Output is correct |
23 |
Correct |
15 ms |
18108 KB |
Output is correct |
24 |
Correct |
17 ms |
20688 KB |
Output is correct |
25 |
Correct |
18 ms |
20288 KB |
Output is correct |
26 |
Correct |
7 ms |
8276 KB |
Output is correct |
27 |
Correct |
14 ms |
19136 KB |
Output is correct |
28 |
Correct |
5 ms |
5188 KB |
Output is correct |
29 |
Correct |
8 ms |
8108 KB |
Output is correct |
30 |
Correct |
8 ms |
9152 KB |
Output is correct |
31 |
Correct |
15 ms |
15892 KB |
Output is correct |
32 |
Correct |
12 ms |
17216 KB |
Output is correct |
33 |
Correct |
17 ms |
18876 KB |
Output is correct |
34 |
Correct |
11 ms |
14344 KB |
Output is correct |
35 |
Correct |
21 ms |
19540 KB |
Output is correct |
36 |
Correct |
16 ms |
22348 KB |
Output is correct |
37 |
Correct |
15 ms |
19260 KB |
Output is correct |
38 |
Correct |
10 ms |
12500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
248 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
203 ms |
9448 KB |
Output is correct |
20 |
Correct |
224 ms |
9392 KB |
Output is correct |
21 |
Correct |
197 ms |
9844 KB |
Output is correct |
22 |
Correct |
186 ms |
10668 KB |
Output is correct |
23 |
Correct |
119 ms |
9584 KB |
Output is correct |
24 |
Correct |
86 ms |
9612 KB |
Output is correct |
25 |
Correct |
213 ms |
13740 KB |
Output is correct |
26 |
Correct |
161 ms |
20096 KB |
Output is correct |
27 |
Correct |
256 ms |
18720 KB |
Output is correct |
28 |
Correct |
353 ms |
36208 KB |
Output is correct |
29 |
Correct |
432 ms |
34856 KB |
Output is correct |
30 |
Correct |
248 ms |
18720 KB |
Output is correct |
31 |
Correct |
233 ms |
18724 KB |
Output is correct |
32 |
Correct |
288 ms |
18724 KB |
Output is correct |
33 |
Correct |
363 ms |
17548 KB |
Output is correct |
34 |
Correct |
239 ms |
18108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
248 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
16 ms |
22064 KB |
Output is correct |
23 |
Correct |
15 ms |
18108 KB |
Output is correct |
24 |
Correct |
17 ms |
20688 KB |
Output is correct |
25 |
Correct |
18 ms |
20288 KB |
Output is correct |
26 |
Correct |
7 ms |
8276 KB |
Output is correct |
27 |
Correct |
14 ms |
19136 KB |
Output is correct |
28 |
Correct |
5 ms |
5188 KB |
Output is correct |
29 |
Correct |
8 ms |
8108 KB |
Output is correct |
30 |
Correct |
8 ms |
9152 KB |
Output is correct |
31 |
Correct |
15 ms |
15892 KB |
Output is correct |
32 |
Correct |
12 ms |
17216 KB |
Output is correct |
33 |
Correct |
17 ms |
18876 KB |
Output is correct |
34 |
Correct |
11 ms |
14344 KB |
Output is correct |
35 |
Correct |
21 ms |
19540 KB |
Output is correct |
36 |
Correct |
16 ms |
22348 KB |
Output is correct |
37 |
Correct |
15 ms |
19260 KB |
Output is correct |
38 |
Correct |
10 ms |
12500 KB |
Output is correct |
39 |
Correct |
203 ms |
9448 KB |
Output is correct |
40 |
Correct |
224 ms |
9392 KB |
Output is correct |
41 |
Correct |
197 ms |
9844 KB |
Output is correct |
42 |
Correct |
186 ms |
10668 KB |
Output is correct |
43 |
Correct |
119 ms |
9584 KB |
Output is correct |
44 |
Correct |
86 ms |
9612 KB |
Output is correct |
45 |
Correct |
213 ms |
13740 KB |
Output is correct |
46 |
Correct |
161 ms |
20096 KB |
Output is correct |
47 |
Correct |
256 ms |
18720 KB |
Output is correct |
48 |
Correct |
353 ms |
36208 KB |
Output is correct |
49 |
Correct |
432 ms |
34856 KB |
Output is correct |
50 |
Correct |
248 ms |
18720 KB |
Output is correct |
51 |
Correct |
233 ms |
18724 KB |
Output is correct |
52 |
Correct |
288 ms |
18724 KB |
Output is correct |
53 |
Correct |
363 ms |
17548 KB |
Output is correct |
54 |
Correct |
239 ms |
18108 KB |
Output is correct |
55 |
Correct |
17 ms |
1460 KB |
Output is correct |
56 |
Correct |
15 ms |
1364 KB |
Output is correct |
57 |
Correct |
158 ms |
11084 KB |
Output is correct |
58 |
Correct |
60 ms |
10736 KB |
Output is correct |
59 |
Correct |
232 ms |
25544 KB |
Output is correct |
60 |
Correct |
918 ms |
69872 KB |
Output is correct |
61 |
Correct |
257 ms |
19640 KB |
Output is correct |
62 |
Correct |
360 ms |
45396 KB |
Output is correct |
63 |
Correct |
343 ms |
45360 KB |
Output is correct |
64 |
Correct |
892 ms |
38980 KB |
Output is correct |
65 |
Correct |
368 ms |
19528 KB |
Output is correct |
66 |
Correct |
753 ms |
59544 KB |
Output is correct |
67 |
Correct |
215 ms |
46260 KB |
Output is correct |
68 |
Correct |
510 ms |
49632 KB |
Output is correct |
69 |
Correct |
482 ms |
49764 KB |
Output is correct |
70 |
Correct |
490 ms |
48648 KB |
Output is correct |