#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define fr first
#define sc second
typedef long long ll;
const int MAX = 1e6 + 15;
const int INF = 0x3f3f3f3f;
int n, k, ans, sub[MAX], f[MAX];
vector<pair<int, int>> adj[MAX], update;
vector<int> fixupdate;
bool removed[MAX];
void dfsInit(int u, int p){
sub[u] = 1;
for(auto e : adj[u]){
if(e.fr == p || removed[e.fr]) continue;
dfsInit(e.fr, u);
sub[u] += sub[e.fr];
}
}
int findCentroid(ll u, ll p, ll sz){
for(auto e : adj[u]){
if(e.fr == p || removed[e.fr]) continue;
if(sub[e.fr] > sz / 2)
return findCentroid(e.fr, u, sz);
}
return u;
}
void dfsCalc(int u, int p, int edges, int length){
if(length <= k) ans = min(ans, edges + f[k - length]);
update.emplace_back(length, edges);
fixupdate.emplace_back(length);
for(auto e : adj[u]){
ll v = e.fr, w = e.sc;
if(v == p || removed[v]) continue;
if(length + w <= k)
dfsCalc(v, u, edges + 1, length + w);
}
}
void decompose(ll u){
dfsInit(u, u);
ll centroid = findCentroid(u, u, sub[u]);
removed[centroid] = true;
f[0] = 0;
for(auto e : adj[centroid]){
if(removed[e.fr]) continue;
dfsCalc(e.fr, centroid, 1, e.sc);
for(auto p : update)
if(p.fr <= k) f[p.fr] = min(f[p.fr], p.sc);
update.clear();
}
for(auto x : fixupdate) f[x] = INF;
fixupdate.clear();
for(auto e : adj[centroid])
if(!removed[e.fr]) decompose(e.fr);
}
int best_path(int _n, int _k, int h[][2], int l[]){
n = _n, k = _k;
for(int i = 0; i < n - 1; i++){
ll u = h[i][0], v = h[i][1], w = l[i];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
ans = INF;
for(int i = 0; i <= k; i++) f[i] = INF;
decompose(0);
return (ans == INF ? -1 : ans);
}
// int main(){
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cin >> n >> k;
// for(int i = 0; i < n - 1; i++)
// cin >> H[i][0] >> H[i][1] >> L[i];
// cout << best_path(n, k, H, L) << '\n';
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
13 ms |
23800 KB |
Output is correct |
3 |
Correct |
11 ms |
23800 KB |
Output is correct |
4 |
Correct |
13 ms |
23796 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23792 KB |
Output is correct |
11 |
Correct |
13 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23796 KB |
Output is correct |
14 |
Correct |
13 ms |
23796 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23796 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
13 ms |
23800 KB |
Output is correct |
3 |
Correct |
11 ms |
23800 KB |
Output is correct |
4 |
Correct |
13 ms |
23796 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23792 KB |
Output is correct |
11 |
Correct |
13 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23796 KB |
Output is correct |
14 |
Correct |
13 ms |
23796 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23796 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23800 KB |
Output is correct |
19 |
Correct |
12 ms |
23728 KB |
Output is correct |
20 |
Correct |
12 ms |
23796 KB |
Output is correct |
21 |
Correct |
12 ms |
23800 KB |
Output is correct |
22 |
Correct |
15 ms |
27604 KB |
Output is correct |
23 |
Correct |
14 ms |
27324 KB |
Output is correct |
24 |
Correct |
14 ms |
27264 KB |
Output is correct |
25 |
Correct |
15 ms |
27132 KB |
Output is correct |
26 |
Correct |
13 ms |
25172 KB |
Output is correct |
27 |
Correct |
14 ms |
26964 KB |
Output is correct |
28 |
Correct |
14 ms |
24660 KB |
Output is correct |
29 |
Correct |
14 ms |
25172 KB |
Output is correct |
30 |
Correct |
15 ms |
25284 KB |
Output is correct |
31 |
Correct |
14 ms |
26452 KB |
Output is correct |
32 |
Correct |
14 ms |
26580 KB |
Output is correct |
33 |
Correct |
14 ms |
26852 KB |
Output is correct |
34 |
Correct |
14 ms |
26112 KB |
Output is correct |
35 |
Correct |
14 ms |
26964 KB |
Output is correct |
36 |
Correct |
14 ms |
27532 KB |
Output is correct |
37 |
Correct |
15 ms |
26964 KB |
Output is correct |
38 |
Correct |
14 ms |
25808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
13 ms |
23800 KB |
Output is correct |
3 |
Correct |
11 ms |
23800 KB |
Output is correct |
4 |
Correct |
13 ms |
23796 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23792 KB |
Output is correct |
11 |
Correct |
13 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23796 KB |
Output is correct |
14 |
Correct |
13 ms |
23796 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23796 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23800 KB |
Output is correct |
19 |
Correct |
107 ms |
29616 KB |
Output is correct |
20 |
Correct |
113 ms |
29628 KB |
Output is correct |
21 |
Correct |
112 ms |
29980 KB |
Output is correct |
22 |
Correct |
111 ms |
30292 KB |
Output is correct |
23 |
Correct |
73 ms |
30796 KB |
Output is correct |
24 |
Correct |
58 ms |
30660 KB |
Output is correct |
25 |
Correct |
104 ms |
31616 KB |
Output is correct |
26 |
Correct |
89 ms |
35652 KB |
Output is correct |
27 |
Correct |
160 ms |
37588 KB |
Output is correct |
28 |
Correct |
218 ms |
42936 KB |
Output is correct |
29 |
Correct |
224 ms |
42460 KB |
Output is correct |
30 |
Correct |
162 ms |
37612 KB |
Output is correct |
31 |
Correct |
151 ms |
37604 KB |
Output is correct |
32 |
Correct |
180 ms |
37608 KB |
Output is correct |
33 |
Correct |
189 ms |
36300 KB |
Output is correct |
34 |
Correct |
196 ms |
41316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
13 ms |
23800 KB |
Output is correct |
3 |
Correct |
11 ms |
23800 KB |
Output is correct |
4 |
Correct |
13 ms |
23796 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23792 KB |
Output is correct |
11 |
Correct |
13 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23796 KB |
Output is correct |
14 |
Correct |
13 ms |
23796 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23796 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23800 KB |
Output is correct |
19 |
Correct |
12 ms |
23728 KB |
Output is correct |
20 |
Correct |
12 ms |
23796 KB |
Output is correct |
21 |
Correct |
12 ms |
23800 KB |
Output is correct |
22 |
Correct |
15 ms |
27604 KB |
Output is correct |
23 |
Correct |
14 ms |
27324 KB |
Output is correct |
24 |
Correct |
14 ms |
27264 KB |
Output is correct |
25 |
Correct |
15 ms |
27132 KB |
Output is correct |
26 |
Correct |
13 ms |
25172 KB |
Output is correct |
27 |
Correct |
14 ms |
26964 KB |
Output is correct |
28 |
Correct |
14 ms |
24660 KB |
Output is correct |
29 |
Correct |
14 ms |
25172 KB |
Output is correct |
30 |
Correct |
15 ms |
25284 KB |
Output is correct |
31 |
Correct |
14 ms |
26452 KB |
Output is correct |
32 |
Correct |
14 ms |
26580 KB |
Output is correct |
33 |
Correct |
14 ms |
26852 KB |
Output is correct |
34 |
Correct |
14 ms |
26112 KB |
Output is correct |
35 |
Correct |
14 ms |
26964 KB |
Output is correct |
36 |
Correct |
14 ms |
27532 KB |
Output is correct |
37 |
Correct |
15 ms |
26964 KB |
Output is correct |
38 |
Correct |
14 ms |
25808 KB |
Output is correct |
39 |
Correct |
107 ms |
29616 KB |
Output is correct |
40 |
Correct |
113 ms |
29628 KB |
Output is correct |
41 |
Correct |
112 ms |
29980 KB |
Output is correct |
42 |
Correct |
111 ms |
30292 KB |
Output is correct |
43 |
Correct |
73 ms |
30796 KB |
Output is correct |
44 |
Correct |
58 ms |
30660 KB |
Output is correct |
45 |
Correct |
104 ms |
31616 KB |
Output is correct |
46 |
Correct |
89 ms |
35652 KB |
Output is correct |
47 |
Correct |
160 ms |
37588 KB |
Output is correct |
48 |
Correct |
218 ms |
42936 KB |
Output is correct |
49 |
Correct |
224 ms |
42460 KB |
Output is correct |
50 |
Correct |
162 ms |
37612 KB |
Output is correct |
51 |
Correct |
151 ms |
37604 KB |
Output is correct |
52 |
Correct |
180 ms |
37608 KB |
Output is correct |
53 |
Correct |
189 ms |
36300 KB |
Output is correct |
54 |
Correct |
196 ms |
41316 KB |
Output is correct |
55 |
Correct |
19 ms |
24532 KB |
Output is correct |
56 |
Correct |
20 ms |
24572 KB |
Output is correct |
57 |
Correct |
77 ms |
31544 KB |
Output is correct |
58 |
Correct |
41 ms |
31272 KB |
Output is correct |
59 |
Correct |
90 ms |
36440 KB |
Output is correct |
60 |
Correct |
351 ms |
51640 KB |
Output is correct |
61 |
Correct |
152 ms |
37996 KB |
Output is correct |
62 |
Correct |
176 ms |
42544 KB |
Output is correct |
63 |
Correct |
229 ms |
42576 KB |
Output is correct |
64 |
Correct |
350 ms |
41940 KB |
Output is correct |
65 |
Correct |
216 ms |
42488 KB |
Output is correct |
66 |
Correct |
301 ms |
45596 KB |
Output is correct |
67 |
Correct |
107 ms |
43964 KB |
Output is correct |
68 |
Correct |
226 ms |
42752 KB |
Output is correct |
69 |
Correct |
215 ms |
42916 KB |
Output is correct |
70 |
Correct |
200 ms |
42172 KB |
Output is correct |