// In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 2e5 + 5, maxk = 1e6 + 5;
const int inf = 1e9 + 7;
ll n, k, ans = inf, sz[maxn], h[maxn], mn[maxk];
vector<pii> adj[maxn];
vector<int> vec;
bool mark[maxn];
void get_sz(int v, int p = -1){
sz[v] = 1;
for(auto [u, w]: adj[v]){
if(u != p && !mark[u]){
get_sz(u, v);
sz[v] += sz[u];
}
}
}
int find(int v, int s, int p = -1){
for(auto [u, w]: adj[v]) if(u != p && !mark[u] && sz[u] * 2 > s) return find(u, s, v);
return v;
}
void dfs(int v, int h1, int h2, int p, bool keep){
if(h2 > k) return;
if(keep){
smin(mn[h2], 1ll * h1);
vec.pb(h2);
}
else smin(ans, h1 + mn[k - h2]);
for(auto [u, w]: adj[v]) if(u != p && !mark[u]) dfs(u, h1 + 1, h2 + w, v, keep);
}
void centroid_decomp(int v = 0){
get_sz(v);
v = find(v, sz[v]);
mn[0] = 0;
for(auto [u, w]: adj[v]){
if(!mark[u]){
dfs(u, 1, w, v, 0);
dfs(u, 1, w, v, 1);
}
}
mn[0] = inf;
for(auto u: vec) mn[u] = inf;
vec.clear();
mark[v] = 1;
for(auto [u, w]: adj[v]) if(!mark[u]) centroid_decomp(u);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
fill(mn, mn + maxk, inf);
for(int i = 0; i < n - 1; i++){
int u = H[i][0], v = H[i][1], w = L[i];
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
centroid_decomp();
return (ans >= inf ? -1 : ans);
}
// int32_t main()
// {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// fill(mn, mn + maxk, inf);
// cin >> n >> k;
// for(int i = 0; i < n - 1; i++){
// int u, v, w; cin >> u >> v >> w;
// adj[u].pb(mp(v, w));
// adj[v].pb(mp(u, w));
// }
// centroid_decomp();
// cout << (ans >= inf ? -1 : ans) << '\n';
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
18776 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
6 ms |
18780 KB |
Output is correct |
5 |
Correct |
5 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
18780 KB |
Output is correct |
7 |
Correct |
5 ms |
18780 KB |
Output is correct |
8 |
Correct |
5 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18920 KB |
Output is correct |
11 |
Correct |
4 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
5 ms |
18892 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
5 ms |
19032 KB |
Output is correct |
16 |
Correct |
5 ms |
18776 KB |
Output is correct |
17 |
Correct |
5 ms |
18780 KB |
Output is correct |
18 |
Correct |
5 ms |
18868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
18776 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
6 ms |
18780 KB |
Output is correct |
5 |
Correct |
5 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
18780 KB |
Output is correct |
7 |
Correct |
5 ms |
18780 KB |
Output is correct |
8 |
Correct |
5 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18920 KB |
Output is correct |
11 |
Correct |
4 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
5 ms |
18892 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
5 ms |
19032 KB |
Output is correct |
16 |
Correct |
5 ms |
18776 KB |
Output is correct |
17 |
Correct |
5 ms |
18780 KB |
Output is correct |
18 |
Correct |
5 ms |
18868 KB |
Output is correct |
19 |
Correct |
4 ms |
18776 KB |
Output is correct |
20 |
Correct |
5 ms |
18780 KB |
Output is correct |
21 |
Correct |
5 ms |
18776 KB |
Output is correct |
22 |
Correct |
5 ms |
18776 KB |
Output is correct |
23 |
Correct |
5 ms |
18892 KB |
Output is correct |
24 |
Correct |
5 ms |
18776 KB |
Output is correct |
25 |
Correct |
4 ms |
18780 KB |
Output is correct |
26 |
Correct |
6 ms |
18780 KB |
Output is correct |
27 |
Correct |
5 ms |
18780 KB |
Output is correct |
28 |
Correct |
5 ms |
18780 KB |
Output is correct |
29 |
Correct |
5 ms |
18780 KB |
Output is correct |
30 |
Correct |
5 ms |
18780 KB |
Output is correct |
31 |
Correct |
5 ms |
18780 KB |
Output is correct |
32 |
Correct |
5 ms |
18780 KB |
Output is correct |
33 |
Correct |
5 ms |
18780 KB |
Output is correct |
34 |
Correct |
6 ms |
18776 KB |
Output is correct |
35 |
Correct |
5 ms |
18776 KB |
Output is correct |
36 |
Correct |
6 ms |
18780 KB |
Output is correct |
37 |
Correct |
5 ms |
18776 KB |
Output is correct |
38 |
Correct |
5 ms |
18780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
18776 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
6 ms |
18780 KB |
Output is correct |
5 |
Correct |
5 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
18780 KB |
Output is correct |
7 |
Correct |
5 ms |
18780 KB |
Output is correct |
8 |
Correct |
5 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18920 KB |
Output is correct |
11 |
Correct |
4 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
5 ms |
18892 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
5 ms |
19032 KB |
Output is correct |
16 |
Correct |
5 ms |
18776 KB |
Output is correct |
17 |
Correct |
5 ms |
18780 KB |
Output is correct |
18 |
Correct |
5 ms |
18868 KB |
Output is correct |
19 |
Correct |
85 ms |
25212 KB |
Output is correct |
20 |
Correct |
73 ms |
25072 KB |
Output is correct |
21 |
Correct |
82 ms |
25428 KB |
Output is correct |
22 |
Correct |
83 ms |
25656 KB |
Output is correct |
23 |
Correct |
78 ms |
25428 KB |
Output is correct |
24 |
Correct |
48 ms |
25112 KB |
Output is correct |
25 |
Correct |
83 ms |
28496 KB |
Output is correct |
26 |
Correct |
82 ms |
32840 KB |
Output is correct |
27 |
Correct |
138 ms |
29764 KB |
Output is correct |
28 |
Correct |
188 ms |
43968 KB |
Output is correct |
29 |
Correct |
235 ms |
43112 KB |
Output is correct |
30 |
Correct |
148 ms |
29560 KB |
Output is correct |
31 |
Correct |
150 ms |
29520 KB |
Output is correct |
32 |
Correct |
185 ms |
29744 KB |
Output is correct |
33 |
Correct |
185 ms |
28608 KB |
Output is correct |
34 |
Correct |
115 ms |
28572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
18776 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
6 ms |
18780 KB |
Output is correct |
5 |
Correct |
5 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
18780 KB |
Output is correct |
7 |
Correct |
5 ms |
18780 KB |
Output is correct |
8 |
Correct |
5 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18920 KB |
Output is correct |
11 |
Correct |
4 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
5 ms |
18892 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
5 ms |
19032 KB |
Output is correct |
16 |
Correct |
5 ms |
18776 KB |
Output is correct |
17 |
Correct |
5 ms |
18780 KB |
Output is correct |
18 |
Correct |
5 ms |
18868 KB |
Output is correct |
19 |
Correct |
4 ms |
18776 KB |
Output is correct |
20 |
Correct |
5 ms |
18780 KB |
Output is correct |
21 |
Correct |
5 ms |
18776 KB |
Output is correct |
22 |
Correct |
5 ms |
18776 KB |
Output is correct |
23 |
Correct |
5 ms |
18892 KB |
Output is correct |
24 |
Correct |
5 ms |
18776 KB |
Output is correct |
25 |
Correct |
4 ms |
18780 KB |
Output is correct |
26 |
Correct |
6 ms |
18780 KB |
Output is correct |
27 |
Correct |
5 ms |
18780 KB |
Output is correct |
28 |
Correct |
5 ms |
18780 KB |
Output is correct |
29 |
Correct |
5 ms |
18780 KB |
Output is correct |
30 |
Correct |
5 ms |
18780 KB |
Output is correct |
31 |
Correct |
5 ms |
18780 KB |
Output is correct |
32 |
Correct |
5 ms |
18780 KB |
Output is correct |
33 |
Correct |
5 ms |
18780 KB |
Output is correct |
34 |
Correct |
6 ms |
18776 KB |
Output is correct |
35 |
Correct |
5 ms |
18776 KB |
Output is correct |
36 |
Correct |
6 ms |
18780 KB |
Output is correct |
37 |
Correct |
5 ms |
18776 KB |
Output is correct |
38 |
Correct |
5 ms |
18780 KB |
Output is correct |
39 |
Correct |
85 ms |
25212 KB |
Output is correct |
40 |
Correct |
73 ms |
25072 KB |
Output is correct |
41 |
Correct |
82 ms |
25428 KB |
Output is correct |
42 |
Correct |
83 ms |
25656 KB |
Output is correct |
43 |
Correct |
78 ms |
25428 KB |
Output is correct |
44 |
Correct |
48 ms |
25112 KB |
Output is correct |
45 |
Correct |
83 ms |
28496 KB |
Output is correct |
46 |
Correct |
82 ms |
32840 KB |
Output is correct |
47 |
Correct |
138 ms |
29764 KB |
Output is correct |
48 |
Correct |
188 ms |
43968 KB |
Output is correct |
49 |
Correct |
235 ms |
43112 KB |
Output is correct |
50 |
Correct |
148 ms |
29560 KB |
Output is correct |
51 |
Correct |
150 ms |
29520 KB |
Output is correct |
52 |
Correct |
185 ms |
29744 KB |
Output is correct |
53 |
Correct |
185 ms |
28608 KB |
Output is correct |
54 |
Correct |
115 ms |
28572 KB |
Output is correct |
55 |
Correct |
11 ms |
19288 KB |
Output is correct |
56 |
Correct |
13 ms |
19292 KB |
Output is correct |
57 |
Correct |
54 ms |
25816 KB |
Output is correct |
58 |
Correct |
30 ms |
26064 KB |
Output is correct |
59 |
Correct |
73 ms |
32728 KB |
Output is correct |
60 |
Correct |
338 ms |
44268 KB |
Output is correct |
61 |
Correct |
163 ms |
30204 KB |
Output is correct |
62 |
Correct |
179 ms |
30624 KB |
Output is correct |
63 |
Correct |
158 ms |
30672 KB |
Output is correct |
64 |
Correct |
280 ms |
30832 KB |
Output is correct |
65 |
Correct |
139 ms |
29560 KB |
Output is correct |
66 |
Correct |
370 ms |
40272 KB |
Output is correct |
67 |
Correct |
80 ms |
31680 KB |
Output is correct |
68 |
Correct |
236 ms |
30296 KB |
Output is correct |
69 |
Correct |
280 ms |
30672 KB |
Output is correct |
70 |
Correct |
249 ms |
30160 KB |
Output is correct |