#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;
struct Edge {
int to, w;
};
int k;
vector<vector<Edge>> g;
vector<bool> used;
vector<int> sz;
vector<int> dist;
int ans = inf;
void dfs_sz(int v, int p = -1) {
sz[v] = 1;
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_sz(u, v);
sz[v] += sz[u];
}
}
}
void dfs_ans(int v, int p = -1, int sum = 0, int d = 0) {
if (k - sum < 0) {
return;
}
if (dist[k - sum] < inf) {
ans = min(ans, dist[k - sum] + d);
}
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_ans(u, v, sum + w, d + 1);
}
}
}
void dfs_mp(int v, int p = -1, int sum = 0, int d = 0) {
if (k - sum < 0) {
return;
}
dist[sum] = min(dist[sum], d);
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_mp(u, v, sum + w, d + 1);
}
}
}
void dfs_clear(int v, int p = -1, int sum = 0) {
if (k - sum < 0) {
return;
}
dist[sum] = inf;
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_clear(u, v, sum + w);
}
}
}
int centroid(int v, int p, int n) {
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
if (sz[u] * 2 > n) {
return centroid(u, v, n);
}
}
}
return v;
}
void build(int v) {
dfs_sz(v);
dist[0] = 0;
for (auto [u, w] : g[v]) {
if (!used[u]) {
dfs_ans(u, v, w, 1);
dfs_mp(u, v, w, 1);
}
}
dfs_clear(v);
used[v] = 1;
for (auto [u, w] : g[v]) {
if (!used[u]) {
build(centroid(u, v, sz[u]));
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
g.resize(N);
for (int i = 0; i < N - 1; ++i) {
int u = H[i][0], v = H[i][1], w = L[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
used.resize(N);
sz.resize(N);
dist.resize(1e6 + 5, inf);
dfs_sz(0);
build(centroid(0, -1, sz[0]));
if (ans == inf) {
return -1;
} else {
return ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4388 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
2 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4184 KB |
Output is correct |
14 |
Correct |
2 ms |
4324 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
2 ms |
4148 KB |
Output is correct |
18 |
Correct |
2 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4388 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
2 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4184 KB |
Output is correct |
14 |
Correct |
2 ms |
4324 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
2 ms |
4148 KB |
Output is correct |
18 |
Correct |
2 ms |
4184 KB |
Output is correct |
19 |
Correct |
2 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
4188 KB |
Output is correct |
21 |
Correct |
3 ms |
4444 KB |
Output is correct |
22 |
Correct |
3 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
3 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
5 ms |
4448 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
3 ms |
4444 KB |
Output is correct |
31 |
Correct |
3 ms |
4296 KB |
Output is correct |
32 |
Correct |
3 ms |
4444 KB |
Output is correct |
33 |
Correct |
3 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4444 KB |
Output is correct |
35 |
Correct |
2 ms |
4468 KB |
Output is correct |
36 |
Correct |
2 ms |
4444 KB |
Output is correct |
37 |
Correct |
2 ms |
4284 KB |
Output is correct |
38 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4388 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
2 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4184 KB |
Output is correct |
14 |
Correct |
2 ms |
4324 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
2 ms |
4148 KB |
Output is correct |
18 |
Correct |
2 ms |
4184 KB |
Output is correct |
19 |
Correct |
103 ms |
13064 KB |
Output is correct |
20 |
Correct |
89 ms |
13252 KB |
Output is correct |
21 |
Correct |
100 ms |
13136 KB |
Output is correct |
22 |
Correct |
89 ms |
13236 KB |
Output is correct |
23 |
Correct |
53 ms |
13404 KB |
Output is correct |
24 |
Correct |
37 ms |
13392 KB |
Output is correct |
25 |
Correct |
93 ms |
15956 KB |
Output is correct |
26 |
Correct |
74 ms |
18892 KB |
Output is correct |
27 |
Correct |
104 ms |
22488 KB |
Output is correct |
28 |
Correct |
152 ms |
33872 KB |
Output is correct |
29 |
Correct |
162 ms |
33104 KB |
Output is correct |
30 |
Correct |
108 ms |
22608 KB |
Output is correct |
31 |
Correct |
108 ms |
22608 KB |
Output is correct |
32 |
Correct |
126 ms |
22612 KB |
Output is correct |
33 |
Correct |
144 ms |
21328 KB |
Output is correct |
34 |
Correct |
117 ms |
22352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4388 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4188 KB |
Output is correct |
12 |
Correct |
2 ms |
4188 KB |
Output is correct |
13 |
Correct |
2 ms |
4184 KB |
Output is correct |
14 |
Correct |
2 ms |
4324 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
2 ms |
4148 KB |
Output is correct |
18 |
Correct |
2 ms |
4184 KB |
Output is correct |
19 |
Correct |
2 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
4188 KB |
Output is correct |
21 |
Correct |
3 ms |
4444 KB |
Output is correct |
22 |
Correct |
3 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
3 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
5 ms |
4448 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
3 ms |
4444 KB |
Output is correct |
31 |
Correct |
3 ms |
4296 KB |
Output is correct |
32 |
Correct |
3 ms |
4444 KB |
Output is correct |
33 |
Correct |
3 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4444 KB |
Output is correct |
35 |
Correct |
2 ms |
4468 KB |
Output is correct |
36 |
Correct |
2 ms |
4444 KB |
Output is correct |
37 |
Correct |
2 ms |
4284 KB |
Output is correct |
38 |
Correct |
2 ms |
4444 KB |
Output is correct |
39 |
Correct |
103 ms |
13064 KB |
Output is correct |
40 |
Correct |
89 ms |
13252 KB |
Output is correct |
41 |
Correct |
100 ms |
13136 KB |
Output is correct |
42 |
Correct |
89 ms |
13236 KB |
Output is correct |
43 |
Correct |
53 ms |
13404 KB |
Output is correct |
44 |
Correct |
37 ms |
13392 KB |
Output is correct |
45 |
Correct |
93 ms |
15956 KB |
Output is correct |
46 |
Correct |
74 ms |
18892 KB |
Output is correct |
47 |
Correct |
104 ms |
22488 KB |
Output is correct |
48 |
Correct |
152 ms |
33872 KB |
Output is correct |
49 |
Correct |
162 ms |
33104 KB |
Output is correct |
50 |
Correct |
108 ms |
22608 KB |
Output is correct |
51 |
Correct |
108 ms |
22608 KB |
Output is correct |
52 |
Correct |
126 ms |
22612 KB |
Output is correct |
53 |
Correct |
144 ms |
21328 KB |
Output is correct |
54 |
Correct |
117 ms |
22352 KB |
Output is correct |
55 |
Correct |
8 ms |
5208 KB |
Output is correct |
56 |
Correct |
9 ms |
5256 KB |
Output is correct |
57 |
Correct |
53 ms |
13404 KB |
Output is correct |
58 |
Correct |
25 ms |
13256 KB |
Output is correct |
59 |
Correct |
73 ms |
18884 KB |
Output is correct |
60 |
Correct |
297 ms |
33204 KB |
Output is correct |
61 |
Correct |
116 ms |
22612 KB |
Output is correct |
62 |
Correct |
128 ms |
22608 KB |
Output is correct |
63 |
Correct |
166 ms |
22612 KB |
Output is correct |
64 |
Correct |
306 ms |
22972 KB |
Output is correct |
65 |
Correct |
125 ms |
23316 KB |
Output is correct |
66 |
Correct |
249 ms |
31056 KB |
Output is correct |
67 |
Correct |
64 ms |
23236 KB |
Output is correct |
68 |
Correct |
162 ms |
23376 KB |
Output is correct |
69 |
Correct |
149 ms |
23560 KB |
Output is correct |
70 |
Correct |
140 ms |
22444 KB |
Output is correct |