#include "race.h"
#include "bits/stdc++.h"
using namespace std;
const int MXN = 200005;
const int MXD = 2000006;
int k;
int sz[MXN];
int dep[MXN];
long long dis[MXN];
vector<int> vec[MXN];
map<long long, multiset<int>> haveDis;
vector<pair<int, int>> g[MXN];
int ans;
int Dfs (int v, int p) {
sz[v] = 1;
for (auto [u, w] : g[v]) {
if (u != p) {
dis[u] = dis[v] + w;
dep[u] = dep[v] + 1;
sz[v] += Dfs(u, v);
}
}
return sz[v];
}
void Dfs2 (int v, int p, bool keep) {
int big = -1;
for (auto [u, w] : g[v]) {
if (u != p && (big == -1 || sz[u] > sz[big])) {
big = u;
}
}
for (auto [u, w] : g[v]) {
if (u != p && u != big) {
Dfs2(u, v, false);
}
}
if (big != -1) {
Dfs2(big, v, true);
swap(vec[v], vec[big]);
}
vec[v].push_back(v);
for (auto [u, w] : g[v]) {
if (u == p || u == big) {
continue;
}
for (int ch : vec[u]) {
long long needed = k - dis[ch] + 2 * dis[v];
if (needed >= 0 && !haveDis[needed].empty()) {
ans = min(ans, dep[ch] + *haveDis[needed].begin() - 2 * dep[v]);
}
}
for (int ch : vec[u]) {
vec[v].push_back(ch);
haveDis[dis[ch]].insert(dep[ch]);
}
}
long long needed = k + dis[v];
if (needed >= 0 && !haveDis[needed].empty()) {
ans = min(ans, *haveDis[needed].begin() - dep[v]);
}
if (!keep) {
for (int u : vec[v]) {
if (u != v) {
haveDis[dis[u]].erase(haveDis[dis[u]].find(dep[u]));
}
}
}
else {
haveDis[dis[v]].insert(dep[v]);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
for (int i = 0; i < N - 1; ++i) {
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
ans = N + 1;
Dfs(0, 0);
Dfs2(0, 0, true);
return (ans == N + 1 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9764 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9696 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9764 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9696 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9648 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
6 ms |
10068 KB |
Output is correct |
23 |
Correct |
6 ms |
10196 KB |
Output is correct |
24 |
Correct |
6 ms |
10236 KB |
Output is correct |
25 |
Correct |
6 ms |
10196 KB |
Output is correct |
26 |
Correct |
6 ms |
10196 KB |
Output is correct |
27 |
Correct |
5 ms |
9812 KB |
Output is correct |
28 |
Correct |
7 ms |
10196 KB |
Output is correct |
29 |
Correct |
7 ms |
10196 KB |
Output is correct |
30 |
Correct |
7 ms |
10188 KB |
Output is correct |
31 |
Correct |
8 ms |
10236 KB |
Output is correct |
32 |
Correct |
7 ms |
10196 KB |
Output is correct |
33 |
Correct |
6 ms |
10200 KB |
Output is correct |
34 |
Correct |
6 ms |
10092 KB |
Output is correct |
35 |
Correct |
5 ms |
10108 KB |
Output is correct |
36 |
Correct |
5 ms |
10068 KB |
Output is correct |
37 |
Correct |
6 ms |
10040 KB |
Output is correct |
38 |
Correct |
6 ms |
10068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9764 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9696 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9648 KB |
Output is correct |
19 |
Correct |
198 ms |
24508 KB |
Output is correct |
20 |
Correct |
189 ms |
24484 KB |
Output is correct |
21 |
Correct |
196 ms |
24600 KB |
Output is correct |
22 |
Correct |
184 ms |
24536 KB |
Output is correct |
23 |
Correct |
266 ms |
25380 KB |
Output is correct |
24 |
Correct |
176 ms |
25648 KB |
Output is correct |
25 |
Correct |
81 ms |
30460 KB |
Output is correct |
26 |
Correct |
55 ms |
37892 KB |
Output is correct |
27 |
Correct |
201 ms |
37336 KB |
Output is correct |
28 |
Correct |
207 ms |
103360 KB |
Output is correct |
29 |
Correct |
239 ms |
100808 KB |
Output is correct |
30 |
Correct |
190 ms |
37348 KB |
Output is correct |
31 |
Correct |
189 ms |
37316 KB |
Output is correct |
32 |
Correct |
277 ms |
38032 KB |
Output is correct |
33 |
Correct |
306 ms |
35236 KB |
Output is correct |
34 |
Correct |
1077 ms |
115124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9764 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9696 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9648 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
6 ms |
10068 KB |
Output is correct |
23 |
Correct |
6 ms |
10196 KB |
Output is correct |
24 |
Correct |
6 ms |
10236 KB |
Output is correct |
25 |
Correct |
6 ms |
10196 KB |
Output is correct |
26 |
Correct |
6 ms |
10196 KB |
Output is correct |
27 |
Correct |
5 ms |
9812 KB |
Output is correct |
28 |
Correct |
7 ms |
10196 KB |
Output is correct |
29 |
Correct |
7 ms |
10196 KB |
Output is correct |
30 |
Correct |
7 ms |
10188 KB |
Output is correct |
31 |
Correct |
8 ms |
10236 KB |
Output is correct |
32 |
Correct |
7 ms |
10196 KB |
Output is correct |
33 |
Correct |
6 ms |
10200 KB |
Output is correct |
34 |
Correct |
6 ms |
10092 KB |
Output is correct |
35 |
Correct |
5 ms |
10108 KB |
Output is correct |
36 |
Correct |
5 ms |
10068 KB |
Output is correct |
37 |
Correct |
6 ms |
10040 KB |
Output is correct |
38 |
Correct |
6 ms |
10068 KB |
Output is correct |
39 |
Correct |
198 ms |
24508 KB |
Output is correct |
40 |
Correct |
189 ms |
24484 KB |
Output is correct |
41 |
Correct |
196 ms |
24600 KB |
Output is correct |
42 |
Correct |
184 ms |
24536 KB |
Output is correct |
43 |
Correct |
266 ms |
25380 KB |
Output is correct |
44 |
Correct |
176 ms |
25648 KB |
Output is correct |
45 |
Correct |
81 ms |
30460 KB |
Output is correct |
46 |
Correct |
55 ms |
37892 KB |
Output is correct |
47 |
Correct |
201 ms |
37336 KB |
Output is correct |
48 |
Correct |
207 ms |
103360 KB |
Output is correct |
49 |
Correct |
239 ms |
100808 KB |
Output is correct |
50 |
Correct |
190 ms |
37348 KB |
Output is correct |
51 |
Correct |
189 ms |
37316 KB |
Output is correct |
52 |
Correct |
277 ms |
38032 KB |
Output is correct |
53 |
Correct |
306 ms |
35236 KB |
Output is correct |
54 |
Correct |
1077 ms |
115124 KB |
Output is correct |
55 |
Correct |
25 ms |
12536 KB |
Output is correct |
56 |
Correct |
17 ms |
11220 KB |
Output is correct |
57 |
Correct |
191 ms |
26700 KB |
Output is correct |
58 |
Correct |
66 ms |
25600 KB |
Output is correct |
59 |
Correct |
93 ms |
57668 KB |
Output is correct |
60 |
Correct |
272 ms |
104840 KB |
Output is correct |
61 |
Correct |
240 ms |
43572 KB |
Output is correct |
62 |
Correct |
221 ms |
41012 KB |
Output is correct |
63 |
Correct |
262 ms |
41024 KB |
Output is correct |
64 |
Correct |
1480 ms |
85052 KB |
Output is correct |
65 |
Correct |
2094 ms |
139584 KB |
Output is correct |
66 |
Correct |
250 ms |
98736 KB |
Output is correct |
67 |
Correct |
229 ms |
42996 KB |
Output is correct |
68 |
Correct |
752 ms |
95896 KB |
Output is correct |
69 |
Correct |
786 ms |
98408 KB |
Output is correct |
70 |
Correct |
785 ms |
93352 KB |
Output is correct |