#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define F first
#define S second
const int maxn = 2e5 + 5, maxk = 1e6 + 5;
vector <pii> adj[maxn];
int w[maxn], sz[maxn];
int h[maxn], d[maxn], mn[maxk];
int n, k;
int inf = 1e9 + 7, ans = 1e9 + 7;
bool dead[maxn];
void dfs(int v, int p = -1){
sz[v] = 1;
for(auto u : adj[v]){
if(u.F != p && !dead[u.F]){
d[u.F] = d[v] + w[u.S];
h[u.F] = h[v] + 1;
dfs(u.F, v);
sz[v] += sz[u.F];
}
}
}
int find_centroid(int v, int p, int z){
for(auto u : adj[v]){
if(u.F != p && !dead[u.F] && sz[u.F] > z / 2){
return find_centroid(u.F, v, z);
}
}
return v;
}
void get(int v, int p = -1){
if(k - d[v] >= 0 && k - d[v] < maxk - 2){
ans = min(mn[k - d[v]] + h[v], ans);
}
for(auto u : adj[v]){
if(u.F != p && !dead[u.F]){
get(u.F, v);
}
}
}
void upd(int v, int p = -1){
if(d[v] >= 0 && d[v] < maxk - 2){
mn[d[v]] = min(h[v], mn[d[v]]);
}
for(auto u : adj[v]){
if(u.F != p && !dead[u.F]){
upd(u.F, v);
}
}
}
void clr(int v, int p = -1){
if(d[v] >= 0 && d[v] < maxk - 2){
mn[d[v]] = inf;
}
for(auto u : adj[v]){
if(u.F != p && !dead[u.F]){
clr(u.F, v);
}
}
}
void solve(int v){
dfs(v);
int c = find_centroid(v, -1, sz[v]);
dead[c] = true;
h[c] = d[c] = 0;
dfs(c);
for(auto x : adj[c]){
int u = x.F;
if(!dead[u]){
get(u, c);
upd(u, c);
}
}
ans = min(ans, mn[k]);
clr(c);
for(auto x : adj[c]){
if(!dead[x.F]){
solve(x.F);
}
}
}
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 x = H[i][0], y = H[i][1];
adj[x].push_back({y, i});
adj[y].push_back({x, i});
}
for(int i = 0; i < n - 1; i ++)
w[i] = L[i];
for(int i = 0; i < maxk - 2; i ++){
mn[i] = inf;
}
solve(0);
if(ans >= inf){
ans = -1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8940 KB |
Output is correct |
2 |
Correct |
7 ms |
8940 KB |
Output is correct |
3 |
Correct |
7 ms |
8940 KB |
Output is correct |
4 |
Correct |
7 ms |
8940 KB |
Output is correct |
5 |
Correct |
7 ms |
8940 KB |
Output is correct |
6 |
Correct |
8 ms |
8940 KB |
Output is correct |
7 |
Correct |
8 ms |
8940 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8940 KB |
Output is correct |
10 |
Correct |
7 ms |
8940 KB |
Output is correct |
11 |
Correct |
7 ms |
8940 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
7 ms |
8940 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
7 ms |
8940 KB |
Output is correct |
16 |
Correct |
7 ms |
8940 KB |
Output is correct |
17 |
Correct |
7 ms |
8940 KB |
Output is correct |
18 |
Correct |
7 ms |
8940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8940 KB |
Output is correct |
2 |
Correct |
7 ms |
8940 KB |
Output is correct |
3 |
Correct |
7 ms |
8940 KB |
Output is correct |
4 |
Correct |
7 ms |
8940 KB |
Output is correct |
5 |
Correct |
7 ms |
8940 KB |
Output is correct |
6 |
Correct |
8 ms |
8940 KB |
Output is correct |
7 |
Correct |
8 ms |
8940 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8940 KB |
Output is correct |
10 |
Correct |
7 ms |
8940 KB |
Output is correct |
11 |
Correct |
7 ms |
8940 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
7 ms |
8940 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
7 ms |
8940 KB |
Output is correct |
16 |
Correct |
7 ms |
8940 KB |
Output is correct |
17 |
Correct |
7 ms |
8940 KB |
Output is correct |
18 |
Correct |
7 ms |
8940 KB |
Output is correct |
19 |
Correct |
7 ms |
8940 KB |
Output is correct |
20 |
Correct |
7 ms |
8940 KB |
Output is correct |
21 |
Correct |
8 ms |
9068 KB |
Output is correct |
22 |
Correct |
8 ms |
9196 KB |
Output is correct |
23 |
Correct |
8 ms |
9068 KB |
Output is correct |
24 |
Correct |
8 ms |
9068 KB |
Output is correct |
25 |
Correct |
8 ms |
9068 KB |
Output is correct |
26 |
Correct |
8 ms |
9068 KB |
Output is correct |
27 |
Correct |
8 ms |
9068 KB |
Output is correct |
28 |
Correct |
8 ms |
9068 KB |
Output is correct |
29 |
Correct |
8 ms |
9068 KB |
Output is correct |
30 |
Correct |
8 ms |
9068 KB |
Output is correct |
31 |
Correct |
9 ms |
9068 KB |
Output is correct |
32 |
Correct |
8 ms |
9068 KB |
Output is correct |
33 |
Correct |
8 ms |
9068 KB |
Output is correct |
34 |
Correct |
8 ms |
9068 KB |
Output is correct |
35 |
Correct |
8 ms |
9068 KB |
Output is correct |
36 |
Correct |
10 ms |
9068 KB |
Output is correct |
37 |
Correct |
8 ms |
9088 KB |
Output is correct |
38 |
Correct |
8 ms |
9068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8940 KB |
Output is correct |
2 |
Correct |
7 ms |
8940 KB |
Output is correct |
3 |
Correct |
7 ms |
8940 KB |
Output is correct |
4 |
Correct |
7 ms |
8940 KB |
Output is correct |
5 |
Correct |
7 ms |
8940 KB |
Output is correct |
6 |
Correct |
8 ms |
8940 KB |
Output is correct |
7 |
Correct |
8 ms |
8940 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8940 KB |
Output is correct |
10 |
Correct |
7 ms |
8940 KB |
Output is correct |
11 |
Correct |
7 ms |
8940 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
7 ms |
8940 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
7 ms |
8940 KB |
Output is correct |
16 |
Correct |
7 ms |
8940 KB |
Output is correct |
17 |
Correct |
7 ms |
8940 KB |
Output is correct |
18 |
Correct |
7 ms |
8940 KB |
Output is correct |
19 |
Correct |
321 ms |
15464 KB |
Output is correct |
20 |
Correct |
344 ms |
15524 KB |
Output is correct |
21 |
Correct |
328 ms |
15468 KB |
Output is correct |
22 |
Correct |
247 ms |
15724 KB |
Output is correct |
23 |
Correct |
205 ms |
15724 KB |
Output is correct |
24 |
Correct |
107 ms |
15596 KB |
Output is correct |
25 |
Correct |
272 ms |
18284 KB |
Output is correct |
26 |
Correct |
167 ms |
21356 KB |
Output is correct |
27 |
Correct |
303 ms |
22124 KB |
Output is correct |
28 |
Correct |
919 ms |
33588 KB |
Output is correct |
29 |
Correct |
1020 ms |
32620 KB |
Output is correct |
30 |
Correct |
343 ms |
22244 KB |
Output is correct |
31 |
Correct |
321 ms |
22252 KB |
Output is correct |
32 |
Correct |
501 ms |
22232 KB |
Output is correct |
33 |
Correct |
669 ms |
21100 KB |
Output is correct |
34 |
Correct |
610 ms |
21100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8940 KB |
Output is correct |
2 |
Correct |
7 ms |
8940 KB |
Output is correct |
3 |
Correct |
7 ms |
8940 KB |
Output is correct |
4 |
Correct |
7 ms |
8940 KB |
Output is correct |
5 |
Correct |
7 ms |
8940 KB |
Output is correct |
6 |
Correct |
8 ms |
8940 KB |
Output is correct |
7 |
Correct |
8 ms |
8940 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8940 KB |
Output is correct |
10 |
Correct |
7 ms |
8940 KB |
Output is correct |
11 |
Correct |
7 ms |
8940 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
7 ms |
8940 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
7 ms |
8940 KB |
Output is correct |
16 |
Correct |
7 ms |
8940 KB |
Output is correct |
17 |
Correct |
7 ms |
8940 KB |
Output is correct |
18 |
Correct |
7 ms |
8940 KB |
Output is correct |
19 |
Correct |
7 ms |
8940 KB |
Output is correct |
20 |
Correct |
7 ms |
8940 KB |
Output is correct |
21 |
Correct |
8 ms |
9068 KB |
Output is correct |
22 |
Correct |
8 ms |
9196 KB |
Output is correct |
23 |
Correct |
8 ms |
9068 KB |
Output is correct |
24 |
Correct |
8 ms |
9068 KB |
Output is correct |
25 |
Correct |
8 ms |
9068 KB |
Output is correct |
26 |
Correct |
8 ms |
9068 KB |
Output is correct |
27 |
Correct |
8 ms |
9068 KB |
Output is correct |
28 |
Correct |
8 ms |
9068 KB |
Output is correct |
29 |
Correct |
8 ms |
9068 KB |
Output is correct |
30 |
Correct |
8 ms |
9068 KB |
Output is correct |
31 |
Correct |
9 ms |
9068 KB |
Output is correct |
32 |
Correct |
8 ms |
9068 KB |
Output is correct |
33 |
Correct |
8 ms |
9068 KB |
Output is correct |
34 |
Correct |
8 ms |
9068 KB |
Output is correct |
35 |
Correct |
8 ms |
9068 KB |
Output is correct |
36 |
Correct |
10 ms |
9068 KB |
Output is correct |
37 |
Correct |
8 ms |
9088 KB |
Output is correct |
38 |
Correct |
8 ms |
9068 KB |
Output is correct |
39 |
Correct |
321 ms |
15464 KB |
Output is correct |
40 |
Correct |
344 ms |
15524 KB |
Output is correct |
41 |
Correct |
328 ms |
15468 KB |
Output is correct |
42 |
Correct |
247 ms |
15724 KB |
Output is correct |
43 |
Correct |
205 ms |
15724 KB |
Output is correct |
44 |
Correct |
107 ms |
15596 KB |
Output is correct |
45 |
Correct |
272 ms |
18284 KB |
Output is correct |
46 |
Correct |
167 ms |
21356 KB |
Output is correct |
47 |
Correct |
303 ms |
22124 KB |
Output is correct |
48 |
Correct |
919 ms |
33588 KB |
Output is correct |
49 |
Correct |
1020 ms |
32620 KB |
Output is correct |
50 |
Correct |
343 ms |
22244 KB |
Output is correct |
51 |
Correct |
321 ms |
22252 KB |
Output is correct |
52 |
Correct |
501 ms |
22232 KB |
Output is correct |
53 |
Correct |
669 ms |
21100 KB |
Output is correct |
54 |
Correct |
610 ms |
21100 KB |
Output is correct |
55 |
Correct |
18 ms |
9580 KB |
Output is correct |
56 |
Correct |
20 ms |
9580 KB |
Output is correct |
57 |
Correct |
122 ms |
15724 KB |
Output is correct |
58 |
Correct |
51 ms |
15712 KB |
Output is correct |
59 |
Correct |
135 ms |
21228 KB |
Output is correct |
60 |
Correct |
874 ms |
32876 KB |
Output is correct |
61 |
Correct |
301 ms |
22124 KB |
Output is correct |
62 |
Correct |
320 ms |
22252 KB |
Output is correct |
63 |
Correct |
480 ms |
22124 KB |
Output is correct |
64 |
Correct |
1053 ms |
22124 KB |
Output is correct |
65 |
Correct |
738 ms |
26092 KB |
Output is correct |
66 |
Correct |
904 ms |
33644 KB |
Output is correct |
67 |
Correct |
172 ms |
25820 KB |
Output is correct |
68 |
Correct |
511 ms |
25836 KB |
Output is correct |
69 |
Correct |
492 ms |
26092 KB |
Output is correct |
70 |
Correct |
459 ms |
25196 KB |
Output is correct |