#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) {
vis[v] = true; sz[v] = 1;
for (auto& [viz, w] : adj[v]) {
if (vis[viz]) continue;
dfs(viz);
sz[v] += sz[viz];
}
}
int find_centroid(int v, int pai) {
// cout << "buscando centroid " << v << endl;
pair<int, int> mx = {v, 0};
for (auto& [viz, w] : adj[v]) {
if (viz == pai or in[viz]) continue;
if (mx.second < sz[viz]) mx = {viz, sz[viz]};
}
if (mx.second <= n / 2) {
// cout << "é" << v <<endl;
return v;
}
return find_centroid(mx.first, 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) {
int centroid = find_centroid(v, -1);
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});
sub_mn_depth[d] = 1e8;
all_dists.push_back(d);
}
sub_dists.clear();
}
for (int d : all_dists) {
sort(dists[d].begin(), dists[d].end());
}
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);
decomp(0);
return (ans == (int)1e8 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
35152 KB |
Output is correct |
2 |
Correct |
18 ms |
35152 KB |
Output is correct |
3 |
Correct |
20 ms |
35292 KB |
Output is correct |
4 |
Correct |
18 ms |
35152 KB |
Output is correct |
5 |
Correct |
18 ms |
35152 KB |
Output is correct |
6 |
Correct |
20 ms |
35152 KB |
Output is correct |
7 |
Correct |
21 ms |
35152 KB |
Output is correct |
8 |
Correct |
22 ms |
35152 KB |
Output is correct |
9 |
Correct |
20 ms |
35152 KB |
Output is correct |
10 |
Correct |
20 ms |
35152 KB |
Output is correct |
11 |
Correct |
18 ms |
35152 KB |
Output is correct |
12 |
Correct |
20 ms |
35152 KB |
Output is correct |
13 |
Correct |
21 ms |
35152 KB |
Output is correct |
14 |
Correct |
19 ms |
35448 KB |
Output is correct |
15 |
Correct |
20 ms |
35152 KB |
Output is correct |
16 |
Correct |
18 ms |
35152 KB |
Output is correct |
17 |
Correct |
20 ms |
35152 KB |
Output is correct |
18 |
Correct |
20 ms |
35152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
35152 KB |
Output is correct |
2 |
Correct |
18 ms |
35152 KB |
Output is correct |
3 |
Correct |
20 ms |
35292 KB |
Output is correct |
4 |
Correct |
18 ms |
35152 KB |
Output is correct |
5 |
Correct |
18 ms |
35152 KB |
Output is correct |
6 |
Correct |
20 ms |
35152 KB |
Output is correct |
7 |
Correct |
21 ms |
35152 KB |
Output is correct |
8 |
Correct |
22 ms |
35152 KB |
Output is correct |
9 |
Correct |
20 ms |
35152 KB |
Output is correct |
10 |
Correct |
20 ms |
35152 KB |
Output is correct |
11 |
Correct |
18 ms |
35152 KB |
Output is correct |
12 |
Correct |
20 ms |
35152 KB |
Output is correct |
13 |
Correct |
21 ms |
35152 KB |
Output is correct |
14 |
Correct |
19 ms |
35448 KB |
Output is correct |
15 |
Correct |
20 ms |
35152 KB |
Output is correct |
16 |
Correct |
18 ms |
35152 KB |
Output is correct |
17 |
Correct |
20 ms |
35152 KB |
Output is correct |
18 |
Correct |
20 ms |
35152 KB |
Output is correct |
19 |
Correct |
19 ms |
35152 KB |
Output is correct |
20 |
Correct |
19 ms |
35152 KB |
Output is correct |
21 |
Correct |
19 ms |
35152 KB |
Output is correct |
22 |
Correct |
21 ms |
35408 KB |
Output is correct |
23 |
Correct |
25 ms |
35408 KB |
Output is correct |
24 |
Correct |
27 ms |
35408 KB |
Output is correct |
25 |
Correct |
26 ms |
35408 KB |
Output is correct |
26 |
Correct |
19 ms |
35408 KB |
Output is correct |
27 |
Correct |
21 ms |
35296 KB |
Output is correct |
28 |
Correct |
22 ms |
35408 KB |
Output is correct |
29 |
Correct |
26 ms |
35408 KB |
Output is correct |
30 |
Correct |
21 ms |
35324 KB |
Output is correct |
31 |
Correct |
28 ms |
35408 KB |
Output is correct |
32 |
Correct |
23 ms |
35408 KB |
Output is correct |
33 |
Correct |
23 ms |
35664 KB |
Output is correct |
34 |
Correct |
26 ms |
35920 KB |
Output is correct |
35 |
Correct |
26 ms |
36188 KB |
Output is correct |
36 |
Correct |
32 ms |
36172 KB |
Output is correct |
37 |
Correct |
39 ms |
37192 KB |
Output is correct |
38 |
Correct |
29 ms |
36688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
35152 KB |
Output is correct |
2 |
Correct |
18 ms |
35152 KB |
Output is correct |
3 |
Correct |
20 ms |
35292 KB |
Output is correct |
4 |
Correct |
18 ms |
35152 KB |
Output is correct |
5 |
Correct |
18 ms |
35152 KB |
Output is correct |
6 |
Correct |
20 ms |
35152 KB |
Output is correct |
7 |
Correct |
21 ms |
35152 KB |
Output is correct |
8 |
Correct |
22 ms |
35152 KB |
Output is correct |
9 |
Correct |
20 ms |
35152 KB |
Output is correct |
10 |
Correct |
20 ms |
35152 KB |
Output is correct |
11 |
Correct |
18 ms |
35152 KB |
Output is correct |
12 |
Correct |
20 ms |
35152 KB |
Output is correct |
13 |
Correct |
21 ms |
35152 KB |
Output is correct |
14 |
Correct |
19 ms |
35448 KB |
Output is correct |
15 |
Correct |
20 ms |
35152 KB |
Output is correct |
16 |
Correct |
18 ms |
35152 KB |
Output is correct |
17 |
Correct |
20 ms |
35152 KB |
Output is correct |
18 |
Correct |
20 ms |
35152 KB |
Output is correct |
19 |
Correct |
515 ms |
40780 KB |
Output is correct |
20 |
Correct |
411 ms |
40604 KB |
Output is correct |
21 |
Correct |
1444 ms |
41668 KB |
Output is correct |
22 |
Correct |
1792 ms |
43112 KB |
Output is correct |
23 |
Correct |
61 ms |
40264 KB |
Output is correct |
24 |
Correct |
57 ms |
40064 KB |
Output is correct |
25 |
Execution timed out |
3066 ms |
179936 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
35152 KB |
Output is correct |
2 |
Correct |
18 ms |
35152 KB |
Output is correct |
3 |
Correct |
20 ms |
35292 KB |
Output is correct |
4 |
Correct |
18 ms |
35152 KB |
Output is correct |
5 |
Correct |
18 ms |
35152 KB |
Output is correct |
6 |
Correct |
20 ms |
35152 KB |
Output is correct |
7 |
Correct |
21 ms |
35152 KB |
Output is correct |
8 |
Correct |
22 ms |
35152 KB |
Output is correct |
9 |
Correct |
20 ms |
35152 KB |
Output is correct |
10 |
Correct |
20 ms |
35152 KB |
Output is correct |
11 |
Correct |
18 ms |
35152 KB |
Output is correct |
12 |
Correct |
20 ms |
35152 KB |
Output is correct |
13 |
Correct |
21 ms |
35152 KB |
Output is correct |
14 |
Correct |
19 ms |
35448 KB |
Output is correct |
15 |
Correct |
20 ms |
35152 KB |
Output is correct |
16 |
Correct |
18 ms |
35152 KB |
Output is correct |
17 |
Correct |
20 ms |
35152 KB |
Output is correct |
18 |
Correct |
20 ms |
35152 KB |
Output is correct |
19 |
Correct |
19 ms |
35152 KB |
Output is correct |
20 |
Correct |
19 ms |
35152 KB |
Output is correct |
21 |
Correct |
19 ms |
35152 KB |
Output is correct |
22 |
Correct |
21 ms |
35408 KB |
Output is correct |
23 |
Correct |
25 ms |
35408 KB |
Output is correct |
24 |
Correct |
27 ms |
35408 KB |
Output is correct |
25 |
Correct |
26 ms |
35408 KB |
Output is correct |
26 |
Correct |
19 ms |
35408 KB |
Output is correct |
27 |
Correct |
21 ms |
35296 KB |
Output is correct |
28 |
Correct |
22 ms |
35408 KB |
Output is correct |
29 |
Correct |
26 ms |
35408 KB |
Output is correct |
30 |
Correct |
21 ms |
35324 KB |
Output is correct |
31 |
Correct |
28 ms |
35408 KB |
Output is correct |
32 |
Correct |
23 ms |
35408 KB |
Output is correct |
33 |
Correct |
23 ms |
35664 KB |
Output is correct |
34 |
Correct |
26 ms |
35920 KB |
Output is correct |
35 |
Correct |
26 ms |
36188 KB |
Output is correct |
36 |
Correct |
32 ms |
36172 KB |
Output is correct |
37 |
Correct |
39 ms |
37192 KB |
Output is correct |
38 |
Correct |
29 ms |
36688 KB |
Output is correct |
39 |
Correct |
515 ms |
40780 KB |
Output is correct |
40 |
Correct |
411 ms |
40604 KB |
Output is correct |
41 |
Correct |
1444 ms |
41668 KB |
Output is correct |
42 |
Correct |
1792 ms |
43112 KB |
Output is correct |
43 |
Correct |
61 ms |
40264 KB |
Output is correct |
44 |
Correct |
57 ms |
40064 KB |
Output is correct |
45 |
Execution timed out |
3066 ms |
179936 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |