#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
#include<stack>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<ll,ll>
#define pppiiii pair<pii,pii>
#define ppii pair<ll,pii>
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
const int mxn=1e6+1;
ll inf=1e9+10;
vector<pii>adj[mxn+10];
ll sz[mxn+10],n,k,mn[mxn+10];
ll ans=inf;
bitset<mxn+10>del;
vector<ll>in;
void dfs(int cur,int p){
sz[cur]=1;
for(auto i:adj[cur])if(i.f!=p&&(!del[i.f]))dfs(i.f,cur),sz[cur]+=sz[i.f];
}
int getcen(int cur,int p,int g){
for(auto i:adj[cur]){
if(i.f==p||del[i.f])continue;
if(sz[i.f]*2>g)return getcen(i.f,cur,g);
}
return cur;
}
void solve(int cur,int p,ll d,ll di){
if(di>k)return;
if(mn[k-di]!=inf)ans=min(ans,mn[k-di]+d);
for(auto i:adj[cur]){
if(i.f==p||del[i.f])continue;
solve(i.f,cur,d+1,di+i.s);
}
}
void upd(int cur,int p,ll d,ll di){
if(di>k)return;
mn[di]=min(mn[di],d);
in.pb(di);
for(auto i:adj[cur]){
if(i.f==p||del[i.f])continue;
upd(i.f,cur,d+1,di+i.s);
}
}
void decomp(int cur){
dfs(cur,-1);
int node=getcen(cur,-1,sz[cur]);
in.clear();
for(auto i:adj[node]){
if(del[i.f])continue;
solve(i.f,node,1,i.s);
upd(i.f,node,1,i.s);
}
del[node]=true;
for(auto i:in)mn[i]=inf;
for(auto i:adj[node])if(!del[i.f])decomp(i.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++){
adj[H[i][0]].pb({H[i][1],L[i]});
adj[H[i][1]].pb({H[i][0],L[i]});
}
fill(mn,mn+mxn+1,inf);
mn[0]=0;
decomp(0);
if(ans==inf)ans=-1;
return ans;
}/*
int main(){
cin>>n>>k;
int h[n][2],l[n];
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 |
7 ms |
36956 KB |
Output is correct |
2 |
Correct |
8 ms |
36956 KB |
Output is correct |
3 |
Correct |
8 ms |
36956 KB |
Output is correct |
4 |
Correct |
9 ms |
36960 KB |
Output is correct |
5 |
Correct |
8 ms |
37208 KB |
Output is correct |
6 |
Correct |
8 ms |
36952 KB |
Output is correct |
7 |
Correct |
7 ms |
36956 KB |
Output is correct |
8 |
Correct |
7 ms |
36956 KB |
Output is correct |
9 |
Correct |
7 ms |
37048 KB |
Output is correct |
10 |
Correct |
8 ms |
36956 KB |
Output is correct |
11 |
Correct |
7 ms |
36956 KB |
Output is correct |
12 |
Correct |
8 ms |
36956 KB |
Output is correct |
13 |
Correct |
7 ms |
37084 KB |
Output is correct |
14 |
Correct |
7 ms |
36952 KB |
Output is correct |
15 |
Correct |
8 ms |
36956 KB |
Output is correct |
16 |
Correct |
8 ms |
36960 KB |
Output is correct |
17 |
Correct |
7 ms |
36956 KB |
Output is correct |
18 |
Correct |
7 ms |
37056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
36956 KB |
Output is correct |
2 |
Correct |
8 ms |
36956 KB |
Output is correct |
3 |
Correct |
8 ms |
36956 KB |
Output is correct |
4 |
Correct |
9 ms |
36960 KB |
Output is correct |
5 |
Correct |
8 ms |
37208 KB |
Output is correct |
6 |
Correct |
8 ms |
36952 KB |
Output is correct |
7 |
Correct |
7 ms |
36956 KB |
Output is correct |
8 |
Correct |
7 ms |
36956 KB |
Output is correct |
9 |
Correct |
7 ms |
37048 KB |
Output is correct |
10 |
Correct |
8 ms |
36956 KB |
Output is correct |
11 |
Correct |
7 ms |
36956 KB |
Output is correct |
12 |
Correct |
8 ms |
36956 KB |
Output is correct |
13 |
Correct |
7 ms |
37084 KB |
Output is correct |
14 |
Correct |
7 ms |
36952 KB |
Output is correct |
15 |
Correct |
8 ms |
36956 KB |
Output is correct |
16 |
Correct |
8 ms |
36960 KB |
Output is correct |
17 |
Correct |
7 ms |
36956 KB |
Output is correct |
18 |
Correct |
7 ms |
37056 KB |
Output is correct |
19 |
Correct |
7 ms |
36956 KB |
Output is correct |
20 |
Correct |
8 ms |
36956 KB |
Output is correct |
21 |
Correct |
8 ms |
37212 KB |
Output is correct |
22 |
Correct |
8 ms |
37212 KB |
Output is correct |
23 |
Correct |
8 ms |
37212 KB |
Output is correct |
24 |
Correct |
8 ms |
37212 KB |
Output is correct |
25 |
Correct |
8 ms |
37212 KB |
Output is correct |
26 |
Correct |
8 ms |
37212 KB |
Output is correct |
27 |
Correct |
8 ms |
37212 KB |
Output is correct |
28 |
Correct |
8 ms |
37252 KB |
Output is correct |
29 |
Correct |
8 ms |
37464 KB |
Output is correct |
30 |
Correct |
8 ms |
37248 KB |
Output is correct |
31 |
Correct |
9 ms |
37212 KB |
Output is correct |
32 |
Correct |
8 ms |
37248 KB |
Output is correct |
33 |
Correct |
8 ms |
37208 KB |
Output is correct |
34 |
Correct |
8 ms |
37212 KB |
Output is correct |
35 |
Correct |
8 ms |
37212 KB |
Output is correct |
36 |
Correct |
8 ms |
37212 KB |
Output is correct |
37 |
Correct |
8 ms |
37064 KB |
Output is correct |
38 |
Correct |
9 ms |
37244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
36956 KB |
Output is correct |
2 |
Correct |
8 ms |
36956 KB |
Output is correct |
3 |
Correct |
8 ms |
36956 KB |
Output is correct |
4 |
Correct |
9 ms |
36960 KB |
Output is correct |
5 |
Correct |
8 ms |
37208 KB |
Output is correct |
6 |
Correct |
8 ms |
36952 KB |
Output is correct |
7 |
Correct |
7 ms |
36956 KB |
Output is correct |
8 |
Correct |
7 ms |
36956 KB |
Output is correct |
9 |
Correct |
7 ms |
37048 KB |
Output is correct |
10 |
Correct |
8 ms |
36956 KB |
Output is correct |
11 |
Correct |
7 ms |
36956 KB |
Output is correct |
12 |
Correct |
8 ms |
36956 KB |
Output is correct |
13 |
Correct |
7 ms |
37084 KB |
Output is correct |
14 |
Correct |
7 ms |
36952 KB |
Output is correct |
15 |
Correct |
8 ms |
36956 KB |
Output is correct |
16 |
Correct |
8 ms |
36960 KB |
Output is correct |
17 |
Correct |
7 ms |
36956 KB |
Output is correct |
18 |
Correct |
7 ms |
37056 KB |
Output is correct |
19 |
Correct |
96 ms |
46752 KB |
Output is correct |
20 |
Correct |
90 ms |
46652 KB |
Output is correct |
21 |
Correct |
91 ms |
47060 KB |
Output is correct |
22 |
Correct |
82 ms |
47572 KB |
Output is correct |
23 |
Correct |
56 ms |
46936 KB |
Output is correct |
24 |
Correct |
46 ms |
45908 KB |
Output is correct |
25 |
Correct |
89 ms |
50120 KB |
Output is correct |
26 |
Correct |
74 ms |
56272 KB |
Output is correct |
27 |
Correct |
128 ms |
55116 KB |
Output is correct |
28 |
Correct |
181 ms |
69496 KB |
Output is correct |
29 |
Correct |
163 ms |
68200 KB |
Output is correct |
30 |
Correct |
117 ms |
55124 KB |
Output is correct |
31 |
Correct |
124 ms |
55220 KB |
Output is correct |
32 |
Correct |
127 ms |
55120 KB |
Output is correct |
33 |
Correct |
136 ms |
53940 KB |
Output is correct |
34 |
Correct |
117 ms |
54612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
36956 KB |
Output is correct |
2 |
Correct |
8 ms |
36956 KB |
Output is correct |
3 |
Correct |
8 ms |
36956 KB |
Output is correct |
4 |
Correct |
9 ms |
36960 KB |
Output is correct |
5 |
Correct |
8 ms |
37208 KB |
Output is correct |
6 |
Correct |
8 ms |
36952 KB |
Output is correct |
7 |
Correct |
7 ms |
36956 KB |
Output is correct |
8 |
Correct |
7 ms |
36956 KB |
Output is correct |
9 |
Correct |
7 ms |
37048 KB |
Output is correct |
10 |
Correct |
8 ms |
36956 KB |
Output is correct |
11 |
Correct |
7 ms |
36956 KB |
Output is correct |
12 |
Correct |
8 ms |
36956 KB |
Output is correct |
13 |
Correct |
7 ms |
37084 KB |
Output is correct |
14 |
Correct |
7 ms |
36952 KB |
Output is correct |
15 |
Correct |
8 ms |
36956 KB |
Output is correct |
16 |
Correct |
8 ms |
36960 KB |
Output is correct |
17 |
Correct |
7 ms |
36956 KB |
Output is correct |
18 |
Correct |
7 ms |
37056 KB |
Output is correct |
19 |
Correct |
7 ms |
36956 KB |
Output is correct |
20 |
Correct |
8 ms |
36956 KB |
Output is correct |
21 |
Correct |
8 ms |
37212 KB |
Output is correct |
22 |
Correct |
8 ms |
37212 KB |
Output is correct |
23 |
Correct |
8 ms |
37212 KB |
Output is correct |
24 |
Correct |
8 ms |
37212 KB |
Output is correct |
25 |
Correct |
8 ms |
37212 KB |
Output is correct |
26 |
Correct |
8 ms |
37212 KB |
Output is correct |
27 |
Correct |
8 ms |
37212 KB |
Output is correct |
28 |
Correct |
8 ms |
37252 KB |
Output is correct |
29 |
Correct |
8 ms |
37464 KB |
Output is correct |
30 |
Correct |
8 ms |
37248 KB |
Output is correct |
31 |
Correct |
9 ms |
37212 KB |
Output is correct |
32 |
Correct |
8 ms |
37248 KB |
Output is correct |
33 |
Correct |
8 ms |
37208 KB |
Output is correct |
34 |
Correct |
8 ms |
37212 KB |
Output is correct |
35 |
Correct |
8 ms |
37212 KB |
Output is correct |
36 |
Correct |
8 ms |
37212 KB |
Output is correct |
37 |
Correct |
8 ms |
37064 KB |
Output is correct |
38 |
Correct |
9 ms |
37244 KB |
Output is correct |
39 |
Correct |
96 ms |
46752 KB |
Output is correct |
40 |
Correct |
90 ms |
46652 KB |
Output is correct |
41 |
Correct |
91 ms |
47060 KB |
Output is correct |
42 |
Correct |
82 ms |
47572 KB |
Output is correct |
43 |
Correct |
56 ms |
46936 KB |
Output is correct |
44 |
Correct |
46 ms |
45908 KB |
Output is correct |
45 |
Correct |
89 ms |
50120 KB |
Output is correct |
46 |
Correct |
74 ms |
56272 KB |
Output is correct |
47 |
Correct |
128 ms |
55116 KB |
Output is correct |
48 |
Correct |
181 ms |
69496 KB |
Output is correct |
49 |
Correct |
163 ms |
68200 KB |
Output is correct |
50 |
Correct |
117 ms |
55124 KB |
Output is correct |
51 |
Correct |
124 ms |
55220 KB |
Output is correct |
52 |
Correct |
127 ms |
55120 KB |
Output is correct |
53 |
Correct |
136 ms |
53940 KB |
Output is correct |
54 |
Correct |
117 ms |
54612 KB |
Output is correct |
55 |
Correct |
13 ms |
37976 KB |
Output is correct |
56 |
Correct |
14 ms |
37980 KB |
Output is correct |
57 |
Correct |
62 ms |
49480 KB |
Output is correct |
58 |
Correct |
34 ms |
48832 KB |
Output is correct |
59 |
Correct |
77 ms |
56456 KB |
Output is correct |
60 |
Correct |
309 ms |
70596 KB |
Output is correct |
61 |
Correct |
150 ms |
56136 KB |
Output is correct |
62 |
Correct |
134 ms |
57288 KB |
Output is correct |
63 |
Correct |
151 ms |
57372 KB |
Output is correct |
64 |
Correct |
307 ms |
57284 KB |
Output is correct |
65 |
Correct |
125 ms |
55380 KB |
Output is correct |
66 |
Correct |
283 ms |
65988 KB |
Output is correct |
67 |
Correct |
89 ms |
58156 KB |
Output is correct |
68 |
Correct |
149 ms |
56492 KB |
Output is correct |
69 |
Correct |
172 ms |
56200 KB |
Output is correct |
70 |
Correct |
139 ms |
55776 KB |
Output is correct |