#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
#define ll long long
#define PI pair<int,int>
#define f first
#define s second
#define pb push_back
#define szo(x) ((int)x.size())
#define all(a) (a).begin(), a.end()
#define dbg(v) \
cout << "Line(" << _LINE << ") -> " << #v << " = " << (v) << endl;
int const INF = 0x3f3f3f3f;
ll const LINF = 0x3f3f3f3f3f3f3f3fll;
const int maxn = 2e5+10;
vector<PI> adj[maxn];
int subsize[maxn];
bool isremoved[maxn];
int get_subsize(int x, int prev=-1){
subsize[x] = 1;
for (auto [nex, w] : adj[x]){
if (nex == prev || isremoved[nex]) continue;
subsize[x] += get_subsize(nex, x);
}
return subsize[x];
}
int get_centroid(int x, int tsize, int prev=-1){
for (auto [nex, w] : adj[x]){
if (nex == prev || isremoved[nex]) continue;
if (subsize[nex] * 2 > tsize) return get_centroid(nex, tsize, x);
}
return x;
}
const int maxk = 1e6+10;
int dp[maxk];
int k;
int maxdep;
void dfs1(int x, int cent, int d, int cnt=1, int prev=-1){
if (d > k) return;
for (auto [nex, w] : adj[x]){
if (nex == prev || isremoved[nex]) continue;
dfs1(nex, cent, d+w, cnt+1, x);
}
maxdep = max(maxdep, d);
dp[d] = min(dp[d], cnt);
}
int ans;
void dfs2(int x, int cent, int d, int cnt=1, int prev=-1){
if (d > k) return;
for (auto [nex, w] : adj[x]){
if (nex == prev || isremoved[nex]) continue;
dfs2(nex, cent, d+w, cnt+1, x);
}
if (k-d <= maxdep) ans = min(ans, cnt + dp[k-d]);
}
void build_decomp(int x){
int cent = get_centroid(x, get_subsize(x));
isremoved[cent] = true;
for (int i = 0; i <= maxdep; ++i) dp[i] = INF;
maxdep = 0;
dp[0] = 0;
for (auto [nex, w] : adj[cent]){
if (!isremoved[nex]){
dfs2(nex, cent, w);
dfs1(nex, cent, w);
}
}
for (auto [nex, w] : adj[cent]){
if (isremoved[nex]) continue;
build_decomp(nex);
}
}
int best_path(int n, int K, int h[][2], int l[]){
k = K;
maxdep = k;
ans = INF;
for (int i = 0; i < n-1; ++i){
adj[h[i][0]].pb({h[i][1], l[i]});
adj[h[i][1]].pb({h[i][0], l[i]});
}
build_decomp(0);
if (ans < INF) return ans;
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11612 KB |
Output is correct |
2 |
Correct |
4 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
3 ms |
11608 KB |
Output is correct |
5 |
Correct |
2 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11608 KB |
Output is correct |
8 |
Correct |
2 ms |
11864 KB |
Output is correct |
9 |
Correct |
2 ms |
11612 KB |
Output is correct |
10 |
Correct |
2 ms |
11612 KB |
Output is correct |
11 |
Correct |
3 ms |
11628 KB |
Output is correct |
12 |
Correct |
3 ms |
11612 KB |
Output is correct |
13 |
Correct |
2 ms |
11608 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
2 ms |
11612 KB |
Output is correct |
16 |
Correct |
3 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11612 KB |
Output is correct |
18 |
Correct |
2 ms |
12020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11612 KB |
Output is correct |
2 |
Correct |
4 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
3 ms |
11608 KB |
Output is correct |
5 |
Correct |
2 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11608 KB |
Output is correct |
8 |
Correct |
2 ms |
11864 KB |
Output is correct |
9 |
Correct |
2 ms |
11612 KB |
Output is correct |
10 |
Correct |
2 ms |
11612 KB |
Output is correct |
11 |
Correct |
3 ms |
11628 KB |
Output is correct |
12 |
Correct |
3 ms |
11612 KB |
Output is correct |
13 |
Correct |
2 ms |
11608 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
2 ms |
11612 KB |
Output is correct |
16 |
Correct |
3 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11612 KB |
Output is correct |
18 |
Correct |
2 ms |
12020 KB |
Output is correct |
19 |
Correct |
2 ms |
11612 KB |
Output is correct |
20 |
Correct |
3 ms |
11636 KB |
Output is correct |
21 |
Correct |
3 ms |
11612 KB |
Output is correct |
22 |
Correct |
22 ms |
13660 KB |
Output is correct |
23 |
Correct |
16 ms |
13660 KB |
Output is correct |
24 |
Correct |
20 ms |
13732 KB |
Output is correct |
25 |
Correct |
5 ms |
13680 KB |
Output is correct |
26 |
Correct |
9 ms |
13660 KB |
Output is correct |
27 |
Correct |
3 ms |
13660 KB |
Output is correct |
28 |
Correct |
5 ms |
13660 KB |
Output is correct |
29 |
Correct |
5 ms |
13660 KB |
Output is correct |
30 |
Correct |
5 ms |
13724 KB |
Output is correct |
31 |
Correct |
6 ms |
13656 KB |
Output is correct |
32 |
Correct |
5 ms |
13664 KB |
Output is correct |
33 |
Correct |
6 ms |
13664 KB |
Output is correct |
34 |
Correct |
6 ms |
13656 KB |
Output is correct |
35 |
Correct |
6 ms |
13660 KB |
Output is correct |
36 |
Correct |
6 ms |
13660 KB |
Output is correct |
37 |
Correct |
4 ms |
13660 KB |
Output is correct |
38 |
Correct |
3 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11612 KB |
Output is correct |
2 |
Correct |
4 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
3 ms |
11608 KB |
Output is correct |
5 |
Correct |
2 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11608 KB |
Output is correct |
8 |
Correct |
2 ms |
11864 KB |
Output is correct |
9 |
Correct |
2 ms |
11612 KB |
Output is correct |
10 |
Correct |
2 ms |
11612 KB |
Output is correct |
11 |
Correct |
3 ms |
11628 KB |
Output is correct |
12 |
Correct |
3 ms |
11612 KB |
Output is correct |
13 |
Correct |
2 ms |
11608 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
2 ms |
11612 KB |
Output is correct |
16 |
Correct |
3 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11612 KB |
Output is correct |
18 |
Correct |
2 ms |
12020 KB |
Output is correct |
19 |
Correct |
87 ms |
17608 KB |
Output is correct |
20 |
Correct |
88 ms |
18640 KB |
Output is correct |
21 |
Correct |
80 ms |
18004 KB |
Output is correct |
22 |
Correct |
74 ms |
18768 KB |
Output is correct |
23 |
Correct |
47 ms |
19344 KB |
Output is correct |
24 |
Correct |
37 ms |
18772 KB |
Output is correct |
25 |
Correct |
83 ms |
19792 KB |
Output is correct |
26 |
Correct |
69 ms |
21612 KB |
Output is correct |
27 |
Correct |
105 ms |
22868 KB |
Output is correct |
28 |
Correct |
152 ms |
27792 KB |
Output is correct |
29 |
Correct |
157 ms |
27740 KB |
Output is correct |
30 |
Correct |
102 ms |
23524 KB |
Output is correct |
31 |
Correct |
100 ms |
23524 KB |
Output is correct |
32 |
Correct |
149 ms |
23508 KB |
Output is correct |
33 |
Correct |
131 ms |
22356 KB |
Output is correct |
34 |
Correct |
121 ms |
22608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11612 KB |
Output is correct |
2 |
Correct |
4 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
3 ms |
11608 KB |
Output is correct |
5 |
Correct |
2 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11608 KB |
Output is correct |
8 |
Correct |
2 ms |
11864 KB |
Output is correct |
9 |
Correct |
2 ms |
11612 KB |
Output is correct |
10 |
Correct |
2 ms |
11612 KB |
Output is correct |
11 |
Correct |
3 ms |
11628 KB |
Output is correct |
12 |
Correct |
3 ms |
11612 KB |
Output is correct |
13 |
Correct |
2 ms |
11608 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
2 ms |
11612 KB |
Output is correct |
16 |
Correct |
3 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11612 KB |
Output is correct |
18 |
Correct |
2 ms |
12020 KB |
Output is correct |
19 |
Correct |
2 ms |
11612 KB |
Output is correct |
20 |
Correct |
3 ms |
11636 KB |
Output is correct |
21 |
Correct |
3 ms |
11612 KB |
Output is correct |
22 |
Correct |
22 ms |
13660 KB |
Output is correct |
23 |
Correct |
16 ms |
13660 KB |
Output is correct |
24 |
Correct |
20 ms |
13732 KB |
Output is correct |
25 |
Correct |
5 ms |
13680 KB |
Output is correct |
26 |
Correct |
9 ms |
13660 KB |
Output is correct |
27 |
Correct |
3 ms |
13660 KB |
Output is correct |
28 |
Correct |
5 ms |
13660 KB |
Output is correct |
29 |
Correct |
5 ms |
13660 KB |
Output is correct |
30 |
Correct |
5 ms |
13724 KB |
Output is correct |
31 |
Correct |
6 ms |
13656 KB |
Output is correct |
32 |
Correct |
5 ms |
13664 KB |
Output is correct |
33 |
Correct |
6 ms |
13664 KB |
Output is correct |
34 |
Correct |
6 ms |
13656 KB |
Output is correct |
35 |
Correct |
6 ms |
13660 KB |
Output is correct |
36 |
Correct |
6 ms |
13660 KB |
Output is correct |
37 |
Correct |
4 ms |
13660 KB |
Output is correct |
38 |
Correct |
3 ms |
13660 KB |
Output is correct |
39 |
Correct |
87 ms |
17608 KB |
Output is correct |
40 |
Correct |
88 ms |
18640 KB |
Output is correct |
41 |
Correct |
80 ms |
18004 KB |
Output is correct |
42 |
Correct |
74 ms |
18768 KB |
Output is correct |
43 |
Correct |
47 ms |
19344 KB |
Output is correct |
44 |
Correct |
37 ms |
18772 KB |
Output is correct |
45 |
Correct |
83 ms |
19792 KB |
Output is correct |
46 |
Correct |
69 ms |
21612 KB |
Output is correct |
47 |
Correct |
105 ms |
22868 KB |
Output is correct |
48 |
Correct |
152 ms |
27792 KB |
Output is correct |
49 |
Correct |
157 ms |
27740 KB |
Output is correct |
50 |
Correct |
102 ms |
23524 KB |
Output is correct |
51 |
Correct |
100 ms |
23524 KB |
Output is correct |
52 |
Correct |
149 ms |
23508 KB |
Output is correct |
53 |
Correct |
131 ms |
22356 KB |
Output is correct |
54 |
Correct |
121 ms |
22608 KB |
Output is correct |
55 |
Correct |
8 ms |
12124 KB |
Output is correct |
56 |
Correct |
10 ms |
11864 KB |
Output is correct |
57 |
Correct |
50 ms |
17944 KB |
Output is correct |
58 |
Correct |
27 ms |
18628 KB |
Output is correct |
59 |
Correct |
65 ms |
23640 KB |
Output is correct |
60 |
Correct |
276 ms |
31824 KB |
Output is correct |
61 |
Correct |
139 ms |
22712 KB |
Output is correct |
62 |
Correct |
117 ms |
25404 KB |
Output is correct |
63 |
Correct |
154 ms |
25488 KB |
Output is correct |
64 |
Correct |
338 ms |
23964 KB |
Output is correct |
65 |
Correct |
125 ms |
24404 KB |
Output is correct |
66 |
Correct |
257 ms |
26988 KB |
Output is correct |
67 |
Correct |
67 ms |
26956 KB |
Output is correct |
68 |
Correct |
175 ms |
25936 KB |
Output is correct |
69 |
Correct |
181 ms |
23920 KB |
Output is correct |
70 |
Correct |
157 ms |
26020 KB |
Output is correct |