#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int nx=2e5+5, kx=1e6+5;
int n, k, sz[nx], ans=INT_MAX, dp[kx];
bool used[nx];
vector<pair<int, int>> d[nx], res[nx];
int dfssz(int u, int p)
{
sz[u]=1;
for (auto [v, w]:d[u]) if (v!=p&&!used[v]) sz[u]+=dfssz(v, u);
return sz[u];
}
int findcentroid(int u, int p, int rtsz)
{
for (auto [v, w]:d[u]) if (v!=p&&!used[v]&&2*sz[v]>rtsz) return findcentroid(v, u, rtsz);
return u;
}
void dfs(int u, vector<pair<int, int>> &vec, int ds, int len, int p)
{
if (ds>k) return;
vec.push_back({ds, len});
for (auto [v, w]:d[u]) if (v!=p&&!used[v]) dfs(v, vec, ds+w, len+1, u);
}
void decom(int u)
{
u=findcentroid(u, -1, dfssz(u, -1));
used[u]=1;
for (auto [v, w]:d[u])
{
if (used[v]) continue;
dfs(v, res[v], w, 1, u);
for (auto [ds, len]:res[v]) ans=min(ans, len+dp[k-ds]);
for (auto [ds, len]:res[v]) dp[ds]=min(dp[ds], len);
}
for (auto [v, w]:d[u])
{
if (used[v]) continue;
for (auto [ds, len]:res[v]) dp[ds]=1e9;
res[v].clear();
}
for (auto [v, w]:d[u]) if (!used[v]) decom(v);
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N; k=K;
dp[0]=0;
for (int i=1; i<=k; i++) dp[i]=1e9;
for (int i=0; i<n-1; i++) d[H[i][0]].push_back({H[i][1], L[i]}), d[H[i][1]].push_back({H[i][0], L[i]});
decom(0);
return ans>=1e9?-1:ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
3 ms |
14880 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14680 KB |
Output is correct |
14 |
Correct |
3 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
3 ms |
14880 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14680 KB |
Output is correct |
14 |
Correct |
3 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
2 ms |
14680 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14680 KB |
Output is correct |
22 |
Correct |
4 ms |
17240 KB |
Output is correct |
23 |
Correct |
4 ms |
16728 KB |
Output is correct |
24 |
Correct |
5 ms |
16984 KB |
Output is correct |
25 |
Correct |
4 ms |
16988 KB |
Output is correct |
26 |
Correct |
3 ms |
16732 KB |
Output is correct |
27 |
Correct |
4 ms |
16728 KB |
Output is correct |
28 |
Correct |
3 ms |
14680 KB |
Output is correct |
29 |
Correct |
3 ms |
16732 KB |
Output is correct |
30 |
Correct |
3 ms |
16728 KB |
Output is correct |
31 |
Correct |
4 ms |
16728 KB |
Output is correct |
32 |
Correct |
4 ms |
16984 KB |
Output is correct |
33 |
Correct |
4 ms |
16728 KB |
Output is correct |
34 |
Correct |
4 ms |
16728 KB |
Output is correct |
35 |
Correct |
4 ms |
16728 KB |
Output is correct |
36 |
Correct |
4 ms |
17240 KB |
Output is correct |
37 |
Correct |
4 ms |
16984 KB |
Output is correct |
38 |
Correct |
4 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
3 ms |
14880 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14680 KB |
Output is correct |
14 |
Correct |
3 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
106 ms |
29264 KB |
Output is correct |
20 |
Correct |
100 ms |
29244 KB |
Output is correct |
21 |
Correct |
99 ms |
30412 KB |
Output is correct |
22 |
Correct |
87 ms |
29412 KB |
Output is correct |
23 |
Correct |
55 ms |
25168 KB |
Output is correct |
24 |
Correct |
47 ms |
25168 KB |
Output is correct |
25 |
Correct |
93 ms |
34832 KB |
Output is correct |
26 |
Correct |
96 ms |
42060 KB |
Output is correct |
27 |
Correct |
130 ms |
33672 KB |
Output is correct |
28 |
Correct |
188 ms |
41520 KB |
Output is correct |
29 |
Correct |
194 ms |
40528 KB |
Output is correct |
30 |
Correct |
146 ms |
33872 KB |
Output is correct |
31 |
Correct |
119 ms |
33884 KB |
Output is correct |
32 |
Correct |
141 ms |
32848 KB |
Output is correct |
33 |
Correct |
181 ms |
40004 KB |
Output is correct |
34 |
Correct |
107 ms |
24908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
3 ms |
14880 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14680 KB |
Output is correct |
14 |
Correct |
3 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
2 ms |
14680 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14680 KB |
Output is correct |
22 |
Correct |
4 ms |
17240 KB |
Output is correct |
23 |
Correct |
4 ms |
16728 KB |
Output is correct |
24 |
Correct |
5 ms |
16984 KB |
Output is correct |
25 |
Correct |
4 ms |
16988 KB |
Output is correct |
26 |
Correct |
3 ms |
16732 KB |
Output is correct |
27 |
Correct |
4 ms |
16728 KB |
Output is correct |
28 |
Correct |
3 ms |
14680 KB |
Output is correct |
29 |
Correct |
3 ms |
16732 KB |
Output is correct |
30 |
Correct |
3 ms |
16728 KB |
Output is correct |
31 |
Correct |
4 ms |
16728 KB |
Output is correct |
32 |
Correct |
4 ms |
16984 KB |
Output is correct |
33 |
Correct |
4 ms |
16728 KB |
Output is correct |
34 |
Correct |
4 ms |
16728 KB |
Output is correct |
35 |
Correct |
4 ms |
16728 KB |
Output is correct |
36 |
Correct |
4 ms |
17240 KB |
Output is correct |
37 |
Correct |
4 ms |
16984 KB |
Output is correct |
38 |
Correct |
4 ms |
16732 KB |
Output is correct |
39 |
Correct |
106 ms |
29264 KB |
Output is correct |
40 |
Correct |
100 ms |
29244 KB |
Output is correct |
41 |
Correct |
99 ms |
30412 KB |
Output is correct |
42 |
Correct |
87 ms |
29412 KB |
Output is correct |
43 |
Correct |
55 ms |
25168 KB |
Output is correct |
44 |
Correct |
47 ms |
25168 KB |
Output is correct |
45 |
Correct |
93 ms |
34832 KB |
Output is correct |
46 |
Correct |
96 ms |
42060 KB |
Output is correct |
47 |
Correct |
130 ms |
33672 KB |
Output is correct |
48 |
Correct |
188 ms |
41520 KB |
Output is correct |
49 |
Correct |
194 ms |
40528 KB |
Output is correct |
50 |
Correct |
146 ms |
33872 KB |
Output is correct |
51 |
Correct |
119 ms |
33884 KB |
Output is correct |
52 |
Correct |
141 ms |
32848 KB |
Output is correct |
53 |
Correct |
181 ms |
40004 KB |
Output is correct |
54 |
Correct |
107 ms |
24908 KB |
Output is correct |
55 |
Correct |
11 ms |
15960 KB |
Output is correct |
56 |
Correct |
11 ms |
15960 KB |
Output is correct |
57 |
Correct |
68 ms |
34504 KB |
Output is correct |
58 |
Correct |
33 ms |
24516 KB |
Output is correct |
59 |
Correct |
90 ms |
42000 KB |
Output is correct |
60 |
Correct |
311 ms |
71344 KB |
Output is correct |
61 |
Correct |
151 ms |
37072 KB |
Output is correct |
62 |
Correct |
140 ms |
42608 KB |
Output is correct |
63 |
Correct |
177 ms |
42576 KB |
Output is correct |
64 |
Correct |
275 ms |
49992 KB |
Output is correct |
65 |
Correct |
143 ms |
26224 KB |
Output is correct |
66 |
Correct |
305 ms |
60680 KB |
Output is correct |
67 |
Correct |
90 ms |
35264 KB |
Output is correct |
68 |
Correct |
169 ms |
40632 KB |
Output is correct |
69 |
Correct |
168 ms |
40784 KB |
Output is correct |
70 |
Correct |
180 ms |
39536 KB |
Output is correct |