#include <iostream>
#include <vector>
#include <cstring>
const int32_t MAX_N = 2e5;
const int32_t MAX_K = 1e6;
#ifdef LOCAL
#include "grader.cpp"
#endif
#ifndef LOCAL
#include "race.h"
#endif
struct Vertex {
bool solved;
int32_t subtree;
std::vector< std::pair< int32_t, int32_t > > v;
};
int32_t k, ans = -1, dists[MAX_K + 5];
Vertex g[MAX_N + 5];
void dfs_subtree(int32_t u, int32_t par) {
g[u].subtree = 1;
for(auto &e : g[u].v) {
auto v = e.first;
if(!g[v].solved && v != par) {
dfs_subtree(v, u);
g[u].subtree += g[v].subtree;
}
}
}
int32_t dfs_centroid(int32_t u, int32_t par, int32_t subtree) {
for(auto &e : g[u].v) {
auto v = e.first;
if(!g[v].solved && v != par && g[v].subtree > subtree / 2) {
return dfs_centroid(v, u, subtree);
}
}
return u;
}
void dfs_ans(int32_t u, int32_t par, int32_t d, int32_t cntEdges) {
if(d > k) {
return;
}
if(dists[k - d] != -1) {
if(ans == -1 || ans > cntEdges + dists[k - d]) {
ans = cntEdges + dists[k - d];
}
}
for(auto &e : g[u].v) {
auto v = e.first;
auto w = e.second;
if(!g[v].solved && v != par) {
dfs_ans(v, u, d + w, cntEdges + 1);
}
}
}
void dfs_add(int32_t u, int32_t par, int32_t d, int32_t cntEdges) {
if(d > k) {
return;
}
if(dists[d] == -1 || dists[d] > cntEdges) {
dists[d] = cntEdges;
}
for(auto &e : g[u].v) {
auto v = e.first;
auto w = e.second;
if(!g[v].solved && v != par) {
dfs_add(v, u, d + w, cntEdges + 1);
}
}
}
void dfs_clear(int32_t u, int32_t par, int32_t d) {
if(d > k) {
return;
}
dists[d] = -1;
for(auto &e : g[u].v) {
auto v = e.first;
auto w = e.second;
if(!g[v].solved && v != par) {
dfs_clear(v, u, d + w);
}
}
}
void solve(int32_t u) {
dfs_subtree(u, -1);
int32_t centroid = dfs_centroid(u, -1, g[u].subtree);
dists[0] = 0;
for(auto &e : g[centroid].v) {
auto v = e.first;
if(!g[v].solved) {
dfs_ans(v, centroid, e.second, 1);
dfs_add(v, centroid, e.second, 1);
}
}
dfs_clear(centroid, -1, 0);
g[centroid].solved = true;
for(auto &e : g[centroid].v) {
auto v = e.first;
if(!g[v].solved) {
solve(v);
}
}
}
int32_t best_path(int32_t n, int32_t _k, int32_t h[][2], int32_t l[]) {
k = _k;
for(int32_t i = 0; i < n - 1; i++) {
g[h[i][0]].v.push_back({ h[i][1], l[i] });
g[h[i][1]].v.push_back({ h[i][0], l[i] });
}
memset(dists, -1, sizeof(dists));
solve(1);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10444 KB |
Output is correct |
2 |
Correct |
6 ms |
10444 KB |
Output is correct |
3 |
Correct |
6 ms |
10444 KB |
Output is correct |
4 |
Correct |
6 ms |
10444 KB |
Output is correct |
5 |
Correct |
6 ms |
10444 KB |
Output is correct |
6 |
Correct |
6 ms |
10444 KB |
Output is correct |
7 |
Correct |
6 ms |
10444 KB |
Output is correct |
8 |
Correct |
6 ms |
10444 KB |
Output is correct |
9 |
Correct |
6 ms |
10444 KB |
Output is correct |
10 |
Correct |
6 ms |
10444 KB |
Output is correct |
11 |
Correct |
6 ms |
10444 KB |
Output is correct |
12 |
Correct |
6 ms |
10408 KB |
Output is correct |
13 |
Correct |
6 ms |
10444 KB |
Output is correct |
14 |
Correct |
6 ms |
10444 KB |
Output is correct |
15 |
Correct |
6 ms |
10444 KB |
Output is correct |
16 |
Correct |
6 ms |
10400 KB |
Output is correct |
17 |
Correct |
6 ms |
10444 KB |
Output is correct |
18 |
Correct |
6 ms |
10444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10444 KB |
Output is correct |
2 |
Correct |
6 ms |
10444 KB |
Output is correct |
3 |
Correct |
6 ms |
10444 KB |
Output is correct |
4 |
Correct |
6 ms |
10444 KB |
Output is correct |
5 |
Correct |
6 ms |
10444 KB |
Output is correct |
6 |
Correct |
6 ms |
10444 KB |
Output is correct |
7 |
Correct |
6 ms |
10444 KB |
Output is correct |
8 |
Correct |
6 ms |
10444 KB |
Output is correct |
9 |
Correct |
6 ms |
10444 KB |
Output is correct |
10 |
Correct |
6 ms |
10444 KB |
Output is correct |
11 |
Correct |
6 ms |
10444 KB |
Output is correct |
12 |
Correct |
6 ms |
10408 KB |
Output is correct |
13 |
Correct |
6 ms |
10444 KB |
Output is correct |
14 |
Correct |
6 ms |
10444 KB |
Output is correct |
15 |
Correct |
6 ms |
10444 KB |
Output is correct |
16 |
Correct |
6 ms |
10400 KB |
Output is correct |
17 |
Correct |
6 ms |
10444 KB |
Output is correct |
18 |
Correct |
6 ms |
10444 KB |
Output is correct |
19 |
Correct |
6 ms |
10392 KB |
Output is correct |
20 |
Correct |
6 ms |
10444 KB |
Output is correct |
21 |
Correct |
6 ms |
10444 KB |
Output is correct |
22 |
Correct |
6 ms |
10444 KB |
Output is correct |
23 |
Correct |
6 ms |
10532 KB |
Output is correct |
24 |
Correct |
7 ms |
10444 KB |
Output is correct |
25 |
Correct |
7 ms |
10552 KB |
Output is correct |
26 |
Correct |
7 ms |
10444 KB |
Output is correct |
27 |
Correct |
7 ms |
10444 KB |
Output is correct |
28 |
Correct |
7 ms |
10548 KB |
Output is correct |
29 |
Correct |
7 ms |
10444 KB |
Output is correct |
30 |
Correct |
7 ms |
10444 KB |
Output is correct |
31 |
Correct |
7 ms |
10544 KB |
Output is correct |
32 |
Correct |
7 ms |
10444 KB |
Output is correct |
33 |
Correct |
7 ms |
10444 KB |
Output is correct |
34 |
Correct |
6 ms |
10468 KB |
Output is correct |
35 |
Correct |
6 ms |
10444 KB |
Output is correct |
36 |
Correct |
6 ms |
10444 KB |
Output is correct |
37 |
Correct |
7 ms |
10444 KB |
Output is correct |
38 |
Correct |
7 ms |
10500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10444 KB |
Output is correct |
2 |
Correct |
6 ms |
10444 KB |
Output is correct |
3 |
Correct |
6 ms |
10444 KB |
Output is correct |
4 |
Correct |
6 ms |
10444 KB |
Output is correct |
5 |
Correct |
6 ms |
10444 KB |
Output is correct |
6 |
Correct |
6 ms |
10444 KB |
Output is correct |
7 |
Correct |
6 ms |
10444 KB |
Output is correct |
8 |
Correct |
6 ms |
10444 KB |
Output is correct |
9 |
Correct |
6 ms |
10444 KB |
Output is correct |
10 |
Correct |
6 ms |
10444 KB |
Output is correct |
11 |
Correct |
6 ms |
10444 KB |
Output is correct |
12 |
Correct |
6 ms |
10408 KB |
Output is correct |
13 |
Correct |
6 ms |
10444 KB |
Output is correct |
14 |
Correct |
6 ms |
10444 KB |
Output is correct |
15 |
Correct |
6 ms |
10444 KB |
Output is correct |
16 |
Correct |
6 ms |
10400 KB |
Output is correct |
17 |
Correct |
6 ms |
10444 KB |
Output is correct |
18 |
Correct |
6 ms |
10444 KB |
Output is correct |
19 |
Correct |
146 ms |
15292 KB |
Output is correct |
20 |
Correct |
138 ms |
15556 KB |
Output is correct |
21 |
Correct |
187 ms |
15744 KB |
Output is correct |
22 |
Correct |
174 ms |
15640 KB |
Output is correct |
23 |
Correct |
99 ms |
15912 KB |
Output is correct |
24 |
Correct |
68 ms |
15816 KB |
Output is correct |
25 |
Correct |
149 ms |
22084 KB |
Output is correct |
26 |
Correct |
119 ms |
23944 KB |
Output is correct |
27 |
Correct |
195 ms |
23276 KB |
Output is correct |
28 |
Correct |
268 ms |
36688 KB |
Output is correct |
29 |
Correct |
290 ms |
31368 KB |
Output is correct |
30 |
Correct |
203 ms |
23200 KB |
Output is correct |
31 |
Correct |
211 ms |
23280 KB |
Output is correct |
32 |
Correct |
219 ms |
23280 KB |
Output is correct |
33 |
Correct |
244 ms |
22132 KB |
Output is correct |
34 |
Correct |
198 ms |
23108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10444 KB |
Output is correct |
2 |
Correct |
6 ms |
10444 KB |
Output is correct |
3 |
Correct |
6 ms |
10444 KB |
Output is correct |
4 |
Correct |
6 ms |
10444 KB |
Output is correct |
5 |
Correct |
6 ms |
10444 KB |
Output is correct |
6 |
Correct |
6 ms |
10444 KB |
Output is correct |
7 |
Correct |
6 ms |
10444 KB |
Output is correct |
8 |
Correct |
6 ms |
10444 KB |
Output is correct |
9 |
Correct |
6 ms |
10444 KB |
Output is correct |
10 |
Correct |
6 ms |
10444 KB |
Output is correct |
11 |
Correct |
6 ms |
10444 KB |
Output is correct |
12 |
Correct |
6 ms |
10408 KB |
Output is correct |
13 |
Correct |
6 ms |
10444 KB |
Output is correct |
14 |
Correct |
6 ms |
10444 KB |
Output is correct |
15 |
Correct |
6 ms |
10444 KB |
Output is correct |
16 |
Correct |
6 ms |
10400 KB |
Output is correct |
17 |
Correct |
6 ms |
10444 KB |
Output is correct |
18 |
Correct |
6 ms |
10444 KB |
Output is correct |
19 |
Correct |
6 ms |
10392 KB |
Output is correct |
20 |
Correct |
6 ms |
10444 KB |
Output is correct |
21 |
Correct |
6 ms |
10444 KB |
Output is correct |
22 |
Correct |
6 ms |
10444 KB |
Output is correct |
23 |
Correct |
6 ms |
10532 KB |
Output is correct |
24 |
Correct |
7 ms |
10444 KB |
Output is correct |
25 |
Correct |
7 ms |
10552 KB |
Output is correct |
26 |
Correct |
7 ms |
10444 KB |
Output is correct |
27 |
Correct |
7 ms |
10444 KB |
Output is correct |
28 |
Correct |
7 ms |
10548 KB |
Output is correct |
29 |
Correct |
7 ms |
10444 KB |
Output is correct |
30 |
Correct |
7 ms |
10444 KB |
Output is correct |
31 |
Correct |
7 ms |
10544 KB |
Output is correct |
32 |
Correct |
7 ms |
10444 KB |
Output is correct |
33 |
Correct |
7 ms |
10444 KB |
Output is correct |
34 |
Correct |
6 ms |
10468 KB |
Output is correct |
35 |
Correct |
6 ms |
10444 KB |
Output is correct |
36 |
Correct |
6 ms |
10444 KB |
Output is correct |
37 |
Correct |
7 ms |
10444 KB |
Output is correct |
38 |
Correct |
7 ms |
10500 KB |
Output is correct |
39 |
Correct |
146 ms |
15292 KB |
Output is correct |
40 |
Correct |
138 ms |
15556 KB |
Output is correct |
41 |
Correct |
187 ms |
15744 KB |
Output is correct |
42 |
Correct |
174 ms |
15640 KB |
Output is correct |
43 |
Correct |
99 ms |
15912 KB |
Output is correct |
44 |
Correct |
68 ms |
15816 KB |
Output is correct |
45 |
Correct |
149 ms |
22084 KB |
Output is correct |
46 |
Correct |
119 ms |
23944 KB |
Output is correct |
47 |
Correct |
195 ms |
23276 KB |
Output is correct |
48 |
Correct |
268 ms |
36688 KB |
Output is correct |
49 |
Correct |
290 ms |
31368 KB |
Output is correct |
50 |
Correct |
203 ms |
23200 KB |
Output is correct |
51 |
Correct |
211 ms |
23280 KB |
Output is correct |
52 |
Correct |
219 ms |
23280 KB |
Output is correct |
53 |
Correct |
244 ms |
22132 KB |
Output is correct |
54 |
Correct |
198 ms |
23108 KB |
Output is correct |
55 |
Correct |
15 ms |
11084 KB |
Output is correct |
56 |
Correct |
16 ms |
11084 KB |
Output is correct |
57 |
Correct |
98 ms |
16876 KB |
Output is correct |
58 |
Correct |
43 ms |
16620 KB |
Output is correct |
59 |
Correct |
145 ms |
23880 KB |
Output is correct |
60 |
Correct |
565 ms |
30168 KB |
Output is correct |
61 |
Correct |
233 ms |
23448 KB |
Output is correct |
62 |
Correct |
252 ms |
23256 KB |
Output is correct |
63 |
Correct |
320 ms |
23376 KB |
Output is correct |
64 |
Correct |
643 ms |
23652 KB |
Output is correct |
65 |
Correct |
262 ms |
24168 KB |
Output is correct |
66 |
Correct |
441 ms |
35528 KB |
Output is correct |
67 |
Correct |
169 ms |
24008 KB |
Output is correct |
68 |
Correct |
348 ms |
24140 KB |
Output is correct |
69 |
Correct |
305 ms |
24504 KB |
Output is correct |
70 |
Correct |
292 ms |
23588 KB |
Output is correct |