#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long int
#define pii pair <ll, int>
#define pb push_back
#define ff first
#define ss second
const int N = 2e5+5;
const int MAXN = 1e6+5;
const int inf = 1e9;
int P[MAXN];
int sub[N], rem[N], tp[N], dpt[N];
ll w[N];
int answer = inf;
vector<pii> E[N], G[N];
vector<int> nodes;
void solve(vector<int> nodes, int T, int K){
for (auto i : nodes){
G[tp[i]].pb({w[i], dpt[i]});
}
for (int i = 1; i <= T; i++){
for (auto j : G[i]){
if (j.ff <= K)
answer = min(answer, P[K-j.ff]+j.ss);
}
for (auto j : G[i]){
if (j.ff <= K) P[j.ff] = min(P[j.ff], j.ss);
}
G[i].clear();
}
for (auto i : nodes) if (w[i] <= K) P[w[i]] = inf;
}
void dfs(int nd, int pr){
sub[nd] = 1;
for (auto i : E[nd]){
if (i.ff == pr or rem[i.ff]) continue;
dfs(i.ff, nd);
sub[nd] += sub[i.ff];
}
}
int centroid(int nd, int pr, int sz){
for (auto i : E[nd]){
if (i.ff == pr or rem[i.ff]) continue;
if (sub[i.ff]*2 > sz) return centroid(i.ff, nd, sz);
}
return nd;
}
void F(int nd, int pr, ll W, int lvl, int T){
nodes.pb(nd);
w[nd] = W;
dpt[nd] = lvl;
tp[nd] = T;
for (auto i : E[nd]){
if (i.ff == pr or rem[i.ff]) continue;
F(i.ff, nd, W+i.ss, lvl+1, T);
}
}
void build(int nd, int pr, int K){
int centr = centroid(nd, 0, sub[nd]);
rem[centr] = 1;
nodes.clear();
int T = 0;
for (auto i : E[centr]){
if (rem[i.ff]) continue;
F(i.ff, 0, i.ss, 1, ++T);
}
solve(nodes, T, K);
for (auto i : E[centr]){
if (rem[i.ff]) continue;
dfs(i.ff, 0);
build(i.ff, centr, K);
}
}
int best_path(int n, int k, int H[][2], int L[]){
for (int i = 0; i < n-1; i++){
E[H[i][0]+1].pb({H[i][1]+1, L[i]});
E[H[i][1]+1].pb({H[i][0]+1, L[i]});
}
P[0] = 0;
for (int i = 1; i <= k; i++) P[i] = inf+7;
dfs(1, 0);
build(1, 0, k);
if (answer > n) answer = -1;
return answer;
}
// int main ()
// {
// ios::sync_with_stdio(false);
// cin.tie(0);
// int n, k;
// cin >> n >> k;
// int H[n][2], L[n];
// for (int i = 0; i < n-1; i++){
// cin >> H[i][0] >> H[i][1] >> L[i];
// }
// cout << best_path(n, k, H, L);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9712 KB |
Output is correct |
2 |
Correct |
6 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9708 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9688 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9712 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9708 KB |
Output is correct |
11 |
Correct |
5 ms |
9760 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9712 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9712 KB |
Output is correct |
2 |
Correct |
6 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9708 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9688 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9712 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9708 KB |
Output is correct |
11 |
Correct |
5 ms |
9760 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9712 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9720 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
9 ms |
13436 KB |
Output is correct |
23 |
Correct |
7 ms |
12804 KB |
Output is correct |
24 |
Correct |
7 ms |
13232 KB |
Output is correct |
25 |
Correct |
7 ms |
13140 KB |
Output is correct |
26 |
Correct |
6 ms |
11092 KB |
Output is correct |
27 |
Correct |
7 ms |
12920 KB |
Output is correct |
28 |
Correct |
5 ms |
10580 KB |
Output is correct |
29 |
Correct |
6 ms |
11124 KB |
Output is correct |
30 |
Correct |
9 ms |
11308 KB |
Output is correct |
31 |
Correct |
8 ms |
12372 KB |
Output is correct |
32 |
Correct |
9 ms |
12644 KB |
Output is correct |
33 |
Correct |
7 ms |
12928 KB |
Output is correct |
34 |
Correct |
7 ms |
12120 KB |
Output is correct |
35 |
Correct |
8 ms |
13012 KB |
Output is correct |
36 |
Correct |
6 ms |
13524 KB |
Output is correct |
37 |
Correct |
8 ms |
12920 KB |
Output is correct |
38 |
Correct |
6 ms |
11864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9712 KB |
Output is correct |
2 |
Correct |
6 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9708 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9688 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9712 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9708 KB |
Output is correct |
11 |
Correct |
5 ms |
9760 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9712 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
133 ms |
21304 KB |
Output is correct |
20 |
Correct |
122 ms |
21916 KB |
Output is correct |
21 |
Correct |
136 ms |
21320 KB |
Output is correct |
22 |
Correct |
105 ms |
21536 KB |
Output is correct |
23 |
Correct |
122 ms |
21748 KB |
Output is correct |
24 |
Correct |
60 ms |
20824 KB |
Output is correct |
25 |
Correct |
156 ms |
25208 KB |
Output is correct |
26 |
Correct |
88 ms |
28996 KB |
Output is correct |
27 |
Correct |
200 ms |
32840 KB |
Output is correct |
28 |
Correct |
410 ms |
47624 KB |
Output is correct |
29 |
Correct |
398 ms |
46448 KB |
Output is correct |
30 |
Correct |
167 ms |
32820 KB |
Output is correct |
31 |
Correct |
161 ms |
32848 KB |
Output is correct |
32 |
Correct |
273 ms |
32780 KB |
Output is correct |
33 |
Correct |
302 ms |
31272 KB |
Output is correct |
34 |
Correct |
264 ms |
31644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9712 KB |
Output is correct |
2 |
Correct |
6 ms |
9720 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9708 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9688 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9712 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9708 KB |
Output is correct |
11 |
Correct |
5 ms |
9760 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9712 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9720 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
9 ms |
13436 KB |
Output is correct |
23 |
Correct |
7 ms |
12804 KB |
Output is correct |
24 |
Correct |
7 ms |
13232 KB |
Output is correct |
25 |
Correct |
7 ms |
13140 KB |
Output is correct |
26 |
Correct |
6 ms |
11092 KB |
Output is correct |
27 |
Correct |
7 ms |
12920 KB |
Output is correct |
28 |
Correct |
5 ms |
10580 KB |
Output is correct |
29 |
Correct |
6 ms |
11124 KB |
Output is correct |
30 |
Correct |
9 ms |
11308 KB |
Output is correct |
31 |
Correct |
8 ms |
12372 KB |
Output is correct |
32 |
Correct |
9 ms |
12644 KB |
Output is correct |
33 |
Correct |
7 ms |
12928 KB |
Output is correct |
34 |
Correct |
7 ms |
12120 KB |
Output is correct |
35 |
Correct |
8 ms |
13012 KB |
Output is correct |
36 |
Correct |
6 ms |
13524 KB |
Output is correct |
37 |
Correct |
8 ms |
12920 KB |
Output is correct |
38 |
Correct |
6 ms |
11864 KB |
Output is correct |
39 |
Correct |
133 ms |
21304 KB |
Output is correct |
40 |
Correct |
122 ms |
21916 KB |
Output is correct |
41 |
Correct |
136 ms |
21320 KB |
Output is correct |
42 |
Correct |
105 ms |
21536 KB |
Output is correct |
43 |
Correct |
122 ms |
21748 KB |
Output is correct |
44 |
Correct |
60 ms |
20824 KB |
Output is correct |
45 |
Correct |
156 ms |
25208 KB |
Output is correct |
46 |
Correct |
88 ms |
28996 KB |
Output is correct |
47 |
Correct |
200 ms |
32840 KB |
Output is correct |
48 |
Correct |
410 ms |
47624 KB |
Output is correct |
49 |
Correct |
398 ms |
46448 KB |
Output is correct |
50 |
Correct |
167 ms |
32820 KB |
Output is correct |
51 |
Correct |
161 ms |
32848 KB |
Output is correct |
52 |
Correct |
273 ms |
32780 KB |
Output is correct |
53 |
Correct |
302 ms |
31272 KB |
Output is correct |
54 |
Correct |
264 ms |
31644 KB |
Output is correct |
55 |
Correct |
12 ms |
11048 KB |
Output is correct |
56 |
Correct |
12 ms |
10964 KB |
Output is correct |
57 |
Correct |
73 ms |
21680 KB |
Output is correct |
58 |
Correct |
42 ms |
22432 KB |
Output is correct |
59 |
Correct |
91 ms |
29664 KB |
Output is correct |
60 |
Correct |
384 ms |
50852 KB |
Output is correct |
61 |
Correct |
172 ms |
32956 KB |
Output is correct |
62 |
Correct |
158 ms |
36684 KB |
Output is correct |
63 |
Correct |
249 ms |
36740 KB |
Output is correct |
64 |
Correct |
442 ms |
35192 KB |
Output is correct |
65 |
Correct |
292 ms |
32152 KB |
Output is correct |
66 |
Correct |
448 ms |
47732 KB |
Output is correct |
67 |
Correct |
106 ms |
38460 KB |
Output is correct |
68 |
Correct |
250 ms |
36884 KB |
Output is correct |
69 |
Correct |
223 ms |
36780 KB |
Output is correct |
70 |
Correct |
237 ms |
35844 KB |
Output is correct |