#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using ll = long long;
#define f first
#define s second
#define nme(x) #x << " : " << x << ' '
const int NN = 200000;
int n, k, sz[NN];
ll level[NN], ANS = INT_MAX, dist[NN];
map<ll, ll> min_edges;
vector<int> nodes[NN];
vector<pair<int, int>> adj[NN];
void dfs_pre(int x, int pr, int lvl, ll sm) {
sz[x] = 1, level[x] = lvl, dist[x] = sm;
for(auto e : adj[x]) if(e.f != pr) {
dfs_pre(e.f, x, lvl + 1, sm + e.s);
sz[x] += sz[e.f];
}
}
void dfs(int x, int pr, bool keep) {
int mx = 0, big_child = -1;
for(auto e : adj[x])
if(e.f != pr && sz[e.f] > mx) {
mx = sz[e.f];
big_child = e.f;
}
if(big_child == -1) {
nodes[x].push_back(x);
if(keep)
min_edges[dist[x]] = level[x];
return;
}
for(auto e : adj[x])
if(e.f != pr && e.f != big_child)
dfs(e.f, x, false);
dfs(big_child, x, true);
swap(nodes[x], nodes[big_child]);
nodes[x].push_back(x);
if(min_edges.find(dist[x]) == min_edges.end()) min_edges[dist[x]] = level[x];
else min_edges[dist[x]] = min(min_edges[dist[x]], level[x]);
if(min_edges.find(dist[x] + k) != min_edges.end()) {
ANS = min(ANS, min_edges[dist[x] + k] - level[x]);
// if(min_edges[dist[x] + k] - level[x] == 2) {
// cerr << nme(x) << '\n';
// }
}
for(auto e : adj[x])
if(e.f != pr && e.f != big_child) {
for(auto nd : nodes[e.f]) {
ll d = dist[nd] - dist[x];
int lvl = level[nd] - level[x];
if(d <= k && min_edges.find(k - d + dist[x]) != min_edges.end()) {
ANS = min(ANS, min_edges[k - d + dist[x]] - level[x] + lvl);
// if(min_edges[k - d + dist[x]] - level[x] + lvl == 2) {
// cerr << nme(x) << '\n';
// if(x == 0) {
// cerr << nme(d) << '\n';
// cerr << min_edges[k - d + dist[x]] << '\n';
// cerr << "yes";
// }
// }
}
}
for(auto nd : nodes[e.f]) {
if(min_edges.find(dist[nd]) == min_edges.end()) min_edges[dist[nd]] = level[nd];
else min_edges[dist[nd]] = min(min_edges[dist[nd]], level[nd]);
nodes[x].push_back(nd);
}
}
// if(ANS == 2) {
// cerr << nme(x) << '\n';
// }
// if(x == 6) {
// cerr << nme(big_child) << '\n';
// cerr << nme(ANS) << '\n';
// }
if(!keep)
min_edges.clear();
}
int best_path(int _N, int K, int H[][2], int L[]) {
n = _N, k = K;
for(int i = 0;i < n - 1;i ++) {
adj[H[i][0]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
dfs_pre(0, -1, 0, 0);
dfs(0, -1, true);
return (ANS == INT_MAX ? -1 : ANS);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
8 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
6 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
6 ms |
9708 KB |
Output is correct |
8 |
Correct |
8 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9708 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
8 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
6 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
6 ms |
9708 KB |
Output is correct |
8 |
Correct |
8 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9708 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
8 ms |
9836 KB |
Output is correct |
22 |
Correct |
9 ms |
9836 KB |
Output is correct |
23 |
Correct |
8 ms |
9836 KB |
Output is correct |
24 |
Correct |
8 ms |
9836 KB |
Output is correct |
25 |
Correct |
8 ms |
9836 KB |
Output is correct |
26 |
Correct |
8 ms |
9836 KB |
Output is correct |
27 |
Correct |
7 ms |
9836 KB |
Output is correct |
28 |
Correct |
9 ms |
9836 KB |
Output is correct |
29 |
Correct |
8 ms |
9836 KB |
Output is correct |
30 |
Correct |
7 ms |
9836 KB |
Output is correct |
31 |
Correct |
7 ms |
9836 KB |
Output is correct |
32 |
Correct |
7 ms |
9836 KB |
Output is correct |
33 |
Correct |
8 ms |
9836 KB |
Output is correct |
34 |
Correct |
8 ms |
9964 KB |
Output is correct |
35 |
Correct |
7 ms |
9964 KB |
Output is correct |
36 |
Correct |
7 ms |
9964 KB |
Output is correct |
37 |
Correct |
7 ms |
9836 KB |
Output is correct |
38 |
Correct |
8 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
8 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
6 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
6 ms |
9708 KB |
Output is correct |
8 |
Correct |
8 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9708 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
194 ms |
20464 KB |
Output is correct |
20 |
Correct |
187 ms |
20456 KB |
Output is correct |
21 |
Correct |
195 ms |
20592 KB |
Output is correct |
22 |
Correct |
207 ms |
20408 KB |
Output is correct |
23 |
Correct |
232 ms |
21380 KB |
Output is correct |
24 |
Correct |
187 ms |
21736 KB |
Output is correct |
25 |
Correct |
122 ms |
25320 KB |
Output is correct |
26 |
Correct |
68 ms |
32244 KB |
Output is correct |
27 |
Correct |
250 ms |
29284 KB |
Output is correct |
28 |
Correct |
348 ms |
66912 KB |
Output is correct |
29 |
Correct |
341 ms |
64480 KB |
Output is correct |
30 |
Correct |
240 ms |
29028 KB |
Output is correct |
31 |
Correct |
246 ms |
29156 KB |
Output is correct |
32 |
Correct |
345 ms |
29028 KB |
Output is correct |
33 |
Correct |
336 ms |
26668 KB |
Output is correct |
34 |
Correct |
462 ms |
38392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9856 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
8 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
6 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
6 ms |
9708 KB |
Output is correct |
8 |
Correct |
8 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9708 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
8 ms |
9836 KB |
Output is correct |
22 |
Correct |
9 ms |
9836 KB |
Output is correct |
23 |
Correct |
8 ms |
9836 KB |
Output is correct |
24 |
Correct |
8 ms |
9836 KB |
Output is correct |
25 |
Correct |
8 ms |
9836 KB |
Output is correct |
26 |
Correct |
8 ms |
9836 KB |
Output is correct |
27 |
Correct |
7 ms |
9836 KB |
Output is correct |
28 |
Correct |
9 ms |
9836 KB |
Output is correct |
29 |
Correct |
8 ms |
9836 KB |
Output is correct |
30 |
Correct |
7 ms |
9836 KB |
Output is correct |
31 |
Correct |
7 ms |
9836 KB |
Output is correct |
32 |
Correct |
7 ms |
9836 KB |
Output is correct |
33 |
Correct |
8 ms |
9836 KB |
Output is correct |
34 |
Correct |
8 ms |
9964 KB |
Output is correct |
35 |
Correct |
7 ms |
9964 KB |
Output is correct |
36 |
Correct |
7 ms |
9964 KB |
Output is correct |
37 |
Correct |
7 ms |
9836 KB |
Output is correct |
38 |
Correct |
8 ms |
9836 KB |
Output is correct |
39 |
Correct |
194 ms |
20464 KB |
Output is correct |
40 |
Correct |
187 ms |
20456 KB |
Output is correct |
41 |
Correct |
195 ms |
20592 KB |
Output is correct |
42 |
Correct |
207 ms |
20408 KB |
Output is correct |
43 |
Correct |
232 ms |
21380 KB |
Output is correct |
44 |
Correct |
187 ms |
21736 KB |
Output is correct |
45 |
Correct |
122 ms |
25320 KB |
Output is correct |
46 |
Correct |
68 ms |
32244 KB |
Output is correct |
47 |
Correct |
250 ms |
29284 KB |
Output is correct |
48 |
Correct |
348 ms |
66912 KB |
Output is correct |
49 |
Correct |
341 ms |
64480 KB |
Output is correct |
50 |
Correct |
240 ms |
29028 KB |
Output is correct |
51 |
Correct |
246 ms |
29156 KB |
Output is correct |
52 |
Correct |
345 ms |
29028 KB |
Output is correct |
53 |
Correct |
336 ms |
26668 KB |
Output is correct |
54 |
Correct |
462 ms |
38392 KB |
Output is correct |
55 |
Correct |
22 ms |
11116 KB |
Output is correct |
56 |
Correct |
16 ms |
10860 KB |
Output is correct |
57 |
Correct |
118 ms |
21484 KB |
Output is correct |
58 |
Correct |
75 ms |
21992 KB |
Output is correct |
59 |
Correct |
105 ms |
39652 KB |
Output is correct |
60 |
Correct |
336 ms |
68320 KB |
Output is correct |
61 |
Correct |
318 ms |
33764 KB |
Output is correct |
62 |
Correct |
249 ms |
32052 KB |
Output is correct |
63 |
Correct |
341 ms |
32100 KB |
Output is correct |
64 |
Correct |
671 ms |
40932 KB |
Output is correct |
65 |
Correct |
631 ms |
47204 KB |
Output is correct |
66 |
Correct |
338 ms |
62692 KB |
Output is correct |
67 |
Correct |
261 ms |
35080 KB |
Output is correct |
68 |
Correct |
482 ms |
43876 KB |
Output is correct |
69 |
Correct |
478 ms |
44312 KB |
Output is correct |
70 |
Correct |
456 ms |
42592 KB |
Output is correct |