#include <bits/stdc++.h>
#include <race.h>
using namespace std;
#define task ""
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int INF = 1e9 + 7;
const int MAX = 200020;
int n, k;
vector<pair<int, int>> adj[MAX];
int sz[MAX];
bool rem[MAX];
int dfsSZ(int u, int p) {
sz[u] = 1;
for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
sz[u] += dfsSZ(v, u);
}
return sz[u];
}
int findCentroid(int u, int p, int n) {
for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
if (2 * sz[v] > n)
return findCentroid(v, u, n);
}
return u;
}
int h[MAX];
int dep[MAX];
int res = +INF;
int maxHigh[1000010];
void dfs(int u, int p, bool upd) {
if (dep[u] > k) return;
if (!upd) res = min(res, h[u] + maxHigh[k - dep[u]]);
if (upd) maxHigh[dep[u]] = min(maxHigh[dep[u]], h[u]);
for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
dep[v] = dep[u] + w;
h[v] = h[u] + 1;
dfs(v, u, upd);
}
}
void del(int u, int p) {
if (dep[u] > k) return;
maxHigh[dep[u]] = +INF;
for (auto [v, w]: adj[u]) if (v != p && !rem[v]) del(v, u);
}
void solve(int u, int p) {
int c = findCentroid(u, p, dfsSZ(u, p)); rem[c] = true;
dep[c] = h[c] = 0;
for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
dep[v] = dep[c] + w;
h[v] = h[c] + 1;
dfs(v, c, 0);
dfs(v, c, 1);
}
for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
del(v, c);
}
for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
solve(v, c);
}
}
int best_path(int _n, int _k, int H[][2], int L[]) {
n = _n; k = _k;
for (int i = 0; i <= k; ++i) {
maxHigh[i] = +INF;
}
for (int i = 0; i < n - 1; i++) {
int u = H[i][0], v = H[i][1], l = L[i];
adj[u].emplace_back(v, l);
adj[v].emplace_back(u, l);
}
maxHigh[0] = 0; solve(0, -1);
return (res == +INF ? -1 : res);
}
//int main() {
// ios_base::sync_with_stdio(false); cin.tie(nullptr);
//
// if (fopen(task".inp", "r")) {
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
// }
//
// cin >> n >> k;
// for (int i = 0; i <= k; ++i) {
// maxHigh[i] = +INF;
// }
// for (int i = 1; i < n; ++i) {
// int u, v, l; cin >> u >> v >> l;
// adj[u].emplace_back(v, l);
// adj[v].emplace_back(u, l);
// }
// maxHigh[0] = 0; solve(0, -1);
// cout << (res == INF ? -1 : res);
// return 0;
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13304 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
3 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13184 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
2 ms |
13400 KB |
Output is correct |
12 |
Correct |
2 ms |
13148 KB |
Output is correct |
13 |
Correct |
2 ms |
13148 KB |
Output is correct |
14 |
Correct |
3 ms |
13148 KB |
Output is correct |
15 |
Correct |
2 ms |
13148 KB |
Output is correct |
16 |
Correct |
2 ms |
13148 KB |
Output is correct |
17 |
Correct |
2 ms |
13148 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13304 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
3 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13184 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
2 ms |
13400 KB |
Output is correct |
12 |
Correct |
2 ms |
13148 KB |
Output is correct |
13 |
Correct |
2 ms |
13148 KB |
Output is correct |
14 |
Correct |
3 ms |
13148 KB |
Output is correct |
15 |
Correct |
2 ms |
13148 KB |
Output is correct |
16 |
Correct |
2 ms |
13148 KB |
Output is correct |
17 |
Correct |
2 ms |
13148 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
3 ms |
13148 KB |
Output is correct |
20 |
Correct |
2 ms |
13176 KB |
Output is correct |
21 |
Correct |
3 ms |
13148 KB |
Output is correct |
22 |
Correct |
3 ms |
15248 KB |
Output is correct |
23 |
Correct |
3 ms |
15196 KB |
Output is correct |
24 |
Correct |
3 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
15196 KB |
Output is correct |
26 |
Correct |
3 ms |
15196 KB |
Output is correct |
27 |
Correct |
3 ms |
15192 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
3 ms |
15196 KB |
Output is correct |
31 |
Correct |
4 ms |
15204 KB |
Output is correct |
32 |
Correct |
3 ms |
15196 KB |
Output is correct |
33 |
Correct |
3 ms |
15192 KB |
Output is correct |
34 |
Correct |
3 ms |
15192 KB |
Output is correct |
35 |
Correct |
4 ms |
15228 KB |
Output is correct |
36 |
Correct |
4 ms |
15196 KB |
Output is correct |
37 |
Correct |
3 ms |
15196 KB |
Output is correct |
38 |
Correct |
3 ms |
15280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13304 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
3 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13184 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
2 ms |
13400 KB |
Output is correct |
12 |
Correct |
2 ms |
13148 KB |
Output is correct |
13 |
Correct |
2 ms |
13148 KB |
Output is correct |
14 |
Correct |
3 ms |
13148 KB |
Output is correct |
15 |
Correct |
2 ms |
13148 KB |
Output is correct |
16 |
Correct |
2 ms |
13148 KB |
Output is correct |
17 |
Correct |
2 ms |
13148 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
93 ms |
20400 KB |
Output is correct |
20 |
Correct |
92 ms |
20396 KB |
Output is correct |
21 |
Correct |
99 ms |
20416 KB |
Output is correct |
22 |
Correct |
97 ms |
20736 KB |
Output is correct |
23 |
Correct |
50 ms |
20572 KB |
Output is correct |
24 |
Correct |
40 ms |
20560 KB |
Output is correct |
25 |
Correct |
96 ms |
23160 KB |
Output is correct |
26 |
Correct |
77 ms |
25968 KB |
Output is correct |
27 |
Correct |
103 ms |
25616 KB |
Output is correct |
28 |
Correct |
212 ms |
37188 KB |
Output is correct |
29 |
Correct |
159 ms |
36180 KB |
Output is correct |
30 |
Correct |
106 ms |
25740 KB |
Output is correct |
31 |
Correct |
103 ms |
25728 KB |
Output is correct |
32 |
Correct |
122 ms |
25940 KB |
Output is correct |
33 |
Correct |
146 ms |
24468 KB |
Output is correct |
34 |
Correct |
111 ms |
25624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13304 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
3 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13184 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
2 ms |
13400 KB |
Output is correct |
12 |
Correct |
2 ms |
13148 KB |
Output is correct |
13 |
Correct |
2 ms |
13148 KB |
Output is correct |
14 |
Correct |
3 ms |
13148 KB |
Output is correct |
15 |
Correct |
2 ms |
13148 KB |
Output is correct |
16 |
Correct |
2 ms |
13148 KB |
Output is correct |
17 |
Correct |
2 ms |
13148 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
3 ms |
13148 KB |
Output is correct |
20 |
Correct |
2 ms |
13176 KB |
Output is correct |
21 |
Correct |
3 ms |
13148 KB |
Output is correct |
22 |
Correct |
3 ms |
15248 KB |
Output is correct |
23 |
Correct |
3 ms |
15196 KB |
Output is correct |
24 |
Correct |
3 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
15196 KB |
Output is correct |
26 |
Correct |
3 ms |
15196 KB |
Output is correct |
27 |
Correct |
3 ms |
15192 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
3 ms |
15196 KB |
Output is correct |
31 |
Correct |
4 ms |
15204 KB |
Output is correct |
32 |
Correct |
3 ms |
15196 KB |
Output is correct |
33 |
Correct |
3 ms |
15192 KB |
Output is correct |
34 |
Correct |
3 ms |
15192 KB |
Output is correct |
35 |
Correct |
4 ms |
15228 KB |
Output is correct |
36 |
Correct |
4 ms |
15196 KB |
Output is correct |
37 |
Correct |
3 ms |
15196 KB |
Output is correct |
38 |
Correct |
3 ms |
15280 KB |
Output is correct |
39 |
Correct |
93 ms |
20400 KB |
Output is correct |
40 |
Correct |
92 ms |
20396 KB |
Output is correct |
41 |
Correct |
99 ms |
20416 KB |
Output is correct |
42 |
Correct |
97 ms |
20736 KB |
Output is correct |
43 |
Correct |
50 ms |
20572 KB |
Output is correct |
44 |
Correct |
40 ms |
20560 KB |
Output is correct |
45 |
Correct |
96 ms |
23160 KB |
Output is correct |
46 |
Correct |
77 ms |
25968 KB |
Output is correct |
47 |
Correct |
103 ms |
25616 KB |
Output is correct |
48 |
Correct |
212 ms |
37188 KB |
Output is correct |
49 |
Correct |
159 ms |
36180 KB |
Output is correct |
50 |
Correct |
106 ms |
25740 KB |
Output is correct |
51 |
Correct |
103 ms |
25728 KB |
Output is correct |
52 |
Correct |
122 ms |
25940 KB |
Output is correct |
53 |
Correct |
146 ms |
24468 KB |
Output is correct |
54 |
Correct |
111 ms |
25624 KB |
Output is correct |
55 |
Correct |
11 ms |
13556 KB |
Output is correct |
56 |
Correct |
10 ms |
13660 KB |
Output is correct |
57 |
Correct |
62 ms |
20636 KB |
Output is correct |
58 |
Correct |
27 ms |
20164 KB |
Output is correct |
59 |
Correct |
76 ms |
27996 KB |
Output is correct |
60 |
Correct |
324 ms |
38296 KB |
Output is correct |
61 |
Correct |
137 ms |
26016 KB |
Output is correct |
62 |
Correct |
130 ms |
27988 KB |
Output is correct |
63 |
Correct |
171 ms |
28016 KB |
Output is correct |
64 |
Correct |
303 ms |
28452 KB |
Output is correct |
65 |
Correct |
119 ms |
26628 KB |
Output is correct |
66 |
Correct |
244 ms |
36388 KB |
Output is correct |
67 |
Correct |
76 ms |
28532 KB |
Output is correct |
68 |
Correct |
161 ms |
28660 KB |
Output is correct |
69 |
Correct |
158 ms |
28756 KB |
Output is correct |
70 |
Correct |
148 ms |
28372 KB |
Output is correct |