#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
const int N=200000;
const int K=1000000;
vector<array<int,2>> v[N];
int root=-1;
int sz[N];
int c[N][18];
ll d[N][18];
int lvl[N];
bool vis[N];
vector<int> ch[N];
vector<int> st[N];
int best[K+1];
int res=N;
int k;
void chmin(int &a,int b){a=min(a,b);}
void find_sz(int a,int p)
{
sz[a]=1;
for(auto [to,l]:v[a])
{
if(to==p||vis[to]) continue;
find_sz(to,a);
sz[a]+=sz[to];
}
}
int find_centroid(int a,int p,int n)
{
for(auto [to,l]:v[a]) if(to!=p&&!vis[to]&&sz[to]>n/2) return find_centroid(to,a,n);
return a;
}
void find_d(int a,int p,int level)
{
for(auto [to,l]:v[a])
{
if(to==p||vis[to]) continue;
c[to][level]=c[a][level]+1;
d[to][level]=d[a][level]+l;
find_d(to,a,level);
}
}
void build_cd(int a,int p)
{
find_sz(a,-1);
int centroid=find_centroid(a,-1,sz[a]);
lvl[centroid]=(p!=-1?lvl[p]+1:0);
if(p!=-1) ch[p].push_back(centroid);
else root=centroid;
find_d(centroid,-1,lvl[centroid]);
vis[centroid]=1;
for(auto [to,l]:v[centroid]) if(!vis[to]) build_cd(to,centroid);
}
void go(int a)
{
st[a].push_back(a);
for(int b:ch[a])
{
go(b);
for(int to:st[b]) st[a].push_back(to);
}
int l=lvl[a];
best[0]=0;
for(int b:ch[a])
{
for(int to:st[b]) if(d[to][l]<=k&&best[k-d[to][l]]!=-1) chmin(res,c[to][l]+best[k-d[to][l]]);
for(int to:st[b]) if(d[to][l]<=k&&(best[d[to][l]]==-1||c[to][l]<best[d[to][l]])) best[d[to][l]]=c[to][l];
}
for(int x:st[a]) if(d[x][l]<=k) best[d[x][l]]=-1;
}
int best_path(int n,int tk,int h[][2],int l[])
{
for(int i=0;i<n-1;i++)
{
int a=h[i][0],b=h[i][1],w=l[i];
v[a].push_back({b,w});
v[b].push_back({a,w});
}
k=tk;
build_cd(0,-1);
memset(best,255,sizeof(best));
go(root);
if(res==N) res=-1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18388 KB |
Output is correct |
2 |
Correct |
9 ms |
18280 KB |
Output is correct |
3 |
Correct |
9 ms |
18388 KB |
Output is correct |
4 |
Correct |
9 ms |
18384 KB |
Output is correct |
5 |
Correct |
9 ms |
18396 KB |
Output is correct |
6 |
Correct |
9 ms |
18388 KB |
Output is correct |
7 |
Correct |
9 ms |
18384 KB |
Output is correct |
8 |
Correct |
10 ms |
18388 KB |
Output is correct |
9 |
Correct |
10 ms |
18388 KB |
Output is correct |
10 |
Correct |
10 ms |
18388 KB |
Output is correct |
11 |
Correct |
10 ms |
18388 KB |
Output is correct |
12 |
Correct |
9 ms |
18388 KB |
Output is correct |
13 |
Correct |
11 ms |
18388 KB |
Output is correct |
14 |
Correct |
10 ms |
18388 KB |
Output is correct |
15 |
Correct |
9 ms |
18376 KB |
Output is correct |
16 |
Correct |
9 ms |
18384 KB |
Output is correct |
17 |
Correct |
11 ms |
18388 KB |
Output is correct |
18 |
Correct |
9 ms |
18388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18388 KB |
Output is correct |
2 |
Correct |
9 ms |
18280 KB |
Output is correct |
3 |
Correct |
9 ms |
18388 KB |
Output is correct |
4 |
Correct |
9 ms |
18384 KB |
Output is correct |
5 |
Correct |
9 ms |
18396 KB |
Output is correct |
6 |
Correct |
9 ms |
18388 KB |
Output is correct |
7 |
Correct |
9 ms |
18384 KB |
Output is correct |
8 |
Correct |
10 ms |
18388 KB |
Output is correct |
9 |
Correct |
10 ms |
18388 KB |
Output is correct |
10 |
Correct |
10 ms |
18388 KB |
Output is correct |
11 |
Correct |
10 ms |
18388 KB |
Output is correct |
12 |
Correct |
9 ms |
18388 KB |
Output is correct |
13 |
Correct |
11 ms |
18388 KB |
Output is correct |
14 |
Correct |
10 ms |
18388 KB |
Output is correct |
15 |
Correct |
9 ms |
18376 KB |
Output is correct |
16 |
Correct |
9 ms |
18384 KB |
Output is correct |
17 |
Correct |
11 ms |
18388 KB |
Output is correct |
18 |
Correct |
9 ms |
18388 KB |
Output is correct |
19 |
Correct |
9 ms |
18308 KB |
Output is correct |
20 |
Correct |
10 ms |
18388 KB |
Output is correct |
21 |
Correct |
10 ms |
18644 KB |
Output is correct |
22 |
Correct |
11 ms |
18672 KB |
Output is correct |
23 |
Correct |
10 ms |
18644 KB |
Output is correct |
24 |
Correct |
14 ms |
18592 KB |
Output is correct |
25 |
Correct |
10 ms |
18652 KB |
Output is correct |
26 |
Correct |
12 ms |
18660 KB |
Output is correct |
27 |
Correct |
10 ms |
18644 KB |
Output is correct |
28 |
Correct |
10 ms |
18712 KB |
Output is correct |
29 |
Correct |
10 ms |
18652 KB |
Output is correct |
30 |
Correct |
11 ms |
18644 KB |
Output is correct |
31 |
Correct |
10 ms |
18616 KB |
Output is correct |
32 |
Correct |
10 ms |
18648 KB |
Output is correct |
33 |
Correct |
10 ms |
18644 KB |
Output is correct |
34 |
Correct |
10 ms |
18652 KB |
Output is correct |
35 |
Correct |
10 ms |
18624 KB |
Output is correct |
36 |
Correct |
10 ms |
18644 KB |
Output is correct |
37 |
Correct |
10 ms |
18632 KB |
Output is correct |
38 |
Correct |
11 ms |
18644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18388 KB |
Output is correct |
2 |
Correct |
9 ms |
18280 KB |
Output is correct |
3 |
Correct |
9 ms |
18388 KB |
Output is correct |
4 |
Correct |
9 ms |
18384 KB |
Output is correct |
5 |
Correct |
9 ms |
18396 KB |
Output is correct |
6 |
Correct |
9 ms |
18388 KB |
Output is correct |
7 |
Correct |
9 ms |
18384 KB |
Output is correct |
8 |
Correct |
10 ms |
18388 KB |
Output is correct |
9 |
Correct |
10 ms |
18388 KB |
Output is correct |
10 |
Correct |
10 ms |
18388 KB |
Output is correct |
11 |
Correct |
10 ms |
18388 KB |
Output is correct |
12 |
Correct |
9 ms |
18388 KB |
Output is correct |
13 |
Correct |
11 ms |
18388 KB |
Output is correct |
14 |
Correct |
10 ms |
18388 KB |
Output is correct |
15 |
Correct |
9 ms |
18376 KB |
Output is correct |
16 |
Correct |
9 ms |
18384 KB |
Output is correct |
17 |
Correct |
11 ms |
18388 KB |
Output is correct |
18 |
Correct |
9 ms |
18388 KB |
Output is correct |
19 |
Correct |
261 ms |
55608 KB |
Output is correct |
20 |
Correct |
256 ms |
55800 KB |
Output is correct |
21 |
Correct |
247 ms |
55344 KB |
Output is correct |
22 |
Correct |
221 ms |
54724 KB |
Output is correct |
23 |
Correct |
223 ms |
56688 KB |
Output is correct |
24 |
Correct |
141 ms |
52884 KB |
Output is correct |
25 |
Correct |
285 ms |
62344 KB |
Output is correct |
26 |
Correct |
143 ms |
66076 KB |
Output is correct |
27 |
Correct |
278 ms |
88520 KB |
Output is correct |
28 |
Correct |
625 ms |
114888 KB |
Output is correct |
29 |
Correct |
694 ms |
113744 KB |
Output is correct |
30 |
Correct |
268 ms |
88484 KB |
Output is correct |
31 |
Correct |
268 ms |
88516 KB |
Output is correct |
32 |
Correct |
383 ms |
88588 KB |
Output is correct |
33 |
Correct |
556 ms |
95640 KB |
Output is correct |
34 |
Correct |
483 ms |
95468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18388 KB |
Output is correct |
2 |
Correct |
9 ms |
18280 KB |
Output is correct |
3 |
Correct |
9 ms |
18388 KB |
Output is correct |
4 |
Correct |
9 ms |
18384 KB |
Output is correct |
5 |
Correct |
9 ms |
18396 KB |
Output is correct |
6 |
Correct |
9 ms |
18388 KB |
Output is correct |
7 |
Correct |
9 ms |
18384 KB |
Output is correct |
8 |
Correct |
10 ms |
18388 KB |
Output is correct |
9 |
Correct |
10 ms |
18388 KB |
Output is correct |
10 |
Correct |
10 ms |
18388 KB |
Output is correct |
11 |
Correct |
10 ms |
18388 KB |
Output is correct |
12 |
Correct |
9 ms |
18388 KB |
Output is correct |
13 |
Correct |
11 ms |
18388 KB |
Output is correct |
14 |
Correct |
10 ms |
18388 KB |
Output is correct |
15 |
Correct |
9 ms |
18376 KB |
Output is correct |
16 |
Correct |
9 ms |
18384 KB |
Output is correct |
17 |
Correct |
11 ms |
18388 KB |
Output is correct |
18 |
Correct |
9 ms |
18388 KB |
Output is correct |
19 |
Correct |
9 ms |
18308 KB |
Output is correct |
20 |
Correct |
10 ms |
18388 KB |
Output is correct |
21 |
Correct |
10 ms |
18644 KB |
Output is correct |
22 |
Correct |
11 ms |
18672 KB |
Output is correct |
23 |
Correct |
10 ms |
18644 KB |
Output is correct |
24 |
Correct |
14 ms |
18592 KB |
Output is correct |
25 |
Correct |
10 ms |
18652 KB |
Output is correct |
26 |
Correct |
12 ms |
18660 KB |
Output is correct |
27 |
Correct |
10 ms |
18644 KB |
Output is correct |
28 |
Correct |
10 ms |
18712 KB |
Output is correct |
29 |
Correct |
10 ms |
18652 KB |
Output is correct |
30 |
Correct |
11 ms |
18644 KB |
Output is correct |
31 |
Correct |
10 ms |
18616 KB |
Output is correct |
32 |
Correct |
10 ms |
18648 KB |
Output is correct |
33 |
Correct |
10 ms |
18644 KB |
Output is correct |
34 |
Correct |
10 ms |
18652 KB |
Output is correct |
35 |
Correct |
10 ms |
18624 KB |
Output is correct |
36 |
Correct |
10 ms |
18644 KB |
Output is correct |
37 |
Correct |
10 ms |
18632 KB |
Output is correct |
38 |
Correct |
11 ms |
18644 KB |
Output is correct |
39 |
Correct |
261 ms |
55608 KB |
Output is correct |
40 |
Correct |
256 ms |
55800 KB |
Output is correct |
41 |
Correct |
247 ms |
55344 KB |
Output is correct |
42 |
Correct |
221 ms |
54724 KB |
Output is correct |
43 |
Correct |
223 ms |
56688 KB |
Output is correct |
44 |
Correct |
141 ms |
52884 KB |
Output is correct |
45 |
Correct |
285 ms |
62344 KB |
Output is correct |
46 |
Correct |
143 ms |
66076 KB |
Output is correct |
47 |
Correct |
278 ms |
88520 KB |
Output is correct |
48 |
Correct |
625 ms |
114888 KB |
Output is correct |
49 |
Correct |
694 ms |
113744 KB |
Output is correct |
50 |
Correct |
268 ms |
88484 KB |
Output is correct |
51 |
Correct |
268 ms |
88516 KB |
Output is correct |
52 |
Correct |
383 ms |
88588 KB |
Output is correct |
53 |
Correct |
556 ms |
95640 KB |
Output is correct |
54 |
Correct |
483 ms |
95468 KB |
Output is correct |
55 |
Correct |
31 ms |
21836 KB |
Output is correct |
56 |
Correct |
24 ms |
21936 KB |
Output is correct |
57 |
Correct |
140 ms |
56536 KB |
Output is correct |
58 |
Correct |
57 ms |
50352 KB |
Output is correct |
59 |
Correct |
156 ms |
66084 KB |
Output is correct |
60 |
Correct |
857 ms |
113988 KB |
Output is correct |
61 |
Correct |
301 ms |
88668 KB |
Output is correct |
62 |
Correct |
290 ms |
88552 KB |
Output is correct |
63 |
Correct |
443 ms |
88616 KB |
Output is correct |
64 |
Correct |
674 ms |
93948 KB |
Output is correct |
65 |
Correct |
532 ms |
92616 KB |
Output is correct |
66 |
Correct |
745 ms |
111316 KB |
Output is correct |
67 |
Correct |
228 ms |
83820 KB |
Output is correct |
68 |
Correct |
426 ms |
87708 KB |
Output is correct |
69 |
Correct |
400 ms |
88280 KB |
Output is correct |
70 |
Correct |
387 ms |
84480 KB |
Output is correct |