#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+7, INF = 1e9;
int k, ans = INF, dp[1000007], sub[N], mx;
vector<array<int,2>> e[N];
bitset<N> b;
int subDfs(int c, int p)
{
sub[c]=1;
for (auto i : e[c])
if (i[0]!=p&&!b[i[0]])
sub[c]+=subDfs(i[0],c);
return sub[c];
}
int findC(int c, int p, int t)
{
for (auto i : e[c])
if (i[0]!=p&&!b[i[0]]&&sub[i[0]]>t)
return findC(i[0],c,t);
return c;
}
void dfs(int c, int p, int h, int t, bool fill)
{
if (h>k)
return;
mx=max(mx,h);
if (fill)
dp[h]=min(dp[h],t);
else
ans=min(ans,dp[k-h]+t);
for (auto i : e[c])
if (i[0]!=p&&!b[i[0]])
dfs(i[0],c,h+i[1],t+1,fill);
}
void undo(int c, int p, int h)
{
if (h>k)
return;
dp[h]=INF;
for (auto i : e[c])
if (i[0]!=p&&!b[i[0]])
undo(i[0],c,h+i[1]);
}
void solve(int n)
{
int c = findC(n,-1,subDfs(n,-1)/2);
b[c]=1;
mx=0;
for (auto i : e[c])
if (!b[i[0]])
{
dfs(i[0],c,i[1],1,0);
dfs(i[0],c,i[1],1,1);
}
for (auto i : e[c])
if (!b[i[0]])
undo(i[0],c,i[1]);
for (auto i : e[c])
if (!b[i[0]])
solve(i[0]);
}
int best_path(int n, int K, int h[][2], int l[])
{
k=K;
for (int i=0;i<n-1;++i)
{
e[h[i][0]].push_back({h[i][1],l[i]});
e[h[i][1]].push_back({h[i][0],l[i]});
}
for (int i=1;i<=k;++i)
dp[i]=INF;
solve(0);
if (ans==INF)
return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4928 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5008 KB |
Output is correct |
14 |
Correct |
4 ms |
5008 KB |
Output is correct |
15 |
Correct |
4 ms |
5008 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4928 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5008 KB |
Output is correct |
14 |
Correct |
4 ms |
5008 KB |
Output is correct |
15 |
Correct |
4 ms |
5008 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
8652 KB |
Output is correct |
23 |
Correct |
6 ms |
7964 KB |
Output is correct |
24 |
Correct |
6 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8268 KB |
Output is correct |
26 |
Correct |
5 ms |
6348 KB |
Output is correct |
27 |
Correct |
6 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
5836 KB |
Output is correct |
29 |
Correct |
5 ms |
6348 KB |
Output is correct |
30 |
Correct |
5 ms |
6476 KB |
Output is correct |
31 |
Correct |
6 ms |
7628 KB |
Output is correct |
32 |
Correct |
6 ms |
7756 KB |
Output is correct |
33 |
Correct |
7 ms |
8140 KB |
Output is correct |
34 |
Correct |
6 ms |
7324 KB |
Output is correct |
35 |
Correct |
6 ms |
8212 KB |
Output is correct |
36 |
Correct |
6 ms |
8652 KB |
Output is correct |
37 |
Correct |
6 ms |
8140 KB |
Output is correct |
38 |
Correct |
5 ms |
6988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4928 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5008 KB |
Output is correct |
14 |
Correct |
4 ms |
5008 KB |
Output is correct |
15 |
Correct |
4 ms |
5008 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
172 ms |
11588 KB |
Output is correct |
20 |
Correct |
152 ms |
11496 KB |
Output is correct |
21 |
Correct |
175 ms |
11672 KB |
Output is correct |
22 |
Correct |
182 ms |
11648 KB |
Output is correct |
23 |
Correct |
92 ms |
11844 KB |
Output is correct |
24 |
Correct |
63 ms |
11772 KB |
Output is correct |
25 |
Correct |
154 ms |
14348 KB |
Output is correct |
26 |
Correct |
116 ms |
17220 KB |
Output is correct |
27 |
Correct |
200 ms |
18488 KB |
Output is correct |
28 |
Correct |
303 ms |
29888 KB |
Output is correct |
29 |
Correct |
293 ms |
29040 KB |
Output is correct |
30 |
Correct |
205 ms |
18504 KB |
Output is correct |
31 |
Correct |
199 ms |
18644 KB |
Output is correct |
32 |
Correct |
252 ms |
18720 KB |
Output is correct |
33 |
Correct |
239 ms |
17424 KB |
Output is correct |
34 |
Correct |
225 ms |
18380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4928 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5008 KB |
Output is correct |
14 |
Correct |
4 ms |
5008 KB |
Output is correct |
15 |
Correct |
4 ms |
5008 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
8652 KB |
Output is correct |
23 |
Correct |
6 ms |
7964 KB |
Output is correct |
24 |
Correct |
6 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8268 KB |
Output is correct |
26 |
Correct |
5 ms |
6348 KB |
Output is correct |
27 |
Correct |
6 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
5836 KB |
Output is correct |
29 |
Correct |
5 ms |
6348 KB |
Output is correct |
30 |
Correct |
5 ms |
6476 KB |
Output is correct |
31 |
Correct |
6 ms |
7628 KB |
Output is correct |
32 |
Correct |
6 ms |
7756 KB |
Output is correct |
33 |
Correct |
7 ms |
8140 KB |
Output is correct |
34 |
Correct |
6 ms |
7324 KB |
Output is correct |
35 |
Correct |
6 ms |
8212 KB |
Output is correct |
36 |
Correct |
6 ms |
8652 KB |
Output is correct |
37 |
Correct |
6 ms |
8140 KB |
Output is correct |
38 |
Correct |
5 ms |
6988 KB |
Output is correct |
39 |
Correct |
172 ms |
11588 KB |
Output is correct |
40 |
Correct |
152 ms |
11496 KB |
Output is correct |
41 |
Correct |
175 ms |
11672 KB |
Output is correct |
42 |
Correct |
182 ms |
11648 KB |
Output is correct |
43 |
Correct |
92 ms |
11844 KB |
Output is correct |
44 |
Correct |
63 ms |
11772 KB |
Output is correct |
45 |
Correct |
154 ms |
14348 KB |
Output is correct |
46 |
Correct |
116 ms |
17220 KB |
Output is correct |
47 |
Correct |
200 ms |
18488 KB |
Output is correct |
48 |
Correct |
303 ms |
29888 KB |
Output is correct |
49 |
Correct |
293 ms |
29040 KB |
Output is correct |
50 |
Correct |
205 ms |
18504 KB |
Output is correct |
51 |
Correct |
199 ms |
18644 KB |
Output is correct |
52 |
Correct |
252 ms |
18720 KB |
Output is correct |
53 |
Correct |
239 ms |
17424 KB |
Output is correct |
54 |
Correct |
225 ms |
18380 KB |
Output is correct |
55 |
Correct |
13 ms |
5712 KB |
Output is correct |
56 |
Correct |
14 ms |
5660 KB |
Output is correct |
57 |
Correct |
96 ms |
11748 KB |
Output is correct |
58 |
Correct |
42 ms |
11512 KB |
Output is correct |
59 |
Correct |
115 ms |
18140 KB |
Output is correct |
60 |
Correct |
614 ms |
33100 KB |
Output is correct |
61 |
Correct |
225 ms |
18772 KB |
Output is correct |
62 |
Correct |
287 ms |
22600 KB |
Output is correct |
63 |
Correct |
358 ms |
22536 KB |
Output is correct |
64 |
Correct |
771 ms |
21100 KB |
Output is correct |
65 |
Correct |
300 ms |
19652 KB |
Output is correct |
66 |
Correct |
489 ms |
31080 KB |
Output is correct |
67 |
Correct |
147 ms |
23348 KB |
Output is correct |
68 |
Correct |
374 ms |
23260 KB |
Output is correct |
69 |
Correct |
357 ms |
23556 KB |
Output is correct |
70 |
Correct |
336 ms |
22676 KB |
Output is correct |