#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 2e5 + 5;
const int K = 1e6;
vector<pair<int,int>> adj[N];
int sz[N];
int depth[N];
int md[K + 1];
i64 dist[N];
bool vis[N];
int k;
int ans = N + 1;
void find_size(int u,int p){
sz[u] = 1;
for(auto [v,w] : adj[u]){
if(vis[v] || v == p) continue;
find_size(v,u);
sz[u] += sz[v];
}
}
// Find Centeroid of the subtree of u
int findC(int u,int p,int S){
for(auto [v,w] : adj[u]){
if(vis[v] || v == p) continue;
if(sz[v] > S / 2){
return findC(v,u,S);
}
}
return u;
}
vector<int> ad;
vector<int> rem;
void dfs(int u,int p){
vector<vector<int>> all;
for(auto [v,w] : adj[u]){
if(v == p || vis[v]) continue;
depth[v] = depth[u] + 1;
dist[v] = dist[u] + w;
ad.push_back(v);
dfs(v,u);
if(p == -1){
all.push_back(ad);
ad.clear();
}
}
if(p != -1) return;
for(auto &vec : all){
for(auto &v: vec){
if(dist[v] <= k){
ans = min(ans,depth[v] + md[k - dist[v]]);
}
}
for(auto &v : vec){
if(k < dist[v]) continue;
md[dist[v]] = min(md[dist[v]],depth[v]);
rem.push_back(dist[v]);
}
}
}
// Initialize the subtree of u based on the array vis
void init(int u,int p){
find_size(u,p);
int C = findC(u,p,sz[u]);
vis[C] = true;
ad.clear();
depth[C] = 0;
dist[C] = 0;
md[0] = 0;
for(int &w : rem){
md[w] = N + 1;
}
rem.clear();
dfs(C,-1);
for(auto [v,w] : adj[C]){
if(vis[v]) continue;
init(v,C);
}
}
int best_path(int n,int K,int H[][2],int L[]){
for(int i = 0;i<N;i++){
vis[i] = false;
}
for(int i = 0;i <= K;i++){
md[i] = N + 1;
}
for(int i = 0;i<n - 1;i++){
adj[H[i][0]].emplace_back(H[i][1],L[i]);
adj[H[i][1]].emplace_back(H[i][0],L[i]);
}
k = K;
init(0,-1);
return (ans > n ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
4 ms |
14112 KB |
Output is correct |
4 |
Correct |
3 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13912 KB |
Output is correct |
7 |
Correct |
3 ms |
13912 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
4 ms |
13904 KB |
Output is correct |
10 |
Correct |
3 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13912 KB |
Output is correct |
12 |
Correct |
3 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13912 KB |
Output is correct |
14 |
Correct |
3 ms |
13916 KB |
Output is correct |
15 |
Correct |
3 ms |
13912 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
3 ms |
13912 KB |
Output is correct |
18 |
Correct |
3 ms |
13912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
4 ms |
14112 KB |
Output is correct |
4 |
Correct |
3 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13912 KB |
Output is correct |
7 |
Correct |
3 ms |
13912 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
4 ms |
13904 KB |
Output is correct |
10 |
Correct |
3 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13912 KB |
Output is correct |
12 |
Correct |
3 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13912 KB |
Output is correct |
14 |
Correct |
3 ms |
13916 KB |
Output is correct |
15 |
Correct |
3 ms |
13912 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
3 ms |
13912 KB |
Output is correct |
18 |
Correct |
3 ms |
13912 KB |
Output is correct |
19 |
Correct |
3 ms |
13912 KB |
Output is correct |
20 |
Correct |
3 ms |
13916 KB |
Output is correct |
21 |
Correct |
3 ms |
13916 KB |
Output is correct |
22 |
Correct |
4 ms |
16216 KB |
Output is correct |
23 |
Correct |
4 ms |
15960 KB |
Output is correct |
24 |
Correct |
4 ms |
15964 KB |
Output is correct |
25 |
Correct |
5 ms |
15964 KB |
Output is correct |
26 |
Correct |
4 ms |
15964 KB |
Output is correct |
27 |
Correct |
4 ms |
15960 KB |
Output is correct |
28 |
Correct |
4 ms |
15964 KB |
Output is correct |
29 |
Correct |
4 ms |
15960 KB |
Output is correct |
30 |
Correct |
4 ms |
15964 KB |
Output is correct |
31 |
Correct |
4 ms |
15960 KB |
Output is correct |
32 |
Correct |
4 ms |
15960 KB |
Output is correct |
33 |
Correct |
4 ms |
15960 KB |
Output is correct |
34 |
Correct |
5 ms |
15964 KB |
Output is correct |
35 |
Correct |
4 ms |
15960 KB |
Output is correct |
36 |
Correct |
4 ms |
15960 KB |
Output is correct |
37 |
Correct |
4 ms |
15960 KB |
Output is correct |
38 |
Correct |
4 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
4 ms |
14112 KB |
Output is correct |
4 |
Correct |
3 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13912 KB |
Output is correct |
7 |
Correct |
3 ms |
13912 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
4 ms |
13904 KB |
Output is correct |
10 |
Correct |
3 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13912 KB |
Output is correct |
12 |
Correct |
3 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13912 KB |
Output is correct |
14 |
Correct |
3 ms |
13916 KB |
Output is correct |
15 |
Correct |
3 ms |
13912 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
3 ms |
13912 KB |
Output is correct |
18 |
Correct |
3 ms |
13912 KB |
Output is correct |
19 |
Correct |
87 ms |
21456 KB |
Output is correct |
20 |
Correct |
86 ms |
21584 KB |
Output is correct |
21 |
Correct |
85 ms |
21712 KB |
Output is correct |
22 |
Correct |
76 ms |
22192 KB |
Output is correct |
23 |
Correct |
70 ms |
21456 KB |
Output is correct |
24 |
Correct |
47 ms |
21328 KB |
Output is correct |
25 |
Correct |
106 ms |
27868 KB |
Output is correct |
26 |
Correct |
78 ms |
29264 KB |
Output is correct |
27 |
Correct |
138 ms |
26252 KB |
Output is correct |
28 |
Correct |
289 ms |
40904 KB |
Output is correct |
29 |
Correct |
298 ms |
39812 KB |
Output is correct |
30 |
Correct |
127 ms |
26192 KB |
Output is correct |
31 |
Correct |
132 ms |
26420 KB |
Output is correct |
32 |
Correct |
148 ms |
26332 KB |
Output is correct |
33 |
Correct |
170 ms |
25748 KB |
Output is correct |
34 |
Correct |
172 ms |
25580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
4 ms |
14112 KB |
Output is correct |
4 |
Correct |
3 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13912 KB |
Output is correct |
7 |
Correct |
3 ms |
13912 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
4 ms |
13904 KB |
Output is correct |
10 |
Correct |
3 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13912 KB |
Output is correct |
12 |
Correct |
3 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13912 KB |
Output is correct |
14 |
Correct |
3 ms |
13916 KB |
Output is correct |
15 |
Correct |
3 ms |
13912 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
3 ms |
13912 KB |
Output is correct |
18 |
Correct |
3 ms |
13912 KB |
Output is correct |
19 |
Correct |
3 ms |
13912 KB |
Output is correct |
20 |
Correct |
3 ms |
13916 KB |
Output is correct |
21 |
Correct |
3 ms |
13916 KB |
Output is correct |
22 |
Correct |
4 ms |
16216 KB |
Output is correct |
23 |
Correct |
4 ms |
15960 KB |
Output is correct |
24 |
Correct |
4 ms |
15964 KB |
Output is correct |
25 |
Correct |
5 ms |
15964 KB |
Output is correct |
26 |
Correct |
4 ms |
15964 KB |
Output is correct |
27 |
Correct |
4 ms |
15960 KB |
Output is correct |
28 |
Correct |
4 ms |
15964 KB |
Output is correct |
29 |
Correct |
4 ms |
15960 KB |
Output is correct |
30 |
Correct |
4 ms |
15964 KB |
Output is correct |
31 |
Correct |
4 ms |
15960 KB |
Output is correct |
32 |
Correct |
4 ms |
15960 KB |
Output is correct |
33 |
Correct |
4 ms |
15960 KB |
Output is correct |
34 |
Correct |
5 ms |
15964 KB |
Output is correct |
35 |
Correct |
4 ms |
15960 KB |
Output is correct |
36 |
Correct |
4 ms |
15960 KB |
Output is correct |
37 |
Correct |
4 ms |
15960 KB |
Output is correct |
38 |
Correct |
4 ms |
15964 KB |
Output is correct |
39 |
Correct |
87 ms |
21456 KB |
Output is correct |
40 |
Correct |
86 ms |
21584 KB |
Output is correct |
41 |
Correct |
85 ms |
21712 KB |
Output is correct |
42 |
Correct |
76 ms |
22192 KB |
Output is correct |
43 |
Correct |
70 ms |
21456 KB |
Output is correct |
44 |
Correct |
47 ms |
21328 KB |
Output is correct |
45 |
Correct |
106 ms |
27868 KB |
Output is correct |
46 |
Correct |
78 ms |
29264 KB |
Output is correct |
47 |
Correct |
138 ms |
26252 KB |
Output is correct |
48 |
Correct |
289 ms |
40904 KB |
Output is correct |
49 |
Correct |
298 ms |
39812 KB |
Output is correct |
50 |
Correct |
127 ms |
26192 KB |
Output is correct |
51 |
Correct |
132 ms |
26420 KB |
Output is correct |
52 |
Correct |
148 ms |
26332 KB |
Output is correct |
53 |
Correct |
170 ms |
25748 KB |
Output is correct |
54 |
Correct |
172 ms |
25580 KB |
Output is correct |
55 |
Correct |
9 ms |
14424 KB |
Output is correct |
56 |
Correct |
11 ms |
14936 KB |
Output is correct |
57 |
Correct |
61 ms |
22092 KB |
Output is correct |
58 |
Correct |
36 ms |
27136 KB |
Output is correct |
59 |
Correct |
80 ms |
29372 KB |
Output is correct |
60 |
Correct |
322 ms |
43540 KB |
Output is correct |
61 |
Correct |
131 ms |
26656 KB |
Output is correct |
62 |
Correct |
127 ms |
29392 KB |
Output is correct |
63 |
Correct |
169 ms |
29376 KB |
Output is correct |
64 |
Correct |
283 ms |
29768 KB |
Output is correct |
65 |
Correct |
191 ms |
27084 KB |
Output is correct |
66 |
Correct |
341 ms |
41672 KB |
Output is correct |
67 |
Correct |
120 ms |
40628 KB |
Output is correct |
68 |
Correct |
208 ms |
29852 KB |
Output is correct |
69 |
Correct |
176 ms |
30100 KB |
Output is correct |
70 |
Correct |
163 ms |
29592 KB |
Output is correct |