// #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) find_centroid(viz, v, qty);
}
// cout << "é" << v <<endl;
return v;
}
int ans = 1e8;
vector<int> dists(MAXV, 1e8);
// op = 1 => preenche, -1 => apaga, 0 => pega resposta
void dfs_dist(int v, int pai, int dist, int depth, int op) {
if (dist > k) return;
if (op == 0)
ans = min(ans, dists[k-dist] + depth);
else if (op == 1)
dists[dist] = min(dists[dist], depth);
else dists[dist] = 1e8;
for (auto& [viz, w] : adj[v]) {
if (viz == pai or in[viz]) continue;
dfs_dist(viz, v, dist + w, depth+1, op);
}
}
void decomp(int v) {
dfs(v, -1);
int centroid = find_centroid(v, -1, sz[v]);
in[centroid] = true;
// if (centroid == 8) cout << centroid << " é centroid decompondo" << v << endl;
for (auto& [viz, w] : adj[centroid]) {
if (in[viz]) continue;
dfs_dist(viz, centroid, w, 1, 0); // resposta considera prefixo subárvores
dfs_dist(viz, centroid, w, 1, 1);
}
ans = min(ans, dists[k]);
for (auto& [viz, w] : adj[centroid]) {
if (in[viz]) continue;
dfs_dist(viz, centroid, w, 1, -1);
}
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});
}
decomp(0);
return (ans == (int)1e8 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
13648 KB |
Output is correct |
2 |
Correct |
5 ms |
13648 KB |
Output is correct |
3 |
Correct |
5 ms |
13824 KB |
Output is correct |
4 |
Correct |
5 ms |
13648 KB |
Output is correct |
5 |
Correct |
5 ms |
13648 KB |
Output is correct |
6 |
Correct |
5 ms |
13648 KB |
Output is correct |
7 |
Correct |
5 ms |
13648 KB |
Output is correct |
8 |
Correct |
5 ms |
13648 KB |
Output is correct |
9 |
Correct |
5 ms |
13648 KB |
Output is correct |
10 |
Correct |
5 ms |
13648 KB |
Output is correct |
11 |
Correct |
5 ms |
13648 KB |
Output is correct |
12 |
Correct |
5 ms |
13648 KB |
Output is correct |
13 |
Correct |
5 ms |
13648 KB |
Output is correct |
14 |
Correct |
5 ms |
13648 KB |
Output is correct |
15 |
Correct |
5 ms |
13648 KB |
Output is correct |
16 |
Correct |
6 ms |
13648 KB |
Output is correct |
17 |
Correct |
5 ms |
13648 KB |
Output is correct |
18 |
Correct |
6 ms |
13648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
13648 KB |
Output is correct |
2 |
Correct |
5 ms |
13648 KB |
Output is correct |
3 |
Correct |
5 ms |
13824 KB |
Output is correct |
4 |
Correct |
5 ms |
13648 KB |
Output is correct |
5 |
Correct |
5 ms |
13648 KB |
Output is correct |
6 |
Correct |
5 ms |
13648 KB |
Output is correct |
7 |
Correct |
5 ms |
13648 KB |
Output is correct |
8 |
Correct |
5 ms |
13648 KB |
Output is correct |
9 |
Correct |
5 ms |
13648 KB |
Output is correct |
10 |
Correct |
5 ms |
13648 KB |
Output is correct |
11 |
Correct |
5 ms |
13648 KB |
Output is correct |
12 |
Correct |
5 ms |
13648 KB |
Output is correct |
13 |
Correct |
5 ms |
13648 KB |
Output is correct |
14 |
Correct |
5 ms |
13648 KB |
Output is correct |
15 |
Correct |
5 ms |
13648 KB |
Output is correct |
16 |
Correct |
6 ms |
13648 KB |
Output is correct |
17 |
Correct |
5 ms |
13648 KB |
Output is correct |
18 |
Correct |
6 ms |
13648 KB |
Output is correct |
19 |
Correct |
5 ms |
13648 KB |
Output is correct |
20 |
Correct |
5 ms |
13648 KB |
Output is correct |
21 |
Correct |
6 ms |
13904 KB |
Output is correct |
22 |
Correct |
6 ms |
13904 KB |
Output is correct |
23 |
Correct |
6 ms |
13904 KB |
Output is correct |
24 |
Correct |
6 ms |
13804 KB |
Output is correct |
25 |
Correct |
6 ms |
13696 KB |
Output is correct |
26 |
Correct |
6 ms |
13904 KB |
Output is correct |
27 |
Correct |
5 ms |
13904 KB |
Output is correct |
28 |
Correct |
6 ms |
13904 KB |
Output is correct |
29 |
Correct |
6 ms |
13876 KB |
Output is correct |
30 |
Correct |
6 ms |
13904 KB |
Output is correct |
31 |
Correct |
7 ms |
13904 KB |
Output is correct |
32 |
Correct |
5 ms |
13748 KB |
Output is correct |
33 |
Correct |
9 ms |
13904 KB |
Output is correct |
34 |
Correct |
8 ms |
13904 KB |
Output is correct |
35 |
Correct |
11 ms |
13904 KB |
Output is correct |
36 |
Correct |
8 ms |
13952 KB |
Output is correct |
37 |
Correct |
9 ms |
13904 KB |
Output is correct |
38 |
Correct |
8 ms |
13904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
13648 KB |
Output is correct |
2 |
Correct |
5 ms |
13648 KB |
Output is correct |
3 |
Correct |
5 ms |
13824 KB |
Output is correct |
4 |
Correct |
5 ms |
13648 KB |
Output is correct |
5 |
Correct |
5 ms |
13648 KB |
Output is correct |
6 |
Correct |
5 ms |
13648 KB |
Output is correct |
7 |
Correct |
5 ms |
13648 KB |
Output is correct |
8 |
Correct |
5 ms |
13648 KB |
Output is correct |
9 |
Correct |
5 ms |
13648 KB |
Output is correct |
10 |
Correct |
5 ms |
13648 KB |
Output is correct |
11 |
Correct |
5 ms |
13648 KB |
Output is correct |
12 |
Correct |
5 ms |
13648 KB |
Output is correct |
13 |
Correct |
5 ms |
13648 KB |
Output is correct |
14 |
Correct |
5 ms |
13648 KB |
Output is correct |
15 |
Correct |
5 ms |
13648 KB |
Output is correct |
16 |
Correct |
6 ms |
13648 KB |
Output is correct |
17 |
Correct |
5 ms |
13648 KB |
Output is correct |
18 |
Correct |
6 ms |
13648 KB |
Output is correct |
19 |
Correct |
204 ms |
19536 KB |
Output is correct |
20 |
Correct |
222 ms |
19740 KB |
Output is correct |
21 |
Correct |
184 ms |
19528 KB |
Output is correct |
22 |
Correct |
136 ms |
19792 KB |
Output is correct |
23 |
Correct |
74 ms |
19784 KB |
Output is correct |
24 |
Correct |
46 ms |
19792 KB |
Output is correct |
25 |
Execution timed out |
3055 ms |
20876 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
13648 KB |
Output is correct |
2 |
Correct |
5 ms |
13648 KB |
Output is correct |
3 |
Correct |
5 ms |
13824 KB |
Output is correct |
4 |
Correct |
5 ms |
13648 KB |
Output is correct |
5 |
Correct |
5 ms |
13648 KB |
Output is correct |
6 |
Correct |
5 ms |
13648 KB |
Output is correct |
7 |
Correct |
5 ms |
13648 KB |
Output is correct |
8 |
Correct |
5 ms |
13648 KB |
Output is correct |
9 |
Correct |
5 ms |
13648 KB |
Output is correct |
10 |
Correct |
5 ms |
13648 KB |
Output is correct |
11 |
Correct |
5 ms |
13648 KB |
Output is correct |
12 |
Correct |
5 ms |
13648 KB |
Output is correct |
13 |
Correct |
5 ms |
13648 KB |
Output is correct |
14 |
Correct |
5 ms |
13648 KB |
Output is correct |
15 |
Correct |
5 ms |
13648 KB |
Output is correct |
16 |
Correct |
6 ms |
13648 KB |
Output is correct |
17 |
Correct |
5 ms |
13648 KB |
Output is correct |
18 |
Correct |
6 ms |
13648 KB |
Output is correct |
19 |
Correct |
5 ms |
13648 KB |
Output is correct |
20 |
Correct |
5 ms |
13648 KB |
Output is correct |
21 |
Correct |
6 ms |
13904 KB |
Output is correct |
22 |
Correct |
6 ms |
13904 KB |
Output is correct |
23 |
Correct |
6 ms |
13904 KB |
Output is correct |
24 |
Correct |
6 ms |
13804 KB |
Output is correct |
25 |
Correct |
6 ms |
13696 KB |
Output is correct |
26 |
Correct |
6 ms |
13904 KB |
Output is correct |
27 |
Correct |
5 ms |
13904 KB |
Output is correct |
28 |
Correct |
6 ms |
13904 KB |
Output is correct |
29 |
Correct |
6 ms |
13876 KB |
Output is correct |
30 |
Correct |
6 ms |
13904 KB |
Output is correct |
31 |
Correct |
7 ms |
13904 KB |
Output is correct |
32 |
Correct |
5 ms |
13748 KB |
Output is correct |
33 |
Correct |
9 ms |
13904 KB |
Output is correct |
34 |
Correct |
8 ms |
13904 KB |
Output is correct |
35 |
Correct |
11 ms |
13904 KB |
Output is correct |
36 |
Correct |
8 ms |
13952 KB |
Output is correct |
37 |
Correct |
9 ms |
13904 KB |
Output is correct |
38 |
Correct |
8 ms |
13904 KB |
Output is correct |
39 |
Correct |
204 ms |
19536 KB |
Output is correct |
40 |
Correct |
222 ms |
19740 KB |
Output is correct |
41 |
Correct |
184 ms |
19528 KB |
Output is correct |
42 |
Correct |
136 ms |
19792 KB |
Output is correct |
43 |
Correct |
74 ms |
19784 KB |
Output is correct |
44 |
Correct |
46 ms |
19792 KB |
Output is correct |
45 |
Execution timed out |
3055 ms |
20876 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |