#include "race.h"
#include <bits/stdc++.h>
using namespace std;
int answ, KK, cnt[1000010], subtree[200010], timer=-1, a[1000010];
set<pair<int,int>> adj[200010];
void dfs(int s, int p){
subtree[s]=1;
for(auto x : adj[s]){
int u = x.first;
if(u!=p) dfs(u,s), subtree[s]+=subtree[u];
}
}
void dfs2(int s, int p, int dep, int weight, bool ok){
if(weight>KK) return;
for(auto u : adj[s])
if(u.first!=p)
dfs2(u.first,s,dep+1,weight+u.second, ok);
int D = KK-weight;
if(!ok){
if(D and a[D]<timer) cnt[D] = cnt[KK+1], a[D]=timer;
answ = min(answ, dep+cnt[D]);
}
else{
if(a[weight]<timer) cnt[weight] = cnt[KK+1];
a[weight]=timer; cnt[weight] = min(cnt[weight], dep);
}
}
int find_centroid(int s, int p, int sz){
for(auto u : adj[s])
if(u.first!=p and subtree[u.first]>sz/2)
return find_centroid(u.first, s, sz);
return s;
}
void centroid_decomp(int s, int p){
dfs(s,-1); int sz = subtree[s];
int centroid = find_centroid(s,-1,sz);
// at each level, i should reset cnt[], but that will take O(K) every time so...
// I use another array, a[], which stores the last time i updated cnt[]
timer++;
for(auto w : adj[centroid]){
int u = w.first; if(u==p) continue;
dfs2(u,centroid,1,w.second,0), dfs2(u,centroid,1,w.second,1);
}
while(!adj[centroid].empty()){
int x = adj[centroid].begin()->first;
adj[x].erase(adj[x].lower_bound({centroid,-1}));
adj[centroid].erase(adj[centroid].begin());
centroid_decomp(x,centroid);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
answ = N+1; KK = K; cnt[0]=0;
for(int i = 1; i <= K+1; i++) cnt[i]=N+1;
for(int i = 0; i < N-1; i++)
adj[++H[i][0]].insert({++H[i][1],L[i]}),
adj[H[i][1]].insert({H[i][0],L[i]});
centroid_decomp(1,-1);
return (answ==N+1?-1:answ);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9672 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
15444 KB |
Output is correct |
24 |
Correct |
9 ms |
16452 KB |
Output is correct |
25 |
Correct |
10 ms |
16212 KB |
Output is correct |
26 |
Correct |
7 ms |
12372 KB |
Output is correct |
27 |
Correct |
7 ms |
12884 KB |
Output is correct |
28 |
Correct |
7 ms |
11348 KB |
Output is correct |
29 |
Correct |
7 ms |
12372 KB |
Output is correct |
30 |
Correct |
8 ms |
12628 KB |
Output is correct |
31 |
Correct |
9 ms |
14932 KB |
Output is correct |
32 |
Correct |
9 ms |
15316 KB |
Output is correct |
33 |
Correct |
9 ms |
15828 KB |
Output is correct |
34 |
Correct |
8 ms |
14456 KB |
Output is correct |
35 |
Correct |
10 ms |
16084 KB |
Output is correct |
36 |
Correct |
10 ms |
17108 KB |
Output is correct |
37 |
Correct |
9 ms |
15924 KB |
Output is correct |
38 |
Correct |
8 ms |
13676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9672 KB |
Output is correct |
19 |
Correct |
198 ms |
20676 KB |
Output is correct |
20 |
Correct |
171 ms |
20668 KB |
Output is correct |
21 |
Correct |
201 ms |
20676 KB |
Output is correct |
22 |
Correct |
200 ms |
20628 KB |
Output is correct |
23 |
Correct |
111 ms |
20640 KB |
Output is correct |
24 |
Correct |
92 ms |
20676 KB |
Output is correct |
25 |
Correct |
197 ms |
24672 KB |
Output is correct |
26 |
Correct |
115 ms |
28492 KB |
Output is correct |
27 |
Correct |
258 ms |
31656 KB |
Output is correct |
28 |
Correct |
357 ms |
47192 KB |
Output is correct |
29 |
Correct |
355 ms |
46116 KB |
Output is correct |
30 |
Correct |
263 ms |
31436 KB |
Output is correct |
31 |
Correct |
267 ms |
31504 KB |
Output is correct |
32 |
Correct |
314 ms |
31608 KB |
Output is correct |
33 |
Correct |
314 ms |
31740 KB |
Output is correct |
34 |
Correct |
268 ms |
31704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9672 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
15444 KB |
Output is correct |
24 |
Correct |
9 ms |
16452 KB |
Output is correct |
25 |
Correct |
10 ms |
16212 KB |
Output is correct |
26 |
Correct |
7 ms |
12372 KB |
Output is correct |
27 |
Correct |
7 ms |
12884 KB |
Output is correct |
28 |
Correct |
7 ms |
11348 KB |
Output is correct |
29 |
Correct |
7 ms |
12372 KB |
Output is correct |
30 |
Correct |
8 ms |
12628 KB |
Output is correct |
31 |
Correct |
9 ms |
14932 KB |
Output is correct |
32 |
Correct |
9 ms |
15316 KB |
Output is correct |
33 |
Correct |
9 ms |
15828 KB |
Output is correct |
34 |
Correct |
8 ms |
14456 KB |
Output is correct |
35 |
Correct |
10 ms |
16084 KB |
Output is correct |
36 |
Correct |
10 ms |
17108 KB |
Output is correct |
37 |
Correct |
9 ms |
15924 KB |
Output is correct |
38 |
Correct |
8 ms |
13676 KB |
Output is correct |
39 |
Correct |
198 ms |
20676 KB |
Output is correct |
40 |
Correct |
171 ms |
20668 KB |
Output is correct |
41 |
Correct |
201 ms |
20676 KB |
Output is correct |
42 |
Correct |
200 ms |
20628 KB |
Output is correct |
43 |
Correct |
111 ms |
20640 KB |
Output is correct |
44 |
Correct |
92 ms |
20676 KB |
Output is correct |
45 |
Correct |
197 ms |
24672 KB |
Output is correct |
46 |
Correct |
115 ms |
28492 KB |
Output is correct |
47 |
Correct |
258 ms |
31656 KB |
Output is correct |
48 |
Correct |
357 ms |
47192 KB |
Output is correct |
49 |
Correct |
355 ms |
46116 KB |
Output is correct |
50 |
Correct |
263 ms |
31436 KB |
Output is correct |
51 |
Correct |
267 ms |
31504 KB |
Output is correct |
52 |
Correct |
314 ms |
31608 KB |
Output is correct |
53 |
Correct |
314 ms |
31740 KB |
Output is correct |
54 |
Correct |
268 ms |
31704 KB |
Output is correct |
55 |
Correct |
15 ms |
10840 KB |
Output is correct |
56 |
Correct |
17 ms |
10748 KB |
Output is correct |
57 |
Correct |
101 ms |
20732 KB |
Output is correct |
58 |
Correct |
66 ms |
20664 KB |
Output is correct |
59 |
Correct |
138 ms |
29816 KB |
Output is correct |
60 |
Correct |
653 ms |
53964 KB |
Output is correct |
61 |
Correct |
282 ms |
31564 KB |
Output is correct |
62 |
Correct |
311 ms |
35532 KB |
Output is correct |
63 |
Correct |
387 ms |
35488 KB |
Output is correct |
64 |
Correct |
644 ms |
35728 KB |
Output is correct |
65 |
Correct |
319 ms |
31624 KB |
Output is correct |
66 |
Correct |
511 ms |
51308 KB |
Output is correct |
67 |
Correct |
295 ms |
35500 KB |
Output is correct |
68 |
Correct |
400 ms |
37764 KB |
Output is correct |
69 |
Correct |
474 ms |
38040 KB |
Output is correct |
70 |
Correct |
384 ms |
36744 KB |
Output is correct |