#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16072 KB |
Output is correct |
2 |
Correct |
9 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
15956 KB |
Output is correct |
4 |
Correct |
9 ms |
15984 KB |
Output is correct |
5 |
Correct |
10 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
15960 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15940 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
16008 KB |
Output is correct |
11 |
Correct |
9 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
16008 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
9 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
11 ms |
16004 KB |
Output is correct |
18 |
Correct |
9 ms |
15896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16072 KB |
Output is correct |
2 |
Correct |
9 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
15956 KB |
Output is correct |
4 |
Correct |
9 ms |
15984 KB |
Output is correct |
5 |
Correct |
10 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
15960 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15940 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
16008 KB |
Output is correct |
11 |
Correct |
9 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
16008 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
9 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
11 ms |
16004 KB |
Output is correct |
18 |
Correct |
9 ms |
15896 KB |
Output is correct |
19 |
Correct |
9 ms |
16084 KB |
Output is correct |
20 |
Correct |
9 ms |
15956 KB |
Output is correct |
21 |
Correct |
9 ms |
16268 KB |
Output is correct |
22 |
Correct |
11 ms |
16276 KB |
Output is correct |
23 |
Correct |
11 ms |
16280 KB |
Output is correct |
24 |
Correct |
10 ms |
16220 KB |
Output is correct |
25 |
Correct |
11 ms |
16296 KB |
Output is correct |
26 |
Correct |
9 ms |
16296 KB |
Output is correct |
27 |
Correct |
10 ms |
16084 KB |
Output is correct |
28 |
Correct |
9 ms |
16212 KB |
Output is correct |
29 |
Correct |
9 ms |
16292 KB |
Output is correct |
30 |
Correct |
9 ms |
16212 KB |
Output is correct |
31 |
Correct |
11 ms |
16212 KB |
Output is correct |
32 |
Correct |
10 ms |
16212 KB |
Output is correct |
33 |
Correct |
11 ms |
16340 KB |
Output is correct |
34 |
Correct |
10 ms |
16212 KB |
Output is correct |
35 |
Correct |
10 ms |
16212 KB |
Output is correct |
36 |
Correct |
9 ms |
16212 KB |
Output is correct |
37 |
Correct |
10 ms |
16192 KB |
Output is correct |
38 |
Correct |
9 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16072 KB |
Output is correct |
2 |
Correct |
9 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
15956 KB |
Output is correct |
4 |
Correct |
9 ms |
15984 KB |
Output is correct |
5 |
Correct |
10 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
15960 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15940 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
16008 KB |
Output is correct |
11 |
Correct |
9 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
16008 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
9 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
11 ms |
16004 KB |
Output is correct |
18 |
Correct |
9 ms |
15896 KB |
Output is correct |
19 |
Correct |
114 ms |
41584 KB |
Output is correct |
20 |
Correct |
111 ms |
41472 KB |
Output is correct |
21 |
Correct |
113 ms |
41296 KB |
Output is correct |
22 |
Correct |
118 ms |
40960 KB |
Output is correct |
23 |
Correct |
132 ms |
49452 KB |
Output is correct |
24 |
Correct |
115 ms |
43260 KB |
Output is correct |
25 |
Correct |
116 ms |
40124 KB |
Output is correct |
26 |
Correct |
65 ms |
43464 KB |
Output is correct |
27 |
Correct |
198 ms |
60616 KB |
Output is correct |
28 |
Correct |
262 ms |
79940 KB |
Output is correct |
29 |
Correct |
261 ms |
79608 KB |
Output is correct |
30 |
Correct |
199 ms |
60720 KB |
Output is correct |
31 |
Correct |
223 ms |
60724 KB |
Output is correct |
32 |
Correct |
314 ms |
60624 KB |
Output is correct |
33 |
Correct |
200 ms |
63820 KB |
Output is correct |
34 |
Correct |
374 ms |
85764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16072 KB |
Output is correct |
2 |
Correct |
9 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
15956 KB |
Output is correct |
4 |
Correct |
9 ms |
15984 KB |
Output is correct |
5 |
Correct |
10 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
15960 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15940 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
16008 KB |
Output is correct |
11 |
Correct |
9 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
16008 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
9 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
11 ms |
16004 KB |
Output is correct |
18 |
Correct |
9 ms |
15896 KB |
Output is correct |
19 |
Correct |
9 ms |
16084 KB |
Output is correct |
20 |
Correct |
9 ms |
15956 KB |
Output is correct |
21 |
Correct |
9 ms |
16268 KB |
Output is correct |
22 |
Correct |
11 ms |
16276 KB |
Output is correct |
23 |
Correct |
11 ms |
16280 KB |
Output is correct |
24 |
Correct |
10 ms |
16220 KB |
Output is correct |
25 |
Correct |
11 ms |
16296 KB |
Output is correct |
26 |
Correct |
9 ms |
16296 KB |
Output is correct |
27 |
Correct |
10 ms |
16084 KB |
Output is correct |
28 |
Correct |
9 ms |
16212 KB |
Output is correct |
29 |
Correct |
9 ms |
16292 KB |
Output is correct |
30 |
Correct |
9 ms |
16212 KB |
Output is correct |
31 |
Correct |
11 ms |
16212 KB |
Output is correct |
32 |
Correct |
10 ms |
16212 KB |
Output is correct |
33 |
Correct |
11 ms |
16340 KB |
Output is correct |
34 |
Correct |
10 ms |
16212 KB |
Output is correct |
35 |
Correct |
10 ms |
16212 KB |
Output is correct |
36 |
Correct |
9 ms |
16212 KB |
Output is correct |
37 |
Correct |
10 ms |
16192 KB |
Output is correct |
38 |
Correct |
9 ms |
16212 KB |
Output is correct |
39 |
Correct |
114 ms |
41584 KB |
Output is correct |
40 |
Correct |
111 ms |
41472 KB |
Output is correct |
41 |
Correct |
113 ms |
41296 KB |
Output is correct |
42 |
Correct |
118 ms |
40960 KB |
Output is correct |
43 |
Correct |
132 ms |
49452 KB |
Output is correct |
44 |
Correct |
115 ms |
43260 KB |
Output is correct |
45 |
Correct |
116 ms |
40124 KB |
Output is correct |
46 |
Correct |
65 ms |
43464 KB |
Output is correct |
47 |
Correct |
198 ms |
60616 KB |
Output is correct |
48 |
Correct |
262 ms |
79940 KB |
Output is correct |
49 |
Correct |
261 ms |
79608 KB |
Output is correct |
50 |
Correct |
199 ms |
60720 KB |
Output is correct |
51 |
Correct |
223 ms |
60724 KB |
Output is correct |
52 |
Correct |
314 ms |
60624 KB |
Output is correct |
53 |
Correct |
200 ms |
63820 KB |
Output is correct |
54 |
Correct |
374 ms |
85764 KB |
Output is correct |
55 |
Correct |
20 ms |
19124 KB |
Output is correct |
56 |
Correct |
15 ms |
18260 KB |
Output is correct |
57 |
Correct |
78 ms |
39636 KB |
Output is correct |
58 |
Correct |
51 ms |
36276 KB |
Output is correct |
59 |
Correct |
71 ms |
47964 KB |
Output is correct |
60 |
Correct |
264 ms |
79788 KB |
Output is correct |
61 |
Correct |
233 ms |
63052 KB |
Output is correct |
62 |
Correct |
205 ms |
60644 KB |
Output is correct |
63 |
Correct |
311 ms |
60684 KB |
Output is correct |
64 |
Correct |
448 ms |
96328 KB |
Output is correct |
65 |
Correct |
463 ms |
95204 KB |
Output is correct |
66 |
Correct |
325 ms |
78548 KB |
Output is correct |
67 |
Correct |
185 ms |
56728 KB |
Output is correct |
68 |
Correct |
384 ms |
71156 KB |
Output is correct |
69 |
Correct |
375 ms |
74476 KB |
Output is correct |
70 |
Correct |
354 ms |
68732 KB |
Output is correct |