#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<pair<i64,int>> ad;
vector<int> rem;
void dfs(int u,int p){
vector<vector<pair<i64,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.emplace_back(dist[v],depth[v]);
dfs(v,u);
if(p == -1){
all.push_back(ad);
ad.clear();
}
}
if(p != -1) return;
for(auto &vec : all){
for(auto [w,d] : vec){
if(w <= k){
ans = min(ans,d + md[k - w]);
}
}
for(auto [w,d] : vec){
if(k < w) continue;
md[w] = min(md[w],d);
rem.push_back(w);
}
}
}
// 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 |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13912 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13912 KB |
Output is correct |
8 |
Correct |
3 ms |
13912 KB |
Output is correct |
9 |
Correct |
4 ms |
13912 KB |
Output is correct |
10 |
Correct |
4 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 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 |
13912 KB |
Output is correct |
15 |
Correct |
3 ms |
13912 KB |
Output is correct |
16 |
Correct |
4 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 |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13912 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13912 KB |
Output is correct |
8 |
Correct |
3 ms |
13912 KB |
Output is correct |
9 |
Correct |
4 ms |
13912 KB |
Output is correct |
10 |
Correct |
4 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 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 |
13912 KB |
Output is correct |
15 |
Correct |
3 ms |
13912 KB |
Output is correct |
16 |
Correct |
4 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 |
13912 KB |
Output is correct |
21 |
Correct |
4 ms |
13912 KB |
Output is correct |
22 |
Correct |
5 ms |
15960 KB |
Output is correct |
23 |
Correct |
5 ms |
15960 KB |
Output is correct |
24 |
Correct |
5 ms |
15964 KB |
Output is correct |
25 |
Correct |
4 ms |
15960 KB |
Output is correct |
26 |
Correct |
4 ms |
15960 KB |
Output is correct |
27 |
Correct |
5 ms |
15960 KB |
Output is correct |
28 |
Correct |
4 ms |
15960 KB |
Output is correct |
29 |
Correct |
4 ms |
15964 KB |
Output is correct |
30 |
Correct |
4 ms |
15960 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 |
4 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 |
15964 KB |
Output is correct |
38 |
Correct |
4 ms |
15960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13912 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13912 KB |
Output is correct |
8 |
Correct |
3 ms |
13912 KB |
Output is correct |
9 |
Correct |
4 ms |
13912 KB |
Output is correct |
10 |
Correct |
4 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 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 |
13912 KB |
Output is correct |
15 |
Correct |
3 ms |
13912 KB |
Output is correct |
16 |
Correct |
4 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 |
89 ms |
23496 KB |
Output is correct |
20 |
Correct |
88 ms |
23496 KB |
Output is correct |
21 |
Correct |
85 ms |
23752 KB |
Output is correct |
22 |
Correct |
76 ms |
24272 KB |
Output is correct |
23 |
Correct |
75 ms |
23432 KB |
Output is correct |
24 |
Correct |
51 ms |
23244 KB |
Output is correct |
25 |
Correct |
114 ms |
30928 KB |
Output is correct |
26 |
Correct |
81 ms |
31692 KB |
Output is correct |
27 |
Correct |
140 ms |
29892 KB |
Output is correct |
28 |
Correct |
301 ms |
46012 KB |
Output is correct |
29 |
Correct |
327 ms |
46012 KB |
Output is correct |
30 |
Correct |
127 ms |
29776 KB |
Output is correct |
31 |
Correct |
137 ms |
29776 KB |
Output is correct |
32 |
Correct |
179 ms |
29744 KB |
Output is correct |
33 |
Correct |
193 ms |
30248 KB |
Output is correct |
34 |
Correct |
197 ms |
30720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13912 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13912 KB |
Output is correct |
8 |
Correct |
3 ms |
13912 KB |
Output is correct |
9 |
Correct |
4 ms |
13912 KB |
Output is correct |
10 |
Correct |
4 ms |
13912 KB |
Output is correct |
11 |
Correct |
3 ms |
13916 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 |
13912 KB |
Output is correct |
15 |
Correct |
3 ms |
13912 KB |
Output is correct |
16 |
Correct |
4 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 |
13912 KB |
Output is correct |
21 |
Correct |
4 ms |
13912 KB |
Output is correct |
22 |
Correct |
5 ms |
15960 KB |
Output is correct |
23 |
Correct |
5 ms |
15960 KB |
Output is correct |
24 |
Correct |
5 ms |
15964 KB |
Output is correct |
25 |
Correct |
4 ms |
15960 KB |
Output is correct |
26 |
Correct |
4 ms |
15960 KB |
Output is correct |
27 |
Correct |
5 ms |
15960 KB |
Output is correct |
28 |
Correct |
4 ms |
15960 KB |
Output is correct |
29 |
Correct |
4 ms |
15964 KB |
Output is correct |
30 |
Correct |
4 ms |
15960 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 |
4 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 |
15964 KB |
Output is correct |
38 |
Correct |
4 ms |
15960 KB |
Output is correct |
39 |
Correct |
89 ms |
23496 KB |
Output is correct |
40 |
Correct |
88 ms |
23496 KB |
Output is correct |
41 |
Correct |
85 ms |
23752 KB |
Output is correct |
42 |
Correct |
76 ms |
24272 KB |
Output is correct |
43 |
Correct |
75 ms |
23432 KB |
Output is correct |
44 |
Correct |
51 ms |
23244 KB |
Output is correct |
45 |
Correct |
114 ms |
30928 KB |
Output is correct |
46 |
Correct |
81 ms |
31692 KB |
Output is correct |
47 |
Correct |
140 ms |
29892 KB |
Output is correct |
48 |
Correct |
301 ms |
46012 KB |
Output is correct |
49 |
Correct |
327 ms |
46012 KB |
Output is correct |
50 |
Correct |
127 ms |
29776 KB |
Output is correct |
51 |
Correct |
137 ms |
29776 KB |
Output is correct |
52 |
Correct |
179 ms |
29744 KB |
Output is correct |
53 |
Correct |
193 ms |
30248 KB |
Output is correct |
54 |
Correct |
197 ms |
30720 KB |
Output is correct |
55 |
Correct |
10 ms |
14680 KB |
Output is correct |
56 |
Correct |
9 ms |
14684 KB |
Output is correct |
57 |
Correct |
64 ms |
24264 KB |
Output is correct |
58 |
Correct |
36 ms |
28092 KB |
Output is correct |
59 |
Correct |
80 ms |
31692 KB |
Output is correct |
60 |
Correct |
342 ms |
49344 KB |
Output is correct |
61 |
Correct |
145 ms |
30288 KB |
Output is correct |
62 |
Correct |
141 ms |
32968 KB |
Output is correct |
63 |
Correct |
186 ms |
32896 KB |
Output is correct |
64 |
Correct |
321 ms |
35392 KB |
Output is correct |
65 |
Correct |
257 ms |
31876 KB |
Output is correct |
66 |
Correct |
374 ms |
48660 KB |
Output is correct |
67 |
Correct |
114 ms |
43444 KB |
Output is correct |
68 |
Correct |
217 ms |
35016 KB |
Output is correct |
69 |
Correct |
204 ms |
35312 KB |
Output is correct |
70 |
Correct |
192 ms |
34372 KB |
Output is correct |