// #include "race.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
const int MAXN = 2e5;
const int MAXV = 1e6+1;
vector<pair<int, int>> adj[MAXN];
bool vis[MAXN];
int sz[MAXN];
bool in[MAXN];
int n, k;
void dfs(int v, int pai) {
sz[v] = 1;
for (auto& [viz, w] : adj[v]) {
if (viz == pai or in[viz]) continue;
dfs(viz, v);
sz[v] += sz[viz];
}
}
int find_centroid(int v, int pai, int qty) {
// cout << "buscando centroid " << v << endl;
for (auto& [viz, w] : adj[v]) {
if (viz == pai or in[viz]) continue;
if (sz[viz] > qty / 2) return find_centroid(viz, v, qty);
}
// cout << "é" << v <<endl;
return v;
}
vector<int> sub_mn_depth(MAXV, 1e8);
vector<pair<int, int>> dists[MAXV];
vector<int> sub_dists;
void get_dist(int v, int pai, int dist, int depth) {
if (dist > k) return;
sub_mn_depth[dist] = min(sub_mn_depth[dist], depth);
sub_dists.push_back(dist);
for (auto& [viz, w] : adj[v]) {
if (viz == pai or in[viz]) continue;
get_dist(viz, v, dist + w, depth+1);
}
}
int ans = 1e8;
void decomp(int v) {
dfs(v, -1);
int centroid = find_centroid(v, -1, sz[v]);
in[centroid] = true;
vector<int> all_dists;
// if (centroid == 8) cout << centroid << " é centroid decompondo" << v << endl;
for (auto& [viz, w] : adj[centroid]) {
if (in[viz]) continue;
get_dist(viz, centroid, w, 1);
for (int d : sub_dists) {
dists[d].push_back({sub_mn_depth[d], viz});
sort(dists[d].begin(), dists[d].end());
if ((int)dists[d].size() > 2) dists[d].pop_back();
sub_mn_depth[d] = 1e8;
all_dists.push_back(d);
}
sub_dists.clear();
}
if (!dists[k].empty()) ans = min(ans, dists[k][0].first);
for (auto& [viz, w] : adj[centroid]) {
if (in[viz]) continue;
get_dist(viz, centroid, w, 1);
for (int d : sub_dists) {
for (int i=0; d <= k and i<min(2, (int)dists[k-d].size()); i++) {
if (dists[k-d][i].second == viz) continue;
ans = min(ans, dists[k-d][i].first + sub_mn_depth[d]);
}
sub_mn_depth[d] = 1e8;
}
sub_dists.clear();
}
for (int d : all_dists) dists[d].clear();
for (auto& [viz, w] : adj[centroid]) {
if (in[viz]) continue;
decomp(viz);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N; k = K;
for (int i=0; i<N-1; i++) {
int u = H[i][0], v = H[i][1];
int w = L[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(0, -1);
decomp(0);
return (ans == (int)1e8 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
37200 KB |
Output is correct |
2 |
Correct |
7 ms |
37200 KB |
Output is correct |
3 |
Correct |
7 ms |
37216 KB |
Output is correct |
4 |
Correct |
7 ms |
37200 KB |
Output is correct |
5 |
Correct |
7 ms |
37300 KB |
Output is correct |
6 |
Correct |
7 ms |
37384 KB |
Output is correct |
7 |
Correct |
7 ms |
37200 KB |
Output is correct |
8 |
Correct |
8 ms |
37200 KB |
Output is correct |
9 |
Correct |
7 ms |
37200 KB |
Output is correct |
10 |
Correct |
8 ms |
37492 KB |
Output is correct |
11 |
Correct |
7 ms |
37416 KB |
Output is correct |
12 |
Correct |
7 ms |
37200 KB |
Output is correct |
13 |
Correct |
8 ms |
37200 KB |
Output is correct |
14 |
Correct |
7 ms |
37200 KB |
Output is correct |
15 |
Correct |
7 ms |
37200 KB |
Output is correct |
16 |
Correct |
8 ms |
37200 KB |
Output is correct |
17 |
Correct |
7 ms |
37232 KB |
Output is correct |
18 |
Correct |
7 ms |
37200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
37200 KB |
Output is correct |
2 |
Correct |
7 ms |
37200 KB |
Output is correct |
3 |
Correct |
7 ms |
37216 KB |
Output is correct |
4 |
Correct |
7 ms |
37200 KB |
Output is correct |
5 |
Correct |
7 ms |
37300 KB |
Output is correct |
6 |
Correct |
7 ms |
37384 KB |
Output is correct |
7 |
Correct |
7 ms |
37200 KB |
Output is correct |
8 |
Correct |
8 ms |
37200 KB |
Output is correct |
9 |
Correct |
7 ms |
37200 KB |
Output is correct |
10 |
Correct |
8 ms |
37492 KB |
Output is correct |
11 |
Correct |
7 ms |
37416 KB |
Output is correct |
12 |
Correct |
7 ms |
37200 KB |
Output is correct |
13 |
Correct |
8 ms |
37200 KB |
Output is correct |
14 |
Correct |
7 ms |
37200 KB |
Output is correct |
15 |
Correct |
7 ms |
37200 KB |
Output is correct |
16 |
Correct |
8 ms |
37200 KB |
Output is correct |
17 |
Correct |
7 ms |
37232 KB |
Output is correct |
18 |
Correct |
7 ms |
37200 KB |
Output is correct |
19 |
Correct |
10 ms |
37200 KB |
Output is correct |
20 |
Correct |
8 ms |
37212 KB |
Output is correct |
21 |
Correct |
7 ms |
37200 KB |
Output is correct |
22 |
Correct |
8 ms |
37200 KB |
Output is correct |
23 |
Correct |
7 ms |
37200 KB |
Output is correct |
24 |
Correct |
7 ms |
37456 KB |
Output is correct |
25 |
Correct |
9 ms |
37456 KB |
Output is correct |
26 |
Correct |
8 ms |
37212 KB |
Output is correct |
27 |
Correct |
7 ms |
37200 KB |
Output is correct |
28 |
Correct |
9 ms |
37456 KB |
Output is correct |
29 |
Correct |
10 ms |
37456 KB |
Output is correct |
30 |
Correct |
8 ms |
37624 KB |
Output is correct |
31 |
Correct |
8 ms |
37456 KB |
Output is correct |
32 |
Correct |
8 ms |
37624 KB |
Output is correct |
33 |
Correct |
8 ms |
37588 KB |
Output is correct |
34 |
Correct |
8 ms |
37456 KB |
Output is correct |
35 |
Correct |
8 ms |
37456 KB |
Output is correct |
36 |
Correct |
8 ms |
37456 KB |
Output is correct |
37 |
Correct |
8 ms |
37456 KB |
Output is correct |
38 |
Correct |
8 ms |
37456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
37200 KB |
Output is correct |
2 |
Correct |
7 ms |
37200 KB |
Output is correct |
3 |
Correct |
7 ms |
37216 KB |
Output is correct |
4 |
Correct |
7 ms |
37200 KB |
Output is correct |
5 |
Correct |
7 ms |
37300 KB |
Output is correct |
6 |
Correct |
7 ms |
37384 KB |
Output is correct |
7 |
Correct |
7 ms |
37200 KB |
Output is correct |
8 |
Correct |
8 ms |
37200 KB |
Output is correct |
9 |
Correct |
7 ms |
37200 KB |
Output is correct |
10 |
Correct |
8 ms |
37492 KB |
Output is correct |
11 |
Correct |
7 ms |
37416 KB |
Output is correct |
12 |
Correct |
7 ms |
37200 KB |
Output is correct |
13 |
Correct |
8 ms |
37200 KB |
Output is correct |
14 |
Correct |
7 ms |
37200 KB |
Output is correct |
15 |
Correct |
7 ms |
37200 KB |
Output is correct |
16 |
Correct |
8 ms |
37200 KB |
Output is correct |
17 |
Correct |
7 ms |
37232 KB |
Output is correct |
18 |
Correct |
7 ms |
37200 KB |
Output is correct |
19 |
Correct |
105 ms |
43336 KB |
Output is correct |
20 |
Correct |
103 ms |
43336 KB |
Output is correct |
21 |
Correct |
132 ms |
43696 KB |
Output is correct |
22 |
Correct |
99 ms |
44224 KB |
Output is correct |
23 |
Correct |
57 ms |
43252 KB |
Output is correct |
24 |
Correct |
45 ms |
43336 KB |
Output is correct |
25 |
Correct |
108 ms |
44360 KB |
Output is correct |
26 |
Correct |
88 ms |
46860 KB |
Output is correct |
27 |
Correct |
122 ms |
46920 KB |
Output is correct |
28 |
Correct |
159 ms |
52040 KB |
Output is correct |
29 |
Correct |
161 ms |
52448 KB |
Output is correct |
30 |
Correct |
149 ms |
47176 KB |
Output is correct |
31 |
Correct |
139 ms |
46992 KB |
Output is correct |
32 |
Correct |
146 ms |
48864 KB |
Output is correct |
33 |
Correct |
157 ms |
45896 KB |
Output is correct |
34 |
Correct |
137 ms |
45716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
37200 KB |
Output is correct |
2 |
Correct |
7 ms |
37200 KB |
Output is correct |
3 |
Correct |
7 ms |
37216 KB |
Output is correct |
4 |
Correct |
7 ms |
37200 KB |
Output is correct |
5 |
Correct |
7 ms |
37300 KB |
Output is correct |
6 |
Correct |
7 ms |
37384 KB |
Output is correct |
7 |
Correct |
7 ms |
37200 KB |
Output is correct |
8 |
Correct |
8 ms |
37200 KB |
Output is correct |
9 |
Correct |
7 ms |
37200 KB |
Output is correct |
10 |
Correct |
8 ms |
37492 KB |
Output is correct |
11 |
Correct |
7 ms |
37416 KB |
Output is correct |
12 |
Correct |
7 ms |
37200 KB |
Output is correct |
13 |
Correct |
8 ms |
37200 KB |
Output is correct |
14 |
Correct |
7 ms |
37200 KB |
Output is correct |
15 |
Correct |
7 ms |
37200 KB |
Output is correct |
16 |
Correct |
8 ms |
37200 KB |
Output is correct |
17 |
Correct |
7 ms |
37232 KB |
Output is correct |
18 |
Correct |
7 ms |
37200 KB |
Output is correct |
19 |
Correct |
10 ms |
37200 KB |
Output is correct |
20 |
Correct |
8 ms |
37212 KB |
Output is correct |
21 |
Correct |
7 ms |
37200 KB |
Output is correct |
22 |
Correct |
8 ms |
37200 KB |
Output is correct |
23 |
Correct |
7 ms |
37200 KB |
Output is correct |
24 |
Correct |
7 ms |
37456 KB |
Output is correct |
25 |
Correct |
9 ms |
37456 KB |
Output is correct |
26 |
Correct |
8 ms |
37212 KB |
Output is correct |
27 |
Correct |
7 ms |
37200 KB |
Output is correct |
28 |
Correct |
9 ms |
37456 KB |
Output is correct |
29 |
Correct |
10 ms |
37456 KB |
Output is correct |
30 |
Correct |
8 ms |
37624 KB |
Output is correct |
31 |
Correct |
8 ms |
37456 KB |
Output is correct |
32 |
Correct |
8 ms |
37624 KB |
Output is correct |
33 |
Correct |
8 ms |
37588 KB |
Output is correct |
34 |
Correct |
8 ms |
37456 KB |
Output is correct |
35 |
Correct |
8 ms |
37456 KB |
Output is correct |
36 |
Correct |
8 ms |
37456 KB |
Output is correct |
37 |
Correct |
8 ms |
37456 KB |
Output is correct |
38 |
Correct |
8 ms |
37456 KB |
Output is correct |
39 |
Correct |
105 ms |
43336 KB |
Output is correct |
40 |
Correct |
103 ms |
43336 KB |
Output is correct |
41 |
Correct |
132 ms |
43696 KB |
Output is correct |
42 |
Correct |
99 ms |
44224 KB |
Output is correct |
43 |
Correct |
57 ms |
43252 KB |
Output is correct |
44 |
Correct |
45 ms |
43336 KB |
Output is correct |
45 |
Correct |
108 ms |
44360 KB |
Output is correct |
46 |
Correct |
88 ms |
46860 KB |
Output is correct |
47 |
Correct |
122 ms |
46920 KB |
Output is correct |
48 |
Correct |
159 ms |
52040 KB |
Output is correct |
49 |
Correct |
161 ms |
52448 KB |
Output is correct |
50 |
Correct |
149 ms |
47176 KB |
Output is correct |
51 |
Correct |
139 ms |
46992 KB |
Output is correct |
52 |
Correct |
146 ms |
48864 KB |
Output is correct |
53 |
Correct |
157 ms |
45896 KB |
Output is correct |
54 |
Correct |
137 ms |
45716 KB |
Output is correct |
55 |
Correct |
17 ms |
37968 KB |
Output is correct |
56 |
Correct |
18 ms |
37788 KB |
Output is correct |
57 |
Correct |
82 ms |
44360 KB |
Output is correct |
58 |
Correct |
34 ms |
44228 KB |
Output is correct |
59 |
Correct |
102 ms |
50624 KB |
Output is correct |
60 |
Correct |
374 ms |
64196 KB |
Output is correct |
61 |
Correct |
152 ms |
47432 KB |
Output is correct |
62 |
Correct |
176 ms |
48320 KB |
Output is correct |
63 |
Correct |
176 ms |
48320 KB |
Output is correct |
64 |
Correct |
334 ms |
55552 KB |
Output is correct |
65 |
Correct |
121 ms |
46920 KB |
Output is correct |
66 |
Correct |
321 ms |
54812 KB |
Output is correct |
67 |
Correct |
81 ms |
51640 KB |
Output is correct |
68 |
Correct |
229 ms |
54164 KB |
Output is correct |
69 |
Correct |
264 ms |
56444 KB |
Output is correct |
70 |
Correct |
285 ms |
53628 KB |
Output is correct |