#include "race.h"
#include <vector>
#include <bitset>
using namespace std;
vector<pair<int, int> > adj[200005];
int dp[1000005], path[1000005], sz[200005];
int n, k, ans = -1, centroid, mxnode, cnt = 0;
bitset<200005> vis;
int dfs_subtree(int nw, int bf){
sz[nw] = 1;
for(pair<int, int> nx : adj[nw])
if(!vis[nx.first] && nx.first != bf)
sz[nw] += dfs_subtree(nx.first, nw);
return sz[nw];
}
void centroid_decomp(int nw, int bf, int all){
int rs = all - sz[nw];
for(pair<int, int> nx : adj[nw])
if(!vis[nx.first] && nx.first != bf){
centroid_decomp(nx.first, nw, all);
rs = max(rs, sz[nx.first]);
}
if(rs < mxnode){
centroid = nw;
mxnode = rs;
}
}
void dfs(int nw, int bf, int cst, int edge, bool fill){
if(cst > k) return;
if(!fill){
if(dp[k - cst] == cnt && (edge + path[k - cst] < ans || ans == -1))
ans = edge + path[k - cst];
if(cst == k && (edge < ans || ans == -1))
ans = edge;
}else if(dp[cst] < cnt || edge < path[cst]){
dp[cst] = cnt;
path[cst] = edge;
}
for(pair<int, int> nx : adj[nw]) if(!vis[nx.first] && nx.first != bf)
dfs(nx.first, nw, cst + nx.second, edge + 1, fill);
}
void solve(int nw){
cnt ++;
if(dfs_subtree(nw, -1) <= 1) return;
mxnode = sz[nw] + 1;
centroid_decomp(nw, -1, sz[nw]);
for(pair<int, int> nx : adj[centroid]) if(!vis[nx.first]){
dfs(nx.first, centroid, nx.second, 1, false);
dfs(nx.first, centroid, nx.second, 1, true);
}
vis[centroid] = 1;
for(pair<int, int> nx : adj[centroid]) if(!vis[nx.first])
solve(nx.first);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
for(int i = 1; i < n; i ++){
adj[H[i - 1][0]].push_back({H[i - 1][1], L[i - 1]});
adj[H[i - 1][1]].push_back({H[i - 1][0], L[i - 1]});
}
solve(0);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5056 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
5096 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
5096 KB |
Output is correct |
17 |
Correct |
4 ms |
5056 KB |
Output is correct |
18 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5056 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
5096 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
5096 KB |
Output is correct |
17 |
Correct |
4 ms |
5056 KB |
Output is correct |
18 |
Correct |
3 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
9 ms |
10112 KB |
Output is correct |
23 |
Correct |
7 ms |
9344 KB |
Output is correct |
24 |
Correct |
9 ms |
10856 KB |
Output is correct |
25 |
Correct |
7 ms |
8648 KB |
Output is correct |
26 |
Correct |
6 ms |
7680 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
6 ms |
6656 KB |
Output is correct |
29 |
Correct |
7 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7936 KB |
Output is correct |
31 |
Correct |
8 ms |
8704 KB |
Output is correct |
32 |
Correct |
8 ms |
8832 KB |
Output is correct |
33 |
Correct |
10 ms |
10368 KB |
Output is correct |
34 |
Correct |
9 ms |
9600 KB |
Output is correct |
35 |
Correct |
8 ms |
11008 KB |
Output is correct |
36 |
Correct |
9 ms |
11520 KB |
Output is correct |
37 |
Correct |
7 ms |
8960 KB |
Output is correct |
38 |
Correct |
6 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5056 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
5096 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
5096 KB |
Output is correct |
17 |
Correct |
4 ms |
5056 KB |
Output is correct |
18 |
Correct |
3 ms |
4992 KB |
Output is correct |
19 |
Correct |
289 ms |
10616 KB |
Output is correct |
20 |
Correct |
263 ms |
10616 KB |
Output is correct |
21 |
Correct |
182 ms |
10616 KB |
Output is correct |
22 |
Correct |
195 ms |
10744 KB |
Output is correct |
23 |
Correct |
110 ms |
10744 KB |
Output is correct |
24 |
Correct |
75 ms |
10616 KB |
Output is correct |
25 |
Correct |
150 ms |
14072 KB |
Output is correct |
26 |
Correct |
114 ms |
17932 KB |
Output is correct |
27 |
Correct |
224 ms |
15916 KB |
Output is correct |
28 |
Correct |
371 ms |
30328 KB |
Output is correct |
29 |
Correct |
412 ms |
29176 KB |
Output is correct |
30 |
Correct |
255 ms |
16120 KB |
Output is correct |
31 |
Correct |
251 ms |
16248 KB |
Output is correct |
32 |
Correct |
287 ms |
15992 KB |
Output is correct |
33 |
Correct |
312 ms |
14968 KB |
Output is correct |
34 |
Correct |
272 ms |
14840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5056 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
5096 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
5096 KB |
Output is correct |
17 |
Correct |
4 ms |
5056 KB |
Output is correct |
18 |
Correct |
3 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
9 ms |
10112 KB |
Output is correct |
23 |
Correct |
7 ms |
9344 KB |
Output is correct |
24 |
Correct |
9 ms |
10856 KB |
Output is correct |
25 |
Correct |
7 ms |
8648 KB |
Output is correct |
26 |
Correct |
6 ms |
7680 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
6 ms |
6656 KB |
Output is correct |
29 |
Correct |
7 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7936 KB |
Output is correct |
31 |
Correct |
8 ms |
8704 KB |
Output is correct |
32 |
Correct |
8 ms |
8832 KB |
Output is correct |
33 |
Correct |
10 ms |
10368 KB |
Output is correct |
34 |
Correct |
9 ms |
9600 KB |
Output is correct |
35 |
Correct |
8 ms |
11008 KB |
Output is correct |
36 |
Correct |
9 ms |
11520 KB |
Output is correct |
37 |
Correct |
7 ms |
8960 KB |
Output is correct |
38 |
Correct |
6 ms |
7424 KB |
Output is correct |
39 |
Correct |
289 ms |
10616 KB |
Output is correct |
40 |
Correct |
263 ms |
10616 KB |
Output is correct |
41 |
Correct |
182 ms |
10616 KB |
Output is correct |
42 |
Correct |
195 ms |
10744 KB |
Output is correct |
43 |
Correct |
110 ms |
10744 KB |
Output is correct |
44 |
Correct |
75 ms |
10616 KB |
Output is correct |
45 |
Correct |
150 ms |
14072 KB |
Output is correct |
46 |
Correct |
114 ms |
17932 KB |
Output is correct |
47 |
Correct |
224 ms |
15916 KB |
Output is correct |
48 |
Correct |
371 ms |
30328 KB |
Output is correct |
49 |
Correct |
412 ms |
29176 KB |
Output is correct |
50 |
Correct |
255 ms |
16120 KB |
Output is correct |
51 |
Correct |
251 ms |
16248 KB |
Output is correct |
52 |
Correct |
287 ms |
15992 KB |
Output is correct |
53 |
Correct |
312 ms |
14968 KB |
Output is correct |
54 |
Correct |
272 ms |
14840 KB |
Output is correct |
55 |
Correct |
14 ms |
5760 KB |
Output is correct |
56 |
Correct |
15 ms |
5760 KB |
Output is correct |
57 |
Correct |
101 ms |
10836 KB |
Output is correct |
58 |
Correct |
43 ms |
10740 KB |
Output is correct |
59 |
Correct |
145 ms |
18552 KB |
Output is correct |
60 |
Correct |
582 ms |
34296 KB |
Output is correct |
61 |
Correct |
233 ms |
15992 KB |
Output is correct |
62 |
Correct |
252 ms |
15992 KB |
Output is correct |
63 |
Correct |
389 ms |
16076 KB |
Output is correct |
64 |
Correct |
697 ms |
18000 KB |
Output is correct |
65 |
Correct |
373 ms |
15980 KB |
Output is correct |
66 |
Correct |
595 ms |
34296 KB |
Output is correct |
67 |
Correct |
138 ms |
16268 KB |
Output is correct |
68 |
Correct |
393 ms |
21496 KB |
Output is correct |
69 |
Correct |
428 ms |
21752 KB |
Output is correct |
70 |
Correct |
345 ms |
20856 KB |
Output is correct |