#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(),(x).end()
#define pb push_back
const int mxN = 300000;
const int ninf = -1e18;
int k;
vector<array<int, 2>> adj[mxN];
set<array<int, 2>> paths[mxN];
array<int, 2> add[mxN];
int ans;
void dfs(int v, int p) {
for(auto[i, w] : adj[v]) {
if(i == p) continue;
dfs(i, v);
add[i][0] += w;
++add[i][1];
if(paths[i].size() > paths[v].size()) {
add[i].swap(add[v]);
paths[i].swap(paths[v]);
}
for(auto[len, edges] : paths[i]) {
int cur = len + add[i][0];
array<int, 2> search = {k - cur - add[v][0], ninf};
auto lb = paths[v].upper_bound(search);
if(lb != paths[v].end() && (*lb)[0] + add[v][0] + cur == k) {
ans = min(ans, edges + add[i][1] + (*lb)[1] + add[v][1]);
}
}
for(auto[len, edges] : paths[i]) {
paths[v].insert({len + add[i][0] - add[v][0], edges + add[i][1] - add[v][1]});
}
paths[i].clear();
}
array<int, 2> find = {k - add[v][0], ninf};
auto lb = paths[v].upper_bound(find);
if(lb != paths[v].end() && (*lb)[0] + add[v][0] == k) {
ans = min(ans, (*lb)[1] + add[v][1]);
}
paths[v].insert({-add[v][0], -add[v][1]});
}
int32_t best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[]) {
k = K;
ans = 1e18;
for(int i = 0; i < N - 1; ++i) {
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
dfs(0, -1);
return ans == 1e18 ? -1 : ans;
}
/*
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, K; cin >> N >> K;
int H[N - 1][2], L[N - 1];
for(int i = 0; i < N - 1; ++i) {
cin >> H[i][0] >> H[i][1] >> L[i];
}
cout << best_path(N, K, H, L) << "\n";
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
15 ms |
21484 KB |
Output is correct |
4 |
Correct |
15 ms |
21484 KB |
Output is correct |
5 |
Correct |
15 ms |
21484 KB |
Output is correct |
6 |
Correct |
18 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21484 KB |
Output is correct |
11 |
Correct |
14 ms |
21484 KB |
Output is correct |
12 |
Correct |
15 ms |
21484 KB |
Output is correct |
13 |
Correct |
16 ms |
21484 KB |
Output is correct |
14 |
Correct |
15 ms |
21516 KB |
Output is correct |
15 |
Correct |
18 ms |
21484 KB |
Output is correct |
16 |
Correct |
16 ms |
21484 KB |
Output is correct |
17 |
Correct |
17 ms |
21484 KB |
Output is correct |
18 |
Correct |
19 ms |
21484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
15 ms |
21484 KB |
Output is correct |
4 |
Correct |
15 ms |
21484 KB |
Output is correct |
5 |
Correct |
15 ms |
21484 KB |
Output is correct |
6 |
Correct |
18 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21484 KB |
Output is correct |
11 |
Correct |
14 ms |
21484 KB |
Output is correct |
12 |
Correct |
15 ms |
21484 KB |
Output is correct |
13 |
Correct |
16 ms |
21484 KB |
Output is correct |
14 |
Correct |
15 ms |
21516 KB |
Output is correct |
15 |
Correct |
18 ms |
21484 KB |
Output is correct |
16 |
Correct |
16 ms |
21484 KB |
Output is correct |
17 |
Correct |
17 ms |
21484 KB |
Output is correct |
18 |
Correct |
19 ms |
21484 KB |
Output is correct |
19 |
Correct |
16 ms |
21484 KB |
Output is correct |
20 |
Correct |
16 ms |
21484 KB |
Output is correct |
21 |
Correct |
21 ms |
21612 KB |
Output is correct |
22 |
Correct |
17 ms |
21612 KB |
Output is correct |
23 |
Correct |
18 ms |
21660 KB |
Output is correct |
24 |
Correct |
18 ms |
21612 KB |
Output is correct |
25 |
Correct |
15 ms |
21612 KB |
Output is correct |
26 |
Correct |
15 ms |
21612 KB |
Output is correct |
27 |
Correct |
21 ms |
21612 KB |
Output is correct |
28 |
Correct |
20 ms |
21612 KB |
Output is correct |
29 |
Correct |
19 ms |
21612 KB |
Output is correct |
30 |
Correct |
22 ms |
21612 KB |
Output is correct |
31 |
Correct |
21 ms |
21612 KB |
Output is correct |
32 |
Correct |
21 ms |
21612 KB |
Output is correct |
33 |
Correct |
15 ms |
21612 KB |
Output is correct |
34 |
Correct |
16 ms |
21612 KB |
Output is correct |
35 |
Correct |
16 ms |
21760 KB |
Output is correct |
36 |
Correct |
14 ms |
21612 KB |
Output is correct |
37 |
Correct |
17 ms |
21612 KB |
Output is correct |
38 |
Correct |
16 ms |
21704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
15 ms |
21484 KB |
Output is correct |
4 |
Correct |
15 ms |
21484 KB |
Output is correct |
5 |
Correct |
15 ms |
21484 KB |
Output is correct |
6 |
Correct |
18 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21484 KB |
Output is correct |
11 |
Correct |
14 ms |
21484 KB |
Output is correct |
12 |
Correct |
15 ms |
21484 KB |
Output is correct |
13 |
Correct |
16 ms |
21484 KB |
Output is correct |
14 |
Correct |
15 ms |
21516 KB |
Output is correct |
15 |
Correct |
18 ms |
21484 KB |
Output is correct |
16 |
Correct |
16 ms |
21484 KB |
Output is correct |
17 |
Correct |
17 ms |
21484 KB |
Output is correct |
18 |
Correct |
19 ms |
21484 KB |
Output is correct |
19 |
Correct |
164 ms |
30276 KB |
Output is correct |
20 |
Correct |
163 ms |
30576 KB |
Output is correct |
21 |
Correct |
150 ms |
30184 KB |
Output is correct |
22 |
Correct |
179 ms |
30188 KB |
Output is correct |
23 |
Correct |
199 ms |
31084 KB |
Output is correct |
24 |
Correct |
137 ms |
29548 KB |
Output is correct |
25 |
Correct |
151 ms |
48492 KB |
Output is correct |
26 |
Correct |
110 ms |
54552 KB |
Output is correct |
27 |
Correct |
255 ms |
41836 KB |
Output is correct |
28 |
Correct |
287 ms |
87020 KB |
Output is correct |
29 |
Correct |
284 ms |
85228 KB |
Output is correct |
30 |
Correct |
253 ms |
42092 KB |
Output is correct |
31 |
Correct |
288 ms |
41780 KB |
Output is correct |
32 |
Correct |
310 ms |
41800 KB |
Output is correct |
33 |
Correct |
246 ms |
38252 KB |
Output is correct |
34 |
Correct |
382 ms |
50924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
15 ms |
21484 KB |
Output is correct |
4 |
Correct |
15 ms |
21484 KB |
Output is correct |
5 |
Correct |
15 ms |
21484 KB |
Output is correct |
6 |
Correct |
18 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21484 KB |
Output is correct |
11 |
Correct |
14 ms |
21484 KB |
Output is correct |
12 |
Correct |
15 ms |
21484 KB |
Output is correct |
13 |
Correct |
16 ms |
21484 KB |
Output is correct |
14 |
Correct |
15 ms |
21516 KB |
Output is correct |
15 |
Correct |
18 ms |
21484 KB |
Output is correct |
16 |
Correct |
16 ms |
21484 KB |
Output is correct |
17 |
Correct |
17 ms |
21484 KB |
Output is correct |
18 |
Correct |
19 ms |
21484 KB |
Output is correct |
19 |
Correct |
16 ms |
21484 KB |
Output is correct |
20 |
Correct |
16 ms |
21484 KB |
Output is correct |
21 |
Correct |
21 ms |
21612 KB |
Output is correct |
22 |
Correct |
17 ms |
21612 KB |
Output is correct |
23 |
Correct |
18 ms |
21660 KB |
Output is correct |
24 |
Correct |
18 ms |
21612 KB |
Output is correct |
25 |
Correct |
15 ms |
21612 KB |
Output is correct |
26 |
Correct |
15 ms |
21612 KB |
Output is correct |
27 |
Correct |
21 ms |
21612 KB |
Output is correct |
28 |
Correct |
20 ms |
21612 KB |
Output is correct |
29 |
Correct |
19 ms |
21612 KB |
Output is correct |
30 |
Correct |
22 ms |
21612 KB |
Output is correct |
31 |
Correct |
21 ms |
21612 KB |
Output is correct |
32 |
Correct |
21 ms |
21612 KB |
Output is correct |
33 |
Correct |
15 ms |
21612 KB |
Output is correct |
34 |
Correct |
16 ms |
21612 KB |
Output is correct |
35 |
Correct |
16 ms |
21760 KB |
Output is correct |
36 |
Correct |
14 ms |
21612 KB |
Output is correct |
37 |
Correct |
17 ms |
21612 KB |
Output is correct |
38 |
Correct |
16 ms |
21704 KB |
Output is correct |
39 |
Correct |
164 ms |
30276 KB |
Output is correct |
40 |
Correct |
163 ms |
30576 KB |
Output is correct |
41 |
Correct |
150 ms |
30184 KB |
Output is correct |
42 |
Correct |
179 ms |
30188 KB |
Output is correct |
43 |
Correct |
199 ms |
31084 KB |
Output is correct |
44 |
Correct |
137 ms |
29548 KB |
Output is correct |
45 |
Correct |
151 ms |
48492 KB |
Output is correct |
46 |
Correct |
110 ms |
54552 KB |
Output is correct |
47 |
Correct |
255 ms |
41836 KB |
Output is correct |
48 |
Correct |
287 ms |
87020 KB |
Output is correct |
49 |
Correct |
284 ms |
85228 KB |
Output is correct |
50 |
Correct |
253 ms |
42092 KB |
Output is correct |
51 |
Correct |
288 ms |
41780 KB |
Output is correct |
52 |
Correct |
310 ms |
41800 KB |
Output is correct |
53 |
Correct |
246 ms |
38252 KB |
Output is correct |
54 |
Correct |
382 ms |
50924 KB |
Output is correct |
55 |
Correct |
30 ms |
23020 KB |
Output is correct |
56 |
Correct |
24 ms |
22380 KB |
Output is correct |
57 |
Correct |
85 ms |
30316 KB |
Output is correct |
58 |
Correct |
69 ms |
29404 KB |
Output is correct |
59 |
Correct |
103 ms |
54508 KB |
Output is correct |
60 |
Correct |
284 ms |
85748 KB |
Output is correct |
61 |
Correct |
298 ms |
45548 KB |
Output is correct |
62 |
Correct |
264 ms |
41580 KB |
Output is correct |
63 |
Correct |
337 ms |
41580 KB |
Output is correct |
64 |
Correct |
519 ms |
49644 KB |
Output is correct |
65 |
Correct |
465 ms |
51564 KB |
Output is correct |
66 |
Correct |
297 ms |
81772 KB |
Output is correct |
67 |
Correct |
191 ms |
37976 KB |
Output is correct |
68 |
Correct |
395 ms |
51052 KB |
Output is correct |
69 |
Correct |
411 ms |
55660 KB |
Output is correct |
70 |
Correct |
373 ms |
49900 KB |
Output is correct |