#include "race.h"
//#include "grader.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int NMAX = 2e5 + 5;
vector<pair<int, int>> adj[NMAX];
int sz[NMAX];
bool removed[NMAX];
int get_size(int u, int e) {
sz[u] = 1;
for (auto&x : adj[u]) {
int v = x.first;
if (v == e || removed[v]) continue;
sz[u] += get_size(v, u);
}
return sz[u];
}
int get_centroid(int u, int e, int s) {
for (auto&x : adj[u]) {
int v = x.first;
if (v == e || removed[v] || sz[v] <= s/2) continue;
return get_centroid(v, u, s);
}
return u;
}
int dist[NMAX], depth[NMAX], ans = 1e18;
void dfs(int u, int e, int root, map<int, int>&res) {
if (!res.count(dist[u])) res[dist[u]] = depth[u];
res[dist[u]] = min(res[dist[u]], depth[u]);
for (auto&x : adj[u]) {
int v = x.first, c = x.second;
if (v == e || removed[v]) continue;
dist[v] = dist[u] + c, depth[v] = depth[u] + 1;
dfs(v, u, root, res);
}
}
void build(int u, int e, int K, int N) {
int s = get_size(u, e), centroid = get_centroid(u, e, s);
//
map<int, int> tot;
tot[0] = dist[centroid] = depth[centroid] = 0;
for (auto&x : adj[centroid]) {
int v = x.first, c = x.second;
if (v == e || removed[v]) continue;
dist[v] = dist[centroid] + c, depth[v] = depth[centroid] + 1;
map<int, int> res;
dfs(v, centroid, v, res);
if (tot.size() < res.size()) swap(tot, res);
for (auto&x : res) {
int cost = x.first, edge = x.second;
if (tot.count(K - cost)) ans = min(ans, edge + tot[K - cost]);
}
for (auto&x : res) {
int cost = x.first, edge = x.second;
if (tot.count(cost)) tot[cost] = min(tot[cost], edge);
else tot[cost] = edge;
}
}
//
removed[centroid] = true;
for (auto&x : adj[centroid]) {
int v = x.first;
if (v == e || removed[v]) continue;
build(v, centroid, K, N);
}
}
signed best_path(signed N, signed K, signed H[][2], signed L[]) {
for (int i = 0; i < N - 1; i++) {
int u = H[i][0], v = H[i][1], c = L[i];
//adj[u].push_back({v, c}), adj[v].push_back({u, c});
adj[u].push_back(make_pair(v, c)), adj[v].push_back(make_pair(u, c));
}
build(0, -1, K, N);
return ans == 1e18 ? -1 : ans;
}
/*signed N, K, H[NMAX][2], L[NMAX];
signed main() {
cin >> N >> K;
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';
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4960 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4996 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
5 ms |
5064 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4960 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4996 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
5 ms |
5064 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
5 ms |
5168 KB |
Output is correct |
22 |
Correct |
5 ms |
5204 KB |
Output is correct |
23 |
Correct |
6 ms |
5204 KB |
Output is correct |
24 |
Correct |
5 ms |
5236 KB |
Output is correct |
25 |
Correct |
5 ms |
5232 KB |
Output is correct |
26 |
Correct |
5 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5204 KB |
Output is correct |
29 |
Correct |
5 ms |
5144 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
6 ms |
5204 KB |
Output is correct |
32 |
Correct |
6 ms |
5204 KB |
Output is correct |
33 |
Correct |
8 ms |
5204 KB |
Output is correct |
34 |
Correct |
5 ms |
5204 KB |
Output is correct |
35 |
Correct |
5 ms |
5204 KB |
Output is correct |
36 |
Correct |
5 ms |
5204 KB |
Output is correct |
37 |
Correct |
6 ms |
5204 KB |
Output is correct |
38 |
Correct |
6 ms |
5148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4960 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4996 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
5 ms |
5064 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
370 ms |
13848 KB |
Output is correct |
20 |
Correct |
406 ms |
13828 KB |
Output is correct |
21 |
Correct |
338 ms |
13832 KB |
Output is correct |
22 |
Correct |
310 ms |
13852 KB |
Output is correct |
23 |
Correct |
464 ms |
14524 KB |
Output is correct |
24 |
Correct |
226 ms |
13392 KB |
Output is correct |
25 |
Correct |
246 ms |
19516 KB |
Output is correct |
26 |
Correct |
104 ms |
19520 KB |
Output is correct |
27 |
Correct |
402 ms |
23168 KB |
Output is correct |
28 |
Correct |
1533 ms |
58924 KB |
Output is correct |
29 |
Correct |
1486 ms |
58048 KB |
Output is correct |
30 |
Correct |
383 ms |
23356 KB |
Output is correct |
31 |
Correct |
367 ms |
23288 KB |
Output is correct |
32 |
Correct |
468 ms |
23244 KB |
Output is correct |
33 |
Correct |
716 ms |
22440 KB |
Output is correct |
34 |
Correct |
1431 ms |
43656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4960 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4996 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
5 ms |
5064 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
5 ms |
5168 KB |
Output is correct |
22 |
Correct |
5 ms |
5204 KB |
Output is correct |
23 |
Correct |
6 ms |
5204 KB |
Output is correct |
24 |
Correct |
5 ms |
5236 KB |
Output is correct |
25 |
Correct |
5 ms |
5232 KB |
Output is correct |
26 |
Correct |
5 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5204 KB |
Output is correct |
29 |
Correct |
5 ms |
5144 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
6 ms |
5204 KB |
Output is correct |
32 |
Correct |
6 ms |
5204 KB |
Output is correct |
33 |
Correct |
8 ms |
5204 KB |
Output is correct |
34 |
Correct |
5 ms |
5204 KB |
Output is correct |
35 |
Correct |
5 ms |
5204 KB |
Output is correct |
36 |
Correct |
5 ms |
5204 KB |
Output is correct |
37 |
Correct |
6 ms |
5204 KB |
Output is correct |
38 |
Correct |
6 ms |
5148 KB |
Output is correct |
39 |
Correct |
370 ms |
13848 KB |
Output is correct |
40 |
Correct |
406 ms |
13828 KB |
Output is correct |
41 |
Correct |
338 ms |
13832 KB |
Output is correct |
42 |
Correct |
310 ms |
13852 KB |
Output is correct |
43 |
Correct |
464 ms |
14524 KB |
Output is correct |
44 |
Correct |
226 ms |
13392 KB |
Output is correct |
45 |
Correct |
246 ms |
19516 KB |
Output is correct |
46 |
Correct |
104 ms |
19520 KB |
Output is correct |
47 |
Correct |
402 ms |
23168 KB |
Output is correct |
48 |
Correct |
1533 ms |
58924 KB |
Output is correct |
49 |
Correct |
1486 ms |
58048 KB |
Output is correct |
50 |
Correct |
383 ms |
23356 KB |
Output is correct |
51 |
Correct |
367 ms |
23288 KB |
Output is correct |
52 |
Correct |
468 ms |
23244 KB |
Output is correct |
53 |
Correct |
716 ms |
22440 KB |
Output is correct |
54 |
Correct |
1431 ms |
43656 KB |
Output is correct |
55 |
Correct |
33 ms |
6612 KB |
Output is correct |
56 |
Correct |
17 ms |
5844 KB |
Output is correct |
57 |
Correct |
153 ms |
14220 KB |
Output is correct |
58 |
Correct |
54 ms |
14400 KB |
Output is correct |
59 |
Correct |
484 ms |
30432 KB |
Output is correct |
60 |
Correct |
1432 ms |
59420 KB |
Output is correct |
61 |
Correct |
433 ms |
28072 KB |
Output is correct |
62 |
Correct |
367 ms |
26204 KB |
Output is correct |
63 |
Correct |
450 ms |
26256 KB |
Output is correct |
64 |
Correct |
1491 ms |
40740 KB |
Output is correct |
65 |
Correct |
1194 ms |
48200 KB |
Output is correct |
66 |
Correct |
1416 ms |
60748 KB |
Output is correct |
67 |
Correct |
176 ms |
25136 KB |
Output is correct |
68 |
Correct |
624 ms |
41860 KB |
Output is correct |
69 |
Correct |
613 ms |
42224 KB |
Output is correct |
70 |
Correct |
590 ms |
40152 KB |
Output is correct |