// looking to shine brighter in this world...
#define TASK "race"
#include "race.h"
#ifdef LOCAL
#include "local.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#define DB(...)
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
#define Fe(...) for (auto __VA_ARGS__)
template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); }
template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; }
template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; }
const bool __initialization = []() {
cin.tie(nullptr)->sync_with_stdio(false);
if (fopen(TASK".inp", "r")) {
(void)(!freopen(TASK".inp", "r", stdin));
(void)(!freopen(TASK".out", "w", stdout)); }
cout << setprecision(12) << fixed;
return true;
}();
constexpr ld eps = 1e-9;
constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 1e18;
// ================================================================================
constexpr int maxn = 2e5;
constexpr int maxk = 1e6 + 5;
int n, k;
struct Info { int v, c; };
vector<Info> g[maxn];
int sz[maxn];
bool rem[maxn];
int ans = inf;
int mindep[maxk];
enum { Get, Update, Clear };
int getSz(int u, int p) {
sz[u] = 1;
Fe (&[v, c] : g[u]) {
if (v == p || rem[v]) continue;
sz[u] += getSz(v, u);
}
return sz[u];
}
int getCen(int u, int sum, int p) {
Fe (&[v, c] : g[u]) {
if (v == p || rem[v]) continue;
if (sz[v] * 2 > sum) {
return getCen(v, sum, u);
}
}
return u;
}
void calc(int u, int p, int task, int dep, int sum) {
if (sum > k) return;
switch (task) {
case Get:
chmin(ans, dep + mindep[k-sum]);
break;
case Update:
chmin(mindep[sum], dep);
break;
case Clear:
mindep[sum] = inf;
break;
default:
assert(false);
}
Fe (&[v, c] : g[u]) {
if (!rem[v] && v != p) {
calc(v, u, task, dep + 1, sum + c);
}
}
}
void process(int u) {
int cen = getCen(u, getSz(u, -1), -1);
rem[cen] = true;
Fe (&[v, c] : g[cen]) {
if (!rem[v]) {
calc(v, cen, Get, 1, c);
calc(v, cen, Update, 1, c);
}
}
Fe (&[v, c] : g[cen]) {
if (!rem[v]) {
calc(v, cen, Clear, 1, c);
}
}
Fe (&[v, c] : g[cen]) {
if (!rem[v]) {
process(v);
}
}
}
int best_path(int N, int K, int h[][2], int l[]) {
n = N, k = K;
Rep (i, n - 1) {
int u = h[i][0];
int v = h[i][1];
int c = l[i];
g[u].push_back({ v, c });
g[v].push_back({ u, c });
}
memset(mindep, 0x3f, sizeof(mindep));
mindep[0] = 0;
process(0);
if (ans == inf) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
5 ms |
8908 KB |
Output is correct |
3 |
Correct |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8872 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8884 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8892 KB |
Output is correct |
17 |
Correct |
5 ms |
8908 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 |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8872 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8884 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8892 KB |
Output is correct |
17 |
Correct |
5 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
5 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
5 ms |
8908 KB |
Output is correct |
23 |
Correct |
5 ms |
9008 KB |
Output is correct |
24 |
Correct |
5 ms |
8908 KB |
Output is correct |
25 |
Correct |
6 ms |
8908 KB |
Output is correct |
26 |
Correct |
6 ms |
9008 KB |
Output is correct |
27 |
Correct |
5 ms |
8908 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
6 ms |
8908 KB |
Output is correct |
31 |
Correct |
5 ms |
8908 KB |
Output is correct |
32 |
Correct |
5 ms |
8908 KB |
Output is correct |
33 |
Correct |
6 ms |
8908 KB |
Output is correct |
34 |
Correct |
6 ms |
8908 KB |
Output is correct |
35 |
Correct |
6 ms |
8908 KB |
Output is correct |
36 |
Correct |
5 ms |
8908 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
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 |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8872 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8884 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8892 KB |
Output is correct |
17 |
Correct |
5 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
141 ms |
14156 KB |
Output is correct |
20 |
Correct |
153 ms |
14268 KB |
Output is correct |
21 |
Correct |
182 ms |
14204 KB |
Output is correct |
22 |
Correct |
146 ms |
14232 KB |
Output is correct |
23 |
Correct |
84 ms |
14516 KB |
Output is correct |
24 |
Correct |
65 ms |
14304 KB |
Output is correct |
25 |
Correct |
145 ms |
17148 KB |
Output is correct |
26 |
Correct |
109 ms |
19908 KB |
Output is correct |
27 |
Correct |
193 ms |
19740 KB |
Output is correct |
28 |
Correct |
281 ms |
30940 KB |
Output is correct |
29 |
Correct |
258 ms |
30008 KB |
Output is correct |
30 |
Correct |
197 ms |
19792 KB |
Output is correct |
31 |
Correct |
182 ms |
19780 KB |
Output is correct |
32 |
Correct |
219 ms |
19796 KB |
Output is correct |
33 |
Correct |
233 ms |
18628 KB |
Output is correct |
34 |
Correct |
208 ms |
18524 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 |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8872 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8884 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8892 KB |
Output is correct |
17 |
Correct |
5 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
5 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
5 ms |
8908 KB |
Output is correct |
23 |
Correct |
5 ms |
9008 KB |
Output is correct |
24 |
Correct |
5 ms |
8908 KB |
Output is correct |
25 |
Correct |
6 ms |
8908 KB |
Output is correct |
26 |
Correct |
6 ms |
9008 KB |
Output is correct |
27 |
Correct |
5 ms |
8908 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
6 ms |
8908 KB |
Output is correct |
31 |
Correct |
5 ms |
8908 KB |
Output is correct |
32 |
Correct |
5 ms |
8908 KB |
Output is correct |
33 |
Correct |
6 ms |
8908 KB |
Output is correct |
34 |
Correct |
6 ms |
8908 KB |
Output is correct |
35 |
Correct |
6 ms |
8908 KB |
Output is correct |
36 |
Correct |
5 ms |
8908 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
5 ms |
8908 KB |
Output is correct |
39 |
Correct |
141 ms |
14156 KB |
Output is correct |
40 |
Correct |
153 ms |
14268 KB |
Output is correct |
41 |
Correct |
182 ms |
14204 KB |
Output is correct |
42 |
Correct |
146 ms |
14232 KB |
Output is correct |
43 |
Correct |
84 ms |
14516 KB |
Output is correct |
44 |
Correct |
65 ms |
14304 KB |
Output is correct |
45 |
Correct |
145 ms |
17148 KB |
Output is correct |
46 |
Correct |
109 ms |
19908 KB |
Output is correct |
47 |
Correct |
193 ms |
19740 KB |
Output is correct |
48 |
Correct |
281 ms |
30940 KB |
Output is correct |
49 |
Correct |
258 ms |
30008 KB |
Output is correct |
50 |
Correct |
197 ms |
19792 KB |
Output is correct |
51 |
Correct |
182 ms |
19780 KB |
Output is correct |
52 |
Correct |
219 ms |
19796 KB |
Output is correct |
53 |
Correct |
233 ms |
18628 KB |
Output is correct |
54 |
Correct |
208 ms |
18524 KB |
Output is correct |
55 |
Correct |
14 ms |
9452 KB |
Output is correct |
56 |
Correct |
18 ms |
9488 KB |
Output is correct |
57 |
Correct |
98 ms |
14444 KB |
Output is correct |
58 |
Correct |
42 ms |
14484 KB |
Output is correct |
59 |
Correct |
113 ms |
19900 KB |
Output is correct |
60 |
Correct |
534 ms |
30404 KB |
Output is correct |
61 |
Correct |
198 ms |
19804 KB |
Output is correct |
62 |
Correct |
227 ms |
19788 KB |
Output is correct |
63 |
Correct |
320 ms |
19808 KB |
Output is correct |
64 |
Correct |
596 ms |
19692 KB |
Output is correct |
65 |
Correct |
228 ms |
19740 KB |
Output is correct |
66 |
Correct |
474 ms |
27900 KB |
Output is correct |
67 |
Correct |
137 ms |
20060 KB |
Output is correct |
68 |
Correct |
337 ms |
19960 KB |
Output is correct |
69 |
Correct |
300 ms |
20064 KB |
Output is correct |
70 |
Correct |
298 ms |
19472 KB |
Output is correct |