#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a ? (a = b, true) : false; }
template<class T> bool chmax(T &a, T b) { return a < b ? (a = b, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 300010, inf = 1e9;
vector<pair<int,int>> edge[MAX_N];
map<ll,int> amp[MAX_N];
int k;
int dfs(int x, int lst = -1, ll len = 0) {
static int dep;
++dep;
DE(x, dep, len);
int res = inf;
auto &cur = amp[x]; cur[len] = dep;
for (auto [u, w] : edge[x]) if (u != lst) {
chmin(res, dfs(u, x, len + w));
auto &nxt = amp[u];
if (cur.size() < nxt.size()) swap(cur, nxt);
for (auto [L, D] : nxt) {
if (cur.count(k + 2 * len - L))
chmin(res, D + cur[k + 2 * len - L] - 2 * dep);
}
for (auto [L, D] : nxt)
if (cur.count(L))
chmin(cur[L], D);
else cur[L] = D;
nxt.clear();
}
// if (false) if (cur.count(k + len)) {
// DE(dep, cur[k + len]);
// chmin(res, cur[k + len] - dep);
// }
--dep;
return res;
}
int best_path(int N, int K, int H[][2], int L[]) {
static int cnt;
assert(++cnt == 1);
for (int i = 0;i+1 < N;++i) {
int a = H[i][0], b = H[i][1], w = L[i];
edge[a].pb(b, w);
edge[b].pb(a, w);
}
k = K;
int res = dfs(0);
return res == inf ? -1 : res;
}
Compilation message
race.cpp: In function 'int dfs(int, int, ll)':
race.cpp:15:17: warning: statement has no effect [-Wunused-value]
15 | #define DE(...) 0
| ^
race.cpp:30:2: note: in expansion of macro 'DE'
30 | DE(x, dep, len);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
16 ms |
21484 KB |
Output is correct |
4 |
Correct |
16 ms |
21484 KB |
Output is correct |
5 |
Correct |
15 ms |
21484 KB |
Output is correct |
6 |
Correct |
15 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21484 KB |
Output is correct |
11 |
Correct |
15 ms |
21484 KB |
Output is correct |
12 |
Correct |
16 ms |
21484 KB |
Output is correct |
13 |
Correct |
15 ms |
21484 KB |
Output is correct |
14 |
Correct |
17 ms |
21632 KB |
Output is correct |
15 |
Correct |
15 ms |
21484 KB |
Output is correct |
16 |
Correct |
15 ms |
21484 KB |
Output is correct |
17 |
Correct |
15 ms |
21484 KB |
Output is correct |
18 |
Correct |
15 ms |
21504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
16 ms |
21484 KB |
Output is correct |
4 |
Correct |
16 ms |
21484 KB |
Output is correct |
5 |
Correct |
15 ms |
21484 KB |
Output is correct |
6 |
Correct |
15 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21484 KB |
Output is correct |
11 |
Correct |
15 ms |
21484 KB |
Output is correct |
12 |
Correct |
16 ms |
21484 KB |
Output is correct |
13 |
Correct |
15 ms |
21484 KB |
Output is correct |
14 |
Correct |
17 ms |
21632 KB |
Output is correct |
15 |
Correct |
15 ms |
21484 KB |
Output is correct |
16 |
Correct |
15 ms |
21484 KB |
Output is correct |
17 |
Correct |
15 ms |
21484 KB |
Output is correct |
18 |
Correct |
15 ms |
21504 KB |
Output is correct |
19 |
Correct |
15 ms |
21484 KB |
Output is correct |
20 |
Correct |
14 ms |
21612 KB |
Output is correct |
21 |
Correct |
16 ms |
21484 KB |
Output is correct |
22 |
Correct |
16 ms |
21632 KB |
Output is correct |
23 |
Correct |
16 ms |
21612 KB |
Output is correct |
24 |
Correct |
16 ms |
21612 KB |
Output is correct |
25 |
Correct |
16 ms |
21612 KB |
Output is correct |
26 |
Correct |
16 ms |
21612 KB |
Output is correct |
27 |
Correct |
15 ms |
21484 KB |
Output is correct |
28 |
Correct |
18 ms |
21612 KB |
Output is correct |
29 |
Correct |
16 ms |
21612 KB |
Output is correct |
30 |
Correct |
20 ms |
21612 KB |
Output is correct |
31 |
Correct |
16 ms |
21612 KB |
Output is correct |
32 |
Correct |
16 ms |
21612 KB |
Output is correct |
33 |
Correct |
16 ms |
21612 KB |
Output is correct |
34 |
Correct |
16 ms |
21612 KB |
Output is correct |
35 |
Correct |
16 ms |
21612 KB |
Output is correct |
36 |
Correct |
15 ms |
21612 KB |
Output is correct |
37 |
Correct |
15 ms |
21612 KB |
Output is correct |
38 |
Correct |
16 ms |
21612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
16 ms |
21484 KB |
Output is correct |
4 |
Correct |
16 ms |
21484 KB |
Output is correct |
5 |
Correct |
15 ms |
21484 KB |
Output is correct |
6 |
Correct |
15 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21484 KB |
Output is correct |
11 |
Correct |
15 ms |
21484 KB |
Output is correct |
12 |
Correct |
16 ms |
21484 KB |
Output is correct |
13 |
Correct |
15 ms |
21484 KB |
Output is correct |
14 |
Correct |
17 ms |
21632 KB |
Output is correct |
15 |
Correct |
15 ms |
21484 KB |
Output is correct |
16 |
Correct |
15 ms |
21484 KB |
Output is correct |
17 |
Correct |
15 ms |
21484 KB |
Output is correct |
18 |
Correct |
15 ms |
21504 KB |
Output is correct |
19 |
Correct |
129 ms |
26348 KB |
Output is correct |
20 |
Correct |
132 ms |
27756 KB |
Output is correct |
21 |
Correct |
132 ms |
27884 KB |
Output is correct |
22 |
Correct |
130 ms |
27756 KB |
Output is correct |
23 |
Correct |
162 ms |
28396 KB |
Output is correct |
24 |
Correct |
143 ms |
28012 KB |
Output is correct |
25 |
Correct |
131 ms |
40684 KB |
Output is correct |
26 |
Correct |
91 ms |
52224 KB |
Output is correct |
27 |
Correct |
227 ms |
34668 KB |
Output is correct |
28 |
Correct |
325 ms |
83180 KB |
Output is correct |
29 |
Correct |
324 ms |
81388 KB |
Output is correct |
30 |
Correct |
228 ms |
34540 KB |
Output is correct |
31 |
Correct |
227 ms |
34540 KB |
Output is correct |
32 |
Correct |
288 ms |
35052 KB |
Output is correct |
33 |
Correct |
253 ms |
34212 KB |
Output is correct |
34 |
Correct |
404 ms |
47724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
16 ms |
21484 KB |
Output is correct |
4 |
Correct |
16 ms |
21484 KB |
Output is correct |
5 |
Correct |
15 ms |
21484 KB |
Output is correct |
6 |
Correct |
15 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21484 KB |
Output is correct |
11 |
Correct |
15 ms |
21484 KB |
Output is correct |
12 |
Correct |
16 ms |
21484 KB |
Output is correct |
13 |
Correct |
15 ms |
21484 KB |
Output is correct |
14 |
Correct |
17 ms |
21632 KB |
Output is correct |
15 |
Correct |
15 ms |
21484 KB |
Output is correct |
16 |
Correct |
15 ms |
21484 KB |
Output is correct |
17 |
Correct |
15 ms |
21484 KB |
Output is correct |
18 |
Correct |
15 ms |
21504 KB |
Output is correct |
19 |
Correct |
15 ms |
21484 KB |
Output is correct |
20 |
Correct |
14 ms |
21612 KB |
Output is correct |
21 |
Correct |
16 ms |
21484 KB |
Output is correct |
22 |
Correct |
16 ms |
21632 KB |
Output is correct |
23 |
Correct |
16 ms |
21612 KB |
Output is correct |
24 |
Correct |
16 ms |
21612 KB |
Output is correct |
25 |
Correct |
16 ms |
21612 KB |
Output is correct |
26 |
Correct |
16 ms |
21612 KB |
Output is correct |
27 |
Correct |
15 ms |
21484 KB |
Output is correct |
28 |
Correct |
18 ms |
21612 KB |
Output is correct |
29 |
Correct |
16 ms |
21612 KB |
Output is correct |
30 |
Correct |
20 ms |
21612 KB |
Output is correct |
31 |
Correct |
16 ms |
21612 KB |
Output is correct |
32 |
Correct |
16 ms |
21612 KB |
Output is correct |
33 |
Correct |
16 ms |
21612 KB |
Output is correct |
34 |
Correct |
16 ms |
21612 KB |
Output is correct |
35 |
Correct |
16 ms |
21612 KB |
Output is correct |
36 |
Correct |
15 ms |
21612 KB |
Output is correct |
37 |
Correct |
15 ms |
21612 KB |
Output is correct |
38 |
Correct |
16 ms |
21612 KB |
Output is correct |
39 |
Correct |
129 ms |
26348 KB |
Output is correct |
40 |
Correct |
132 ms |
27756 KB |
Output is correct |
41 |
Correct |
132 ms |
27884 KB |
Output is correct |
42 |
Correct |
130 ms |
27756 KB |
Output is correct |
43 |
Correct |
162 ms |
28396 KB |
Output is correct |
44 |
Correct |
143 ms |
28012 KB |
Output is correct |
45 |
Correct |
131 ms |
40684 KB |
Output is correct |
46 |
Correct |
91 ms |
52224 KB |
Output is correct |
47 |
Correct |
227 ms |
34668 KB |
Output is correct |
48 |
Correct |
325 ms |
83180 KB |
Output is correct |
49 |
Correct |
324 ms |
81388 KB |
Output is correct |
50 |
Correct |
228 ms |
34540 KB |
Output is correct |
51 |
Correct |
227 ms |
34540 KB |
Output is correct |
52 |
Correct |
288 ms |
35052 KB |
Output is correct |
53 |
Correct |
253 ms |
34212 KB |
Output is correct |
54 |
Correct |
404 ms |
47724 KB |
Output is correct |
55 |
Correct |
28 ms |
22508 KB |
Output is correct |
56 |
Correct |
21 ms |
22124 KB |
Output is correct |
57 |
Correct |
76 ms |
27884 KB |
Output is correct |
58 |
Correct |
65 ms |
27616 KB |
Output is correct |
59 |
Correct |
117 ms |
52204 KB |
Output is correct |
60 |
Correct |
317 ms |
81688 KB |
Output is correct |
61 |
Correct |
267 ms |
36608 KB |
Output is correct |
62 |
Correct |
223 ms |
34796 KB |
Output is correct |
63 |
Correct |
272 ms |
34944 KB |
Output is correct |
64 |
Correct |
530 ms |
43372 KB |
Output is correct |
65 |
Correct |
505 ms |
47852 KB |
Output is correct |
66 |
Correct |
349 ms |
77292 KB |
Output is correct |
67 |
Correct |
195 ms |
35164 KB |
Output is correct |
68 |
Correct |
397 ms |
45804 KB |
Output is correct |
69 |
Correct |
434 ms |
50412 KB |
Output is correct |
70 |
Correct |
431 ms |
44920 KB |
Output is correct |