#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 200'000;
const int INF = 1e9;
vector<pair<int, int>> g[MAX_N];
unordered_map<long long, int> s[MAX_N];
int depth[MAX_N];
long long len[MAX_N];
int ans = INF;
int k;
template<typename T>
void merge(T& lhs, T& rhs, int v) {
if (lhs.size() > rhs.size()) {
swap(lhs, rhs);
}
for (const auto& [curr_len, curr_depth] : lhs) {
long long need_find = k + 2 * len[v] - curr_len;
if (rhs.find(need_find) != rhs.end()) {
ans = min(ans, curr_depth + rhs.at(need_find) - 2 * depth[v]);
}
}
for (const auto& [key, value] : lhs) {
if (rhs.find(key) == rhs.end()) {
rhs[key] = value;
} else {
rhs[key] = min(rhs[key], value);
}
}
}
void dfs(int v = 0, int p = -1) {
s[v][len[v]] = depth[v];
for (const auto& [u, weight] : g[v]) {
if (u != p) {
depth[u] = depth[v] + 1;
len[u] = len[v] + weight;
dfs(u, v);
merge(s[u], s[v], v);
}
}
}
int best_path(int n, int curr_k, int highways[][2], int length[]) {
for (int i = 0; i < n - 1; ++i) {
auto [u, v] = highways[i];
g[u].emplace_back(v, length[i]);
g[v].emplace_back(u, length[i]);
}
k = curr_k;
dfs();
if (ans == INF) ans = -1;
return ans;
}
// int main() {
// int n;
// cin >> n;
// cin >> k;
// for (int i = 0; i < n - 1; ++i) {
// int u, v, w;
// cin >> u >> v >> w;
// g[u].emplace_back(v, w);
// g[v].emplace_back(u, w);
// }
// dfs();
// cout << ans << '\n';
// }
/*
4 3
0 1 1
1 2 2
1 3 4
2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15956 KB |
Output is correct |
2 |
Correct |
9 ms |
15896 KB |
Output is correct |
3 |
Correct |
8 ms |
15956 KB |
Output is correct |
4 |
Correct |
8 ms |
15960 KB |
Output is correct |
5 |
Correct |
8 ms |
15956 KB |
Output is correct |
6 |
Correct |
9 ms |
15964 KB |
Output is correct |
7 |
Correct |
9 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15956 KB |
Output is correct |
10 |
Correct |
8 ms |
16004 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
15940 KB |
Output is correct |
13 |
Correct |
9 ms |
15972 KB |
Output is correct |
14 |
Correct |
9 ms |
15980 KB |
Output is correct |
15 |
Correct |
9 ms |
15968 KB |
Output is correct |
16 |
Correct |
9 ms |
15964 KB |
Output is correct |
17 |
Correct |
9 ms |
15956 KB |
Output is correct |
18 |
Correct |
9 ms |
16008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15956 KB |
Output is correct |
2 |
Correct |
9 ms |
15896 KB |
Output is correct |
3 |
Correct |
8 ms |
15956 KB |
Output is correct |
4 |
Correct |
8 ms |
15960 KB |
Output is correct |
5 |
Correct |
8 ms |
15956 KB |
Output is correct |
6 |
Correct |
9 ms |
15964 KB |
Output is correct |
7 |
Correct |
9 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15956 KB |
Output is correct |
10 |
Correct |
8 ms |
16004 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
15940 KB |
Output is correct |
13 |
Correct |
9 ms |
15972 KB |
Output is correct |
14 |
Correct |
9 ms |
15980 KB |
Output is correct |
15 |
Correct |
9 ms |
15968 KB |
Output is correct |
16 |
Correct |
9 ms |
15964 KB |
Output is correct |
17 |
Correct |
9 ms |
15956 KB |
Output is correct |
18 |
Correct |
9 ms |
16008 KB |
Output is correct |
19 |
Correct |
11 ms |
15876 KB |
Output is correct |
20 |
Correct |
10 ms |
15968 KB |
Output is correct |
21 |
Correct |
9 ms |
16212 KB |
Output is correct |
22 |
Correct |
10 ms |
16288 KB |
Output is correct |
23 |
Correct |
9 ms |
16268 KB |
Output is correct |
24 |
Correct |
9 ms |
16308 KB |
Output is correct |
25 |
Correct |
10 ms |
16228 KB |
Output is correct |
26 |
Correct |
10 ms |
16260 KB |
Output is correct |
27 |
Correct |
10 ms |
16216 KB |
Output is correct |
28 |
Correct |
10 ms |
16232 KB |
Output is correct |
29 |
Correct |
10 ms |
16260 KB |
Output is correct |
30 |
Correct |
11 ms |
16212 KB |
Output is correct |
31 |
Correct |
12 ms |
16236 KB |
Output is correct |
32 |
Correct |
10 ms |
16212 KB |
Output is correct |
33 |
Correct |
11 ms |
16232 KB |
Output is correct |
34 |
Correct |
10 ms |
16232 KB |
Output is correct |
35 |
Correct |
10 ms |
16212 KB |
Output is correct |
36 |
Correct |
9 ms |
16224 KB |
Output is correct |
37 |
Correct |
11 ms |
16212 KB |
Output is correct |
38 |
Correct |
12 ms |
16228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15956 KB |
Output is correct |
2 |
Correct |
9 ms |
15896 KB |
Output is correct |
3 |
Correct |
8 ms |
15956 KB |
Output is correct |
4 |
Correct |
8 ms |
15960 KB |
Output is correct |
5 |
Correct |
8 ms |
15956 KB |
Output is correct |
6 |
Correct |
9 ms |
15964 KB |
Output is correct |
7 |
Correct |
9 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15956 KB |
Output is correct |
10 |
Correct |
8 ms |
16004 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
15940 KB |
Output is correct |
13 |
Correct |
9 ms |
15972 KB |
Output is correct |
14 |
Correct |
9 ms |
15980 KB |
Output is correct |
15 |
Correct |
9 ms |
15968 KB |
Output is correct |
16 |
Correct |
9 ms |
15964 KB |
Output is correct |
17 |
Correct |
9 ms |
15956 KB |
Output is correct |
18 |
Correct |
9 ms |
16008 KB |
Output is correct |
19 |
Correct |
129 ms |
42948 KB |
Output is correct |
20 |
Correct |
117 ms |
42884 KB |
Output is correct |
21 |
Correct |
128 ms |
42676 KB |
Output is correct |
22 |
Correct |
112 ms |
42444 KB |
Output is correct |
23 |
Correct |
133 ms |
50968 KB |
Output is correct |
24 |
Correct |
112 ms |
44740 KB |
Output is correct |
25 |
Correct |
113 ms |
41484 KB |
Output is correct |
26 |
Correct |
82 ms |
44712 KB |
Output is correct |
27 |
Correct |
211 ms |
63592 KB |
Output is correct |
28 |
Correct |
260 ms |
83112 KB |
Output is correct |
29 |
Correct |
288 ms |
82800 KB |
Output is correct |
30 |
Correct |
193 ms |
63636 KB |
Output is correct |
31 |
Correct |
204 ms |
63600 KB |
Output is correct |
32 |
Correct |
283 ms |
63612 KB |
Output is correct |
33 |
Correct |
202 ms |
66564 KB |
Output is correct |
34 |
Correct |
371 ms |
89600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15956 KB |
Output is correct |
2 |
Correct |
9 ms |
15896 KB |
Output is correct |
3 |
Correct |
8 ms |
15956 KB |
Output is correct |
4 |
Correct |
8 ms |
15960 KB |
Output is correct |
5 |
Correct |
8 ms |
15956 KB |
Output is correct |
6 |
Correct |
9 ms |
15964 KB |
Output is correct |
7 |
Correct |
9 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15956 KB |
Output is correct |
10 |
Correct |
8 ms |
16004 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
15940 KB |
Output is correct |
13 |
Correct |
9 ms |
15972 KB |
Output is correct |
14 |
Correct |
9 ms |
15980 KB |
Output is correct |
15 |
Correct |
9 ms |
15968 KB |
Output is correct |
16 |
Correct |
9 ms |
15964 KB |
Output is correct |
17 |
Correct |
9 ms |
15956 KB |
Output is correct |
18 |
Correct |
9 ms |
16008 KB |
Output is correct |
19 |
Correct |
11 ms |
15876 KB |
Output is correct |
20 |
Correct |
10 ms |
15968 KB |
Output is correct |
21 |
Correct |
9 ms |
16212 KB |
Output is correct |
22 |
Correct |
10 ms |
16288 KB |
Output is correct |
23 |
Correct |
9 ms |
16268 KB |
Output is correct |
24 |
Correct |
9 ms |
16308 KB |
Output is correct |
25 |
Correct |
10 ms |
16228 KB |
Output is correct |
26 |
Correct |
10 ms |
16260 KB |
Output is correct |
27 |
Correct |
10 ms |
16216 KB |
Output is correct |
28 |
Correct |
10 ms |
16232 KB |
Output is correct |
29 |
Correct |
10 ms |
16260 KB |
Output is correct |
30 |
Correct |
11 ms |
16212 KB |
Output is correct |
31 |
Correct |
12 ms |
16236 KB |
Output is correct |
32 |
Correct |
10 ms |
16212 KB |
Output is correct |
33 |
Correct |
11 ms |
16232 KB |
Output is correct |
34 |
Correct |
10 ms |
16232 KB |
Output is correct |
35 |
Correct |
10 ms |
16212 KB |
Output is correct |
36 |
Correct |
9 ms |
16224 KB |
Output is correct |
37 |
Correct |
11 ms |
16212 KB |
Output is correct |
38 |
Correct |
12 ms |
16228 KB |
Output is correct |
39 |
Correct |
129 ms |
42948 KB |
Output is correct |
40 |
Correct |
117 ms |
42884 KB |
Output is correct |
41 |
Correct |
128 ms |
42676 KB |
Output is correct |
42 |
Correct |
112 ms |
42444 KB |
Output is correct |
43 |
Correct |
133 ms |
50968 KB |
Output is correct |
44 |
Correct |
112 ms |
44740 KB |
Output is correct |
45 |
Correct |
113 ms |
41484 KB |
Output is correct |
46 |
Correct |
82 ms |
44712 KB |
Output is correct |
47 |
Correct |
211 ms |
63592 KB |
Output is correct |
48 |
Correct |
260 ms |
83112 KB |
Output is correct |
49 |
Correct |
288 ms |
82800 KB |
Output is correct |
50 |
Correct |
193 ms |
63636 KB |
Output is correct |
51 |
Correct |
204 ms |
63600 KB |
Output is correct |
52 |
Correct |
283 ms |
63612 KB |
Output is correct |
53 |
Correct |
202 ms |
66564 KB |
Output is correct |
54 |
Correct |
371 ms |
89600 KB |
Output is correct |
55 |
Correct |
19 ms |
19272 KB |
Output is correct |
56 |
Correct |
16 ms |
18388 KB |
Output is correct |
57 |
Correct |
79 ms |
40920 KB |
Output is correct |
58 |
Correct |
52 ms |
37312 KB |
Output is correct |
59 |
Correct |
71 ms |
49372 KB |
Output is correct |
60 |
Correct |
282 ms |
82704 KB |
Output is correct |
61 |
Correct |
268 ms |
66088 KB |
Output is correct |
62 |
Correct |
190 ms |
63648 KB |
Output is correct |
63 |
Correct |
289 ms |
63620 KB |
Output is correct |
64 |
Correct |
447 ms |
99732 KB |
Output is correct |
65 |
Correct |
451 ms |
99064 KB |
Output is correct |
66 |
Correct |
270 ms |
81808 KB |
Output is correct |
67 |
Correct |
182 ms |
60112 KB |
Output is correct |
68 |
Correct |
390 ms |
74780 KB |
Output is correct |
69 |
Correct |
399 ms |
78140 KB |
Output is correct |
70 |
Correct |
327 ms |
72148 KB |
Output is correct |