#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6+5;
const int maxn=2e5+5;
int cnt[N],ans = 1e9;
int sub[maxn],k;
bool done[maxn];
vector<int>v;
vector<pair<int,int>>E[maxn];
void dfs(int nd,int pr){
sub[nd] = 1;
for(auto i : E[nd]) if(i.first != pr and !done[i.first]) dfs(i.first,nd), sub[nd] += sub[i.first];
}
int cent(int nd){
int sz = sub[nd];
while(1){
pair<int,int>mx = {0,0};
for(auto i : E[nd]){
if(!done[i.first] and sub[i.first] < sub[nd] and sub[i.first] > mx.first) mx = {sub[i.first],i.first};
}
if(mx.first * 2 <= sz) return nd;
nd = mx.second;
}
}
void calc(int nd,int pr,int lvl,ll ds){
if(ds <= k){
assert(k-ds < N and k-ds >= 0);
ans = min(ans,cnt[k-ds] + lvl);
}
for(auto i : E[nd])
if(i.first != pr and !done[i.first]) calc(i.first,nd,lvl+1,ds+i.second);
}
void put(int nd,int pr,int lvl,ll ds){
if(ds <= k){
assert(ds < N and ds >= 0);
cnt[ds] = min(cnt[ds],lvl);
v.push_back(ds);
}
for(auto i : E[nd])
if(i.first != pr and !done[i.first]) put(i.first,nd,lvl+1,i.second+ds);
}
void f(int nd){
dfs(nd,-1);
int cen = cent(nd);
done[cen] = 1;
sub[cen] = sub[nd];
// cout<<cen<<' ';
for(auto i : E[cen]){
if(!done[i.first]){
// if(cen == 1) cout<<i.first<<' ';
calc(i.first,cen,1,i.second);
put(i.first,cen,1,i.second);
}
}
for(auto i : v) cnt[i] = 1e9;
v.clear();
for(auto i : E[cen])
if(!done[i.first]) f(i.first);
}
int best_path(int n,int K,int h[][2],int l[]){
k = K;
for(int i = 0;i < n-1;i++){
E[h[i][0]].push_back({h[i][1],l[i]});
E[h[i][1]].push_back({h[i][0],l[i]});
}
for(int i = 1;i < N;i++) cnt[i] = 1e9;
f(0);
// cout<<'\n';
if(ans == 1e9) ans = -1;
return ans;
}
// int main(){
// ios::sync_with_stdio(false);
// cin.tie(0);
// int n,k;
// cin >> n >> k;
// int H[n-1][2],L[n-1];
// for(int i = 0;i < n-1;i++) cin >> H[i][0] >> H[i][1] >> L[i];
// cout<<best_path(n,k,H,L);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8848 KB |
Output is correct |
3 |
Correct |
6 ms |
8852 KB |
Output is correct |
4 |
Correct |
6 ms |
8848 KB |
Output is correct |
5 |
Correct |
6 ms |
8852 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
7 ms |
8908 KB |
Output is correct |
10 |
Correct |
6 ms |
8908 KB |
Output is correct |
11 |
Correct |
6 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8908 KB |
Output is correct |
13 |
Correct |
6 ms |
8852 KB |
Output is correct |
14 |
Correct |
6 ms |
8852 KB |
Output is correct |
15 |
Correct |
7 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
7 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8848 KB |
Output is correct |
3 |
Correct |
6 ms |
8852 KB |
Output is correct |
4 |
Correct |
6 ms |
8848 KB |
Output is correct |
5 |
Correct |
6 ms |
8852 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
7 ms |
8908 KB |
Output is correct |
10 |
Correct |
6 ms |
8908 KB |
Output is correct |
11 |
Correct |
6 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8908 KB |
Output is correct |
13 |
Correct |
6 ms |
8852 KB |
Output is correct |
14 |
Correct |
6 ms |
8852 KB |
Output is correct |
15 |
Correct |
7 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
7 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
6 ms |
8852 KB |
Output is correct |
21 |
Correct |
6 ms |
8908 KB |
Output is correct |
22 |
Correct |
7 ms |
8992 KB |
Output is correct |
23 |
Correct |
7 ms |
8984 KB |
Output is correct |
24 |
Correct |
7 ms |
8908 KB |
Output is correct |
25 |
Correct |
7 ms |
8908 KB |
Output is correct |
26 |
Correct |
7 ms |
8908 KB |
Output is correct |
27 |
Correct |
7 ms |
8908 KB |
Output is correct |
28 |
Correct |
7 ms |
8996 KB |
Output is correct |
29 |
Correct |
7 ms |
8916 KB |
Output is correct |
30 |
Correct |
7 ms |
8908 KB |
Output is correct |
31 |
Correct |
8 ms |
8908 KB |
Output is correct |
32 |
Correct |
9 ms |
8908 KB |
Output is correct |
33 |
Correct |
8 ms |
8908 KB |
Output is correct |
34 |
Correct |
7 ms |
8908 KB |
Output is correct |
35 |
Correct |
7 ms |
8988 KB |
Output is correct |
36 |
Correct |
7 ms |
8988 KB |
Output is correct |
37 |
Correct |
9 ms |
8908 KB |
Output is correct |
38 |
Correct |
7 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8848 KB |
Output is correct |
3 |
Correct |
6 ms |
8852 KB |
Output is correct |
4 |
Correct |
6 ms |
8848 KB |
Output is correct |
5 |
Correct |
6 ms |
8852 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
7 ms |
8908 KB |
Output is correct |
10 |
Correct |
6 ms |
8908 KB |
Output is correct |
11 |
Correct |
6 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8908 KB |
Output is correct |
13 |
Correct |
6 ms |
8852 KB |
Output is correct |
14 |
Correct |
6 ms |
8852 KB |
Output is correct |
15 |
Correct |
7 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
7 ms |
8908 KB |
Output is correct |
19 |
Correct |
222 ms |
15696 KB |
Output is correct |
20 |
Correct |
207 ms |
15756 KB |
Output is correct |
21 |
Correct |
176 ms |
15864 KB |
Output is correct |
22 |
Correct |
187 ms |
16156 KB |
Output is correct |
23 |
Correct |
169 ms |
15920 KB |
Output is correct |
24 |
Correct |
79 ms |
15732 KB |
Output is correct |
25 |
Correct |
196 ms |
19200 KB |
Output is correct |
26 |
Correct |
125 ms |
23476 KB |
Output is correct |
27 |
Correct |
259 ms |
22748 KB |
Output is correct |
28 |
Correct |
574 ms |
37104 KB |
Output is correct |
29 |
Correct |
527 ms |
35972 KB |
Output is correct |
30 |
Correct |
239 ms |
22788 KB |
Output is correct |
31 |
Correct |
254 ms |
22868 KB |
Output is correct |
32 |
Correct |
354 ms |
22828 KB |
Output is correct |
33 |
Correct |
377 ms |
21640 KB |
Output is correct |
34 |
Correct |
381 ms |
22452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8848 KB |
Output is correct |
3 |
Correct |
6 ms |
8852 KB |
Output is correct |
4 |
Correct |
6 ms |
8848 KB |
Output is correct |
5 |
Correct |
6 ms |
8852 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
7 ms |
8908 KB |
Output is correct |
10 |
Correct |
6 ms |
8908 KB |
Output is correct |
11 |
Correct |
6 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8908 KB |
Output is correct |
13 |
Correct |
6 ms |
8852 KB |
Output is correct |
14 |
Correct |
6 ms |
8852 KB |
Output is correct |
15 |
Correct |
7 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
7 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
6 ms |
8852 KB |
Output is correct |
21 |
Correct |
6 ms |
8908 KB |
Output is correct |
22 |
Correct |
7 ms |
8992 KB |
Output is correct |
23 |
Correct |
7 ms |
8984 KB |
Output is correct |
24 |
Correct |
7 ms |
8908 KB |
Output is correct |
25 |
Correct |
7 ms |
8908 KB |
Output is correct |
26 |
Correct |
7 ms |
8908 KB |
Output is correct |
27 |
Correct |
7 ms |
8908 KB |
Output is correct |
28 |
Correct |
7 ms |
8996 KB |
Output is correct |
29 |
Correct |
7 ms |
8916 KB |
Output is correct |
30 |
Correct |
7 ms |
8908 KB |
Output is correct |
31 |
Correct |
8 ms |
8908 KB |
Output is correct |
32 |
Correct |
9 ms |
8908 KB |
Output is correct |
33 |
Correct |
8 ms |
8908 KB |
Output is correct |
34 |
Correct |
7 ms |
8908 KB |
Output is correct |
35 |
Correct |
7 ms |
8988 KB |
Output is correct |
36 |
Correct |
7 ms |
8988 KB |
Output is correct |
37 |
Correct |
9 ms |
8908 KB |
Output is correct |
38 |
Correct |
7 ms |
8908 KB |
Output is correct |
39 |
Correct |
222 ms |
15696 KB |
Output is correct |
40 |
Correct |
207 ms |
15756 KB |
Output is correct |
41 |
Correct |
176 ms |
15864 KB |
Output is correct |
42 |
Correct |
187 ms |
16156 KB |
Output is correct |
43 |
Correct |
169 ms |
15920 KB |
Output is correct |
44 |
Correct |
79 ms |
15732 KB |
Output is correct |
45 |
Correct |
196 ms |
19200 KB |
Output is correct |
46 |
Correct |
125 ms |
23476 KB |
Output is correct |
47 |
Correct |
259 ms |
22748 KB |
Output is correct |
48 |
Correct |
574 ms |
37104 KB |
Output is correct |
49 |
Correct |
527 ms |
35972 KB |
Output is correct |
50 |
Correct |
239 ms |
22788 KB |
Output is correct |
51 |
Correct |
254 ms |
22868 KB |
Output is correct |
52 |
Correct |
354 ms |
22828 KB |
Output is correct |
53 |
Correct |
377 ms |
21640 KB |
Output is correct |
54 |
Correct |
381 ms |
22452 KB |
Output is correct |
55 |
Correct |
18 ms |
9548 KB |
Output is correct |
56 |
Correct |
15 ms |
9680 KB |
Output is correct |
57 |
Correct |
100 ms |
16488 KB |
Output is correct |
58 |
Correct |
44 ms |
16420 KB |
Output is correct |
59 |
Correct |
109 ms |
23400 KB |
Output is correct |
60 |
Correct |
666 ms |
37272 KB |
Output is correct |
61 |
Correct |
246 ms |
23056 KB |
Output is correct |
62 |
Correct |
256 ms |
23816 KB |
Output is correct |
63 |
Correct |
346 ms |
23744 KB |
Output is correct |
64 |
Correct |
726 ms |
24124 KB |
Output is correct |
65 |
Correct |
530 ms |
23704 KB |
Output is correct |
66 |
Correct |
701 ms |
33684 KB |
Output is correct |
67 |
Correct |
172 ms |
25224 KB |
Output is correct |
68 |
Correct |
393 ms |
23956 KB |
Output is correct |
69 |
Correct |
397 ms |
24252 KB |
Output is correct |
70 |
Correct |
359 ms |
23360 KB |
Output is correct |