#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pii pair<ll,ll>
#include "race.h"
using namespace std;
const ll CN=1000005;
vector<pii>g[CN];
map<ll,ll>*mp[CN];
ll dist[CN],sz[CN],dep[CN];
ll res=INT_MAX;
ll k;
void dfs(ll u,ll prev){
sz[u]=1;
ll mxsz=-1,mxn=-1;
for(auto el:g[u]){
ll v=el.first;
ll c=el.second;
if(v==prev)continue;
dist[v]=dist[u]+c;
dep[v]=dep[u]+1;
dfs(v,u);
sz[u]+=sz[v];
if(sz[v]>mxsz){
mxsz=sz[v];
mxn=v;
}
}
if(mxsz!=-1)mp[u]=mp[mxn];
else mp[u]=new map<ll,ll>();
if((*mp[u]).count(dist[u]))(*mp[u])[dist[u]]=min((*mp[u])[dist[u]],dep[u]);
else (*mp[u])[dist[u]]=dep[u];
for(auto E:g[u]){
ll v=E.first;
if(v==prev ||v==mxn)continue;
for(auto el:(*mp[v])){
ll nx=el.first;
ll de=el.second;
if((*mp[u]).count(k-nx+2*dist[u])){
ll node=(*mp[u])[k-nx+2*dist[u]];
res=min(res,abs(de-dep[u])+abs(dep[u]-node));
// cout<<"CHANGE"<<nx<<' '<<k<<' '<<de<<' '<<res<<'\n';
}
}
for(auto el:(*mp[v])){
ll nx=el.first;
ll de=el.second;
if((*mp[u]).count(nx)){
(*mp[u])[nx]=min((*mp[u])[nx],de);
}else (*mp[u])[nx]=de;
}
}
/*
cout<<"NODE";
cout<<u<<' '<<prev<<' '<<dist[u]<<' '<<dep[u]<<'\n';
for(auto el:(*mp[u]))cout<<el.first<<' '<<el.second<<'\n';
*/
if((*mp[u]).count(k+dist[u])){
ll node=(*mp[u])[k+dist[u]];
//cout<<"CHANG"<<k-dist[u]<<' '<<node<<' '<<dep[u]<<' '<<u<<'\n';
res=min(res,abs(node-dep[u]));
}
}
int32_t best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[])
{
k=K;
for(int i=0;i<N-1;i++){
ll u,v,c;u=H[i][0];
v=H[i][1];
c=L[i];
g[u].push_back({v,c});
g[v].push_back({u,c});
}
for(int i=0;i<CN;i++){
dep[i]=0;
dist[i]=0;
sz[i]=0;
}
dep[1]=0;
dfs(1,N);
int ans=res;
if(ans<=N)return ans;
else return -1;
}
/*
int main(){
int n=4;
int k=7;
int h[][2]={{0,1},{1,2},{1,3}};
int l[]={1,2,4};
std::cout<<best_path(n,k,h,l)<<'\n';
}
*/
/*
int main(){
int n=11;
int k=12;
int h[][2]={{0,1},{0,2},{2,3},{3,4},{4,5},{0,6},{6,7},{6,8},{8,9},{8,10}};
int l[]={3,4,5,4,6,3,2,5,6,7};
cout<<best_path(n,k,h,l)<<'\n';
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47352 KB |
Output is correct |
2 |
Correct |
28 ms |
47360 KB |
Output is correct |
3 |
Correct |
28 ms |
47360 KB |
Output is correct |
4 |
Correct |
28 ms |
47360 KB |
Output is correct |
5 |
Correct |
29 ms |
47360 KB |
Output is correct |
6 |
Correct |
29 ms |
47268 KB |
Output is correct |
7 |
Correct |
30 ms |
47352 KB |
Output is correct |
8 |
Correct |
29 ms |
47360 KB |
Output is correct |
9 |
Correct |
28 ms |
47360 KB |
Output is correct |
10 |
Correct |
28 ms |
47360 KB |
Output is correct |
11 |
Correct |
28 ms |
47352 KB |
Output is correct |
12 |
Correct |
29 ms |
47488 KB |
Output is correct |
13 |
Correct |
28 ms |
47360 KB |
Output is correct |
14 |
Correct |
28 ms |
47352 KB |
Output is correct |
15 |
Correct |
28 ms |
47352 KB |
Output is correct |
16 |
Correct |
28 ms |
47360 KB |
Output is correct |
17 |
Correct |
28 ms |
47360 KB |
Output is correct |
18 |
Correct |
28 ms |
47352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47352 KB |
Output is correct |
2 |
Correct |
28 ms |
47360 KB |
Output is correct |
3 |
Correct |
28 ms |
47360 KB |
Output is correct |
4 |
Correct |
28 ms |
47360 KB |
Output is correct |
5 |
Correct |
29 ms |
47360 KB |
Output is correct |
6 |
Correct |
29 ms |
47268 KB |
Output is correct |
7 |
Correct |
30 ms |
47352 KB |
Output is correct |
8 |
Correct |
29 ms |
47360 KB |
Output is correct |
9 |
Correct |
28 ms |
47360 KB |
Output is correct |
10 |
Correct |
28 ms |
47360 KB |
Output is correct |
11 |
Correct |
28 ms |
47352 KB |
Output is correct |
12 |
Correct |
29 ms |
47488 KB |
Output is correct |
13 |
Correct |
28 ms |
47360 KB |
Output is correct |
14 |
Correct |
28 ms |
47352 KB |
Output is correct |
15 |
Correct |
28 ms |
47352 KB |
Output is correct |
16 |
Correct |
28 ms |
47360 KB |
Output is correct |
17 |
Correct |
28 ms |
47360 KB |
Output is correct |
18 |
Correct |
28 ms |
47352 KB |
Output is correct |
19 |
Correct |
29 ms |
47360 KB |
Output is correct |
20 |
Correct |
29 ms |
47276 KB |
Output is correct |
21 |
Correct |
30 ms |
47608 KB |
Output is correct |
22 |
Correct |
30 ms |
47616 KB |
Output is correct |
23 |
Correct |
30 ms |
47616 KB |
Output is correct |
24 |
Correct |
30 ms |
47616 KB |
Output is correct |
25 |
Correct |
30 ms |
47608 KB |
Output is correct |
26 |
Correct |
29 ms |
47616 KB |
Output is correct |
27 |
Correct |
29 ms |
47480 KB |
Output is correct |
28 |
Correct |
30 ms |
47616 KB |
Output is correct |
29 |
Correct |
30 ms |
47608 KB |
Output is correct |
30 |
Correct |
29 ms |
47616 KB |
Output is correct |
31 |
Correct |
29 ms |
47608 KB |
Output is correct |
32 |
Correct |
29 ms |
47616 KB |
Output is correct |
33 |
Correct |
30 ms |
47608 KB |
Output is correct |
34 |
Correct |
29 ms |
47488 KB |
Output is correct |
35 |
Correct |
29 ms |
47480 KB |
Output is correct |
36 |
Correct |
29 ms |
47488 KB |
Output is correct |
37 |
Correct |
29 ms |
47480 KB |
Output is correct |
38 |
Correct |
30 ms |
47480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47352 KB |
Output is correct |
2 |
Correct |
28 ms |
47360 KB |
Output is correct |
3 |
Correct |
28 ms |
47360 KB |
Output is correct |
4 |
Correct |
28 ms |
47360 KB |
Output is correct |
5 |
Correct |
29 ms |
47360 KB |
Output is correct |
6 |
Correct |
29 ms |
47268 KB |
Output is correct |
7 |
Correct |
30 ms |
47352 KB |
Output is correct |
8 |
Correct |
29 ms |
47360 KB |
Output is correct |
9 |
Correct |
28 ms |
47360 KB |
Output is correct |
10 |
Correct |
28 ms |
47360 KB |
Output is correct |
11 |
Correct |
28 ms |
47352 KB |
Output is correct |
12 |
Correct |
29 ms |
47488 KB |
Output is correct |
13 |
Correct |
28 ms |
47360 KB |
Output is correct |
14 |
Correct |
28 ms |
47352 KB |
Output is correct |
15 |
Correct |
28 ms |
47352 KB |
Output is correct |
16 |
Correct |
28 ms |
47360 KB |
Output is correct |
17 |
Correct |
28 ms |
47360 KB |
Output is correct |
18 |
Correct |
28 ms |
47352 KB |
Output is correct |
19 |
Correct |
157 ms |
68128 KB |
Output is correct |
20 |
Correct |
159 ms |
69368 KB |
Output is correct |
21 |
Correct |
159 ms |
69496 KB |
Output is correct |
22 |
Correct |
170 ms |
70136 KB |
Output is correct |
23 |
Correct |
203 ms |
83832 KB |
Output is correct |
24 |
Correct |
171 ms |
78072 KB |
Output is correct |
25 |
Correct |
142 ms |
70136 KB |
Output is correct |
26 |
Correct |
89 ms |
70904 KB |
Output is correct |
27 |
Correct |
264 ms |
83832 KB |
Output is correct |
28 |
Correct |
322 ms |
106488 KB |
Output is correct |
29 |
Correct |
341 ms |
99832 KB |
Output is correct |
30 |
Correct |
262 ms |
83980 KB |
Output is correct |
31 |
Correct |
258 ms |
83960 KB |
Output is correct |
32 |
Correct |
305 ms |
84088 KB |
Output is correct |
33 |
Correct |
279 ms |
76408 KB |
Output is correct |
34 |
Correct |
414 ms |
110328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47352 KB |
Output is correct |
2 |
Correct |
28 ms |
47360 KB |
Output is correct |
3 |
Correct |
28 ms |
47360 KB |
Output is correct |
4 |
Correct |
28 ms |
47360 KB |
Output is correct |
5 |
Correct |
29 ms |
47360 KB |
Output is correct |
6 |
Correct |
29 ms |
47268 KB |
Output is correct |
7 |
Correct |
30 ms |
47352 KB |
Output is correct |
8 |
Correct |
29 ms |
47360 KB |
Output is correct |
9 |
Correct |
28 ms |
47360 KB |
Output is correct |
10 |
Correct |
28 ms |
47360 KB |
Output is correct |
11 |
Correct |
28 ms |
47352 KB |
Output is correct |
12 |
Correct |
29 ms |
47488 KB |
Output is correct |
13 |
Correct |
28 ms |
47360 KB |
Output is correct |
14 |
Correct |
28 ms |
47352 KB |
Output is correct |
15 |
Correct |
28 ms |
47352 KB |
Output is correct |
16 |
Correct |
28 ms |
47360 KB |
Output is correct |
17 |
Correct |
28 ms |
47360 KB |
Output is correct |
18 |
Correct |
28 ms |
47352 KB |
Output is correct |
19 |
Correct |
29 ms |
47360 KB |
Output is correct |
20 |
Correct |
29 ms |
47276 KB |
Output is correct |
21 |
Correct |
30 ms |
47608 KB |
Output is correct |
22 |
Correct |
30 ms |
47616 KB |
Output is correct |
23 |
Correct |
30 ms |
47616 KB |
Output is correct |
24 |
Correct |
30 ms |
47616 KB |
Output is correct |
25 |
Correct |
30 ms |
47608 KB |
Output is correct |
26 |
Correct |
29 ms |
47616 KB |
Output is correct |
27 |
Correct |
29 ms |
47480 KB |
Output is correct |
28 |
Correct |
30 ms |
47616 KB |
Output is correct |
29 |
Correct |
30 ms |
47608 KB |
Output is correct |
30 |
Correct |
29 ms |
47616 KB |
Output is correct |
31 |
Correct |
29 ms |
47608 KB |
Output is correct |
32 |
Correct |
29 ms |
47616 KB |
Output is correct |
33 |
Correct |
30 ms |
47608 KB |
Output is correct |
34 |
Correct |
29 ms |
47488 KB |
Output is correct |
35 |
Correct |
29 ms |
47480 KB |
Output is correct |
36 |
Correct |
29 ms |
47488 KB |
Output is correct |
37 |
Correct |
29 ms |
47480 KB |
Output is correct |
38 |
Correct |
30 ms |
47480 KB |
Output is correct |
39 |
Correct |
157 ms |
68128 KB |
Output is correct |
40 |
Correct |
159 ms |
69368 KB |
Output is correct |
41 |
Correct |
159 ms |
69496 KB |
Output is correct |
42 |
Correct |
170 ms |
70136 KB |
Output is correct |
43 |
Correct |
203 ms |
83832 KB |
Output is correct |
44 |
Correct |
171 ms |
78072 KB |
Output is correct |
45 |
Correct |
142 ms |
70136 KB |
Output is correct |
46 |
Correct |
89 ms |
70904 KB |
Output is correct |
47 |
Correct |
264 ms |
83832 KB |
Output is correct |
48 |
Correct |
322 ms |
106488 KB |
Output is correct |
49 |
Correct |
341 ms |
99832 KB |
Output is correct |
50 |
Correct |
262 ms |
83980 KB |
Output is correct |
51 |
Correct |
258 ms |
83960 KB |
Output is correct |
52 |
Correct |
305 ms |
84088 KB |
Output is correct |
53 |
Correct |
279 ms |
76408 KB |
Output is correct |
54 |
Correct |
414 ms |
110328 KB |
Output is correct |
55 |
Correct |
45 ms |
50740 KB |
Output is correct |
56 |
Correct |
39 ms |
49144 KB |
Output is correct |
57 |
Correct |
104 ms |
68856 KB |
Output is correct |
58 |
Correct |
100 ms |
67540 KB |
Output is correct |
59 |
Correct |
119 ms |
77248 KB |
Output is correct |
60 |
Correct |
333 ms |
97400 KB |
Output is correct |
61 |
Correct |
294 ms |
87416 KB |
Output is correct |
62 |
Correct |
256 ms |
84088 KB |
Output is correct |
63 |
Correct |
302 ms |
83960 KB |
Output is correct |
64 |
Correct |
537 ms |
134264 KB |
Output is correct |
65 |
Correct |
535 ms |
134648 KB |
Output is correct |
66 |
Correct |
325 ms |
104696 KB |
Output is correct |
67 |
Correct |
246 ms |
88928 KB |
Output is correct |
68 |
Correct |
405 ms |
104056 KB |
Output is correct |
69 |
Correct |
404 ms |
104696 KB |
Output is correct |
70 |
Correct |
384 ms |
101624 KB |
Output is correct |