#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define num long long
#define di pair<int, num>
#define sdi set<di>
#define graph vector<sdi>
#define idata vector<int>
#define INF 1e18
graph roads;
int nbNodes, pathLength;
struct MMap {
map<num, num> m;
bool has (num depth) {
return m.find(depth) != m.end();
}
num get (num depth) {
return (*(m.find(depth))).second;
}
void set (num depth, num value) {
if (has(depth)) {
value = min(value, get(depth));
m.erase(depth);
}
m.insert({ depth, value });
}
void clear () {
m.clear();
}
void show () {
for (auto u : m)
printf("%lld:%lld ", u.first, u.second);
cout << endl;
}
};
struct CDecompose {
num answer = 1e18;
idata subtree;
MMap min_map;
void init () {
subtree.resize(nbNodes);
build (0, -1);
}
void build (int node, int parent) {
int compSize = w_dfs(node, parent);
int centroid = c_dfs(node, parent, compSize >> 1);
min_map.set(0, 0);
for (di next : roads[centroid]) {
comp(next.first, centroid, next.second, 1);
fill(next.first, centroid, next.second, 1);
}
min_map.clear();
while (roads[centroid].size() != 0) {
di err = *roads[centroid].begin();
roads[centroid ].erase(err);
roads[err.first].erase({ centroid, err.second });
build (err.first, -1);
}
}
int w_dfs (int node, int parent) {
subtree[node] = 1;
for (di next : roads[node]) {
if (next.first == parent) continue ;
subtree[node] += w_dfs(next.first, node);
}
return subtree[node];
}
int c_dfs (int node, int parent, int desired) {
for (di next : roads[node])
if (next.first != parent && desired <= subtree[next.first])
return c_dfs(next.first, node, desired);
return node;
}
void comp (int node, int parent, num length, int depth) {
if (length > pathLength) return ;
if (depth > answer) return ;
if (min_map.has(pathLength - length)) {
answer = min(
answer,
min_map.get(pathLength - length) + depth
);
}
for (di next : roads[node]) {
if (next.first == parent) continue ;
comp(next.first, node, length + next.second, depth + 1);
}
}
void fill (int node, int parent, num length, int depth) {
if (length > pathLength) return ;
if (depth > answer) return ;
min_map.set(length, depth);
for (di next : roads[node]) {
if (next.first == parent) continue ;
fill(next.first, node, length + next.second, depth + 1);
}
}
};
int best_path(int N, int K, int H[][2], int L[])
{
nbNodes = N;
pathLength = K;
roads.resize(nbNodes);
for (int idRoad = 0; idRoad + 1 < nbNodes; idRoad ++) {
int a, b; num l;
a = H[idRoad][0];
b = H[idRoad][1];
l = L[idRoad];
roads[a].insert({ b, l });
roads[b].insert({ a, l });
}
CDecompose dec;
dec.init();
return dec.answer == INF ? -1 : dec.answer;
}
/*int main () {
int N, K;
cin >> N >> K;
int H[N][2];
int L[N];
for (int i = 0; i + 1 < N; i++)
cin >> H[i][0] >> H[i][1] >> L[i];
printf("%d", best_path(N, K, H, L));
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
2 ms |
440 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
576 KB |
Output is correct |
32 |
Correct |
2 ms |
580 KB |
Output is correct |
33 |
Correct |
2 ms |
580 KB |
Output is correct |
34 |
Correct |
2 ms |
448 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
270 ms |
20452 KB |
Output is correct |
20 |
Correct |
281 ms |
20456 KB |
Output is correct |
21 |
Correct |
286 ms |
20480 KB |
Output is correct |
22 |
Correct |
346 ms |
20496 KB |
Output is correct |
23 |
Correct |
123 ms |
20412 KB |
Output is correct |
24 |
Correct |
118 ms |
20532 KB |
Output is correct |
25 |
Correct |
264 ms |
22644 KB |
Output is correct |
26 |
Correct |
198 ms |
26640 KB |
Output is correct |
27 |
Correct |
326 ms |
40716 KB |
Output is correct |
28 |
Correct |
402 ms |
49164 KB |
Output is correct |
29 |
Correct |
415 ms |
48672 KB |
Output is correct |
30 |
Correct |
324 ms |
40504 KB |
Output is correct |
31 |
Correct |
342 ms |
40568 KB |
Output is correct |
32 |
Correct |
363 ms |
40636 KB |
Output is correct |
33 |
Correct |
511 ms |
40680 KB |
Output is correct |
34 |
Correct |
281 ms |
41496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
2 ms |
440 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
576 KB |
Output is correct |
32 |
Correct |
2 ms |
580 KB |
Output is correct |
33 |
Correct |
2 ms |
580 KB |
Output is correct |
34 |
Correct |
2 ms |
448 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
270 ms |
20452 KB |
Output is correct |
40 |
Correct |
281 ms |
20456 KB |
Output is correct |
41 |
Correct |
286 ms |
20480 KB |
Output is correct |
42 |
Correct |
346 ms |
20496 KB |
Output is correct |
43 |
Correct |
123 ms |
20412 KB |
Output is correct |
44 |
Correct |
118 ms |
20532 KB |
Output is correct |
45 |
Correct |
264 ms |
22644 KB |
Output is correct |
46 |
Correct |
198 ms |
26640 KB |
Output is correct |
47 |
Correct |
326 ms |
40716 KB |
Output is correct |
48 |
Correct |
402 ms |
49164 KB |
Output is correct |
49 |
Correct |
415 ms |
48672 KB |
Output is correct |
50 |
Correct |
324 ms |
40504 KB |
Output is correct |
51 |
Correct |
342 ms |
40568 KB |
Output is correct |
52 |
Correct |
363 ms |
40636 KB |
Output is correct |
53 |
Correct |
511 ms |
40680 KB |
Output is correct |
54 |
Correct |
281 ms |
41496 KB |
Output is correct |
55 |
Correct |
20 ms |
2388 KB |
Output is correct |
56 |
Correct |
23 ms |
2232 KB |
Output is correct |
57 |
Correct |
265 ms |
20432 KB |
Output is correct |
58 |
Correct |
104 ms |
20060 KB |
Output is correct |
59 |
Correct |
348 ms |
31308 KB |
Output is correct |
60 |
Correct |
1167 ms |
64672 KB |
Output is correct |
61 |
Correct |
378 ms |
41072 KB |
Output is correct |
62 |
Correct |
473 ms |
40908 KB |
Output is correct |
63 |
Correct |
517 ms |
40784 KB |
Output is correct |
64 |
Correct |
1145 ms |
47432 KB |
Output is correct |
65 |
Correct |
331 ms |
41824 KB |
Output is correct |
66 |
Correct |
724 ms |
47716 KB |
Output is correct |
67 |
Correct |
346 ms |
41352 KB |
Output is correct |
68 |
Correct |
402 ms |
44892 KB |
Output is correct |
69 |
Correct |
361 ms |
41832 KB |
Output is correct |
70 |
Correct |
328 ms |
38916 KB |
Output is correct |