#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define DEBUG(x) //x
#define A(x) DEBUG(assert(x))
#define PRINT(x) DEBUG(cerr << x)
#define PV(x) DEBUG(cerr << #x << " = " << x << '\n')
#define PV2(x) DEBUG(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define PAR(x) DEBUG(PRINT(#x << " = { "); for (auto y : x) PRINT(y << ' '); PRINT("}\n");)
#define PAR2(x) DEBUG(PRINT(#x << " = { "); for (auto [y, z] : x) PRINT(y << ',' << z << " "); PRINT("}\n");)
#define PAR2D(x) DEBUG(PRINT(#x << ":\n"); for (auto arr : x) {PAR(arr);} PRINT('\n'));
typedef long long i64;
const int INF = 1000000007; //998244353;
int n, k, ans;
vector<i64> pref;
vector<int> sub, dep;
vector<vector<pair<int, i64>>> edges;
vector<map<i64, int>> paths;
void dfs1(int node, int par) {
for (auto [next, wt] : edges[node]) {
if (next == par) continue;
pref[next] = pref[node] + wt;
dep[next] = dep[node] + 1;
dfs1(next, node);
sub[node] += sub[next];
}
}
void dfs2(int node, int par) {
int heavy = -1, mx = -1;
for (auto [next, wt] : edges[node]) {
if (next == par) continue;
if (sub[next] > mx) {
mx = sub[next];
heavy = next;
}
}
for (auto [next, wt] : edges[node]) {
if (next == par || next == heavy) continue;
dfs2(next, node);
}
if (heavy != -1) {
dfs2(heavy, node);
swap(paths[heavy], paths[node]);
}
if (!paths[node].count(pref[node]) || paths[node][pref[node]] > dep[node]) {
paths[node][pref[node]] = dep[node];
}
for (auto [next, wt] : edges[node]) {
if (next == par || next == heavy) continue;
for (auto [s, d] : paths[next]) {
if (paths[node].count(k + 2 * pref[node] - s)) {
ans = min(ans, d + paths[node][k + 2 * pref[node] - s] - 2 * dep[node]);
}
}
for (auto [s, d] : paths[next]) {
if (!paths[node].count(s) || paths[node][s] >= d) {
paths[node][s] = d;
}
}
}
if (paths[node].count(k + pref[node])) {
ans = min(ans, paths[node][k + pref[node]] - dep[node]);
}
PV(node); PAR2(paths[node]);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
edges.resize(n);
for (int i = 0; i < n - 1; ++i) {
edges[H[i][0]].emplace_back(H[i][1], L[i]);
edges[H[i][1]].emplace_back(H[i][0], L[i]);
}
pref.resize(n, 0); sub.resize(n, 1); dep.resize(n, 0);
dfs1(0, 0);
paths.resize(n); ans = INF;
dfs2(0, 0);
return (ans == INF ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
576 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
704 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
97 ms |
27664 KB |
Output is correct |
20 |
Correct |
94 ms |
27656 KB |
Output is correct |
21 |
Correct |
87 ms |
27596 KB |
Output is correct |
22 |
Correct |
97 ms |
27700 KB |
Output is correct |
23 |
Correct |
128 ms |
41424 KB |
Output is correct |
24 |
Correct |
98 ms |
33464 KB |
Output is correct |
25 |
Correct |
79 ms |
26668 KB |
Output is correct |
26 |
Correct |
47 ms |
34940 KB |
Output is correct |
27 |
Correct |
156 ms |
46336 KB |
Output is correct |
28 |
Correct |
221 ms |
82260 KB |
Output is correct |
29 |
Correct |
222 ms |
80388 KB |
Output is correct |
30 |
Correct |
153 ms |
46228 KB |
Output is correct |
31 |
Correct |
159 ms |
46216 KB |
Output is correct |
32 |
Correct |
217 ms |
46368 KB |
Output is correct |
33 |
Correct |
184 ms |
45152 KB |
Output is correct |
34 |
Correct |
271 ms |
77984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
576 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
704 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
97 ms |
27664 KB |
Output is correct |
40 |
Correct |
94 ms |
27656 KB |
Output is correct |
41 |
Correct |
87 ms |
27596 KB |
Output is correct |
42 |
Correct |
97 ms |
27700 KB |
Output is correct |
43 |
Correct |
128 ms |
41424 KB |
Output is correct |
44 |
Correct |
98 ms |
33464 KB |
Output is correct |
45 |
Correct |
79 ms |
26668 KB |
Output is correct |
46 |
Correct |
47 ms |
34940 KB |
Output is correct |
47 |
Correct |
156 ms |
46336 KB |
Output is correct |
48 |
Correct |
221 ms |
82260 KB |
Output is correct |
49 |
Correct |
222 ms |
80388 KB |
Output is correct |
50 |
Correct |
153 ms |
46228 KB |
Output is correct |
51 |
Correct |
159 ms |
46216 KB |
Output is correct |
52 |
Correct |
217 ms |
46368 KB |
Output is correct |
53 |
Correct |
184 ms |
45152 KB |
Output is correct |
54 |
Correct |
271 ms |
77984 KB |
Output is correct |
55 |
Correct |
12 ms |
4052 KB |
Output is correct |
56 |
Correct |
6 ms |
2748 KB |
Output is correct |
57 |
Correct |
54 ms |
26564 KB |
Output is correct |
58 |
Correct |
46 ms |
22048 KB |
Output is correct |
59 |
Correct |
69 ms |
41136 KB |
Output is correct |
60 |
Correct |
218 ms |
80932 KB |
Output is correct |
61 |
Correct |
179 ms |
49816 KB |
Output is correct |
62 |
Correct |
159 ms |
46196 KB |
Output is correct |
63 |
Correct |
203 ms |
46448 KB |
Output is correct |
64 |
Correct |
374 ms |
96548 KB |
Output is correct |
65 |
Correct |
365 ms |
97276 KB |
Output is correct |
66 |
Correct |
234 ms |
76380 KB |
Output is correct |
67 |
Correct |
169 ms |
45288 KB |
Output is correct |
68 |
Correct |
291 ms |
64324 KB |
Output is correct |
69 |
Correct |
283 ms |
64800 KB |
Output is correct |
70 |
Correct |
269 ms |
61372 KB |
Output is correct |