#include "race.h"
#include <bits/stdc++.h>
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
#define NL(x) " \n"[(x)]
#define lld long long
#define pil pair<int,lld>
#define pli pair<lld,int>
#define pll pair<lld,lld>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 200010
#define MAXK 1000010
vector<pii> adj[MAXN];
int N, K, siz[MAXN], res, b[MAXK];
bool vis[MAXN];
int dfsSize(int node, int prev) {
siz[node] = 1;
for(auto x : adj[node]) {
if(x.fi != prev && !vis[x.fi]) siz[node] += dfsSize(x.fi, node);
}
return siz[node];
}
int dfsCentroid(int node, int prev, int S) {
for(auto x : adj[node]) {
if(x.fi != prev && !vis[x.fi] && siz[x.fi] > S/2) return dfsCentroid(x.fi, node, S);
}
return node;
}
vector<int> v;
vector<pii> vt;
void dfsSolve(int node, int prev, int len, int dub) {
vt.pb({dub, len});
v.pb(len);
res = min(res, b[K-len]+dub);
for(auto x : adj[node]) {
if(x.fi != prev && !vis[x.fi] && len+x.se <= K) dfsSolve(x.fi, node, len+x.se, dub+1);
}
}
void centroidDecomp(int node) {
int S = dfsSize(node, node);
node = dfsCentroid(node, node, S);
vis[node] = 1;
for(auto x : adj[node]) {
if(!vis[x.fi] && x.se <= K) dfsSolve(x.fi, node, x.se, 1);
for(auto y : vt) {
b[y.se] = min(b[y.se], y.fi);
}
res = min(res, b[K]);
vt.clear();
}
for(auto x : v) b[x] = N;
v.clear();
for(auto x : adj[node]) if(!vis[x.fi]) centroidDecomp(x.fi);
}
int best_path(int n, int k, int H[][2], int L[]) {
N = n; K = k;
for(int i = 0; i < N-1; i++) {
adj[H[i][0]+1].pb({H[i][1]+1, L[i]});
adj[H[i][1]+1].pb({H[i][0]+1, L[i]});
}
res = N;
for(int i = 0; i <= K; i++) b[i] = N;
centroidDecomp(1);
return (res == N ? -1:res);
}
/*
4 3
0 1 1
1 2 2
1 3 4
2
3 3
0 1 1
1 2 1
-1
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
5008 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5008 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
5008 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5008 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5012 KB |
Output is correct |
22 |
Correct |
6 ms |
8652 KB |
Output is correct |
23 |
Correct |
6 ms |
7932 KB |
Output is correct |
24 |
Correct |
7 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8268 KB |
Output is correct |
26 |
Correct |
5 ms |
6368 KB |
Output is correct |
27 |
Correct |
6 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
5888 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 |
7584 KB |
Output is correct |
32 |
Correct |
6 ms |
7836 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
7324 KB |
Output is correct |
35 |
Correct |
6 ms |
8268 KB |
Output is correct |
36 |
Correct |
7 ms |
8652 KB |
Output is correct |
37 |
Correct |
6 ms |
8216 KB |
Output is correct |
38 |
Correct |
6 ms |
6988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
5008 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5008 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
126 ms |
11716 KB |
Output is correct |
20 |
Correct |
120 ms |
11856 KB |
Output is correct |
21 |
Correct |
126 ms |
12148 KB |
Output is correct |
22 |
Correct |
114 ms |
12608 KB |
Output is correct |
23 |
Correct |
78 ms |
12020 KB |
Output is correct |
24 |
Correct |
71 ms |
11960 KB |
Output is correct |
25 |
Correct |
120 ms |
14492 KB |
Output is correct |
26 |
Correct |
105 ms |
18372 KB |
Output is correct |
27 |
Correct |
189 ms |
18812 KB |
Output is correct |
28 |
Correct |
274 ms |
30144 KB |
Output is correct |
29 |
Correct |
289 ms |
29236 KB |
Output is correct |
30 |
Correct |
177 ms |
18836 KB |
Output is correct |
31 |
Correct |
179 ms |
18876 KB |
Output is correct |
32 |
Correct |
245 ms |
18800 KB |
Output is correct |
33 |
Correct |
228 ms |
17544 KB |
Output is correct |
34 |
Correct |
202 ms |
18500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
5008 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5008 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5012 KB |
Output is correct |
22 |
Correct |
6 ms |
8652 KB |
Output is correct |
23 |
Correct |
6 ms |
7932 KB |
Output is correct |
24 |
Correct |
7 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8268 KB |
Output is correct |
26 |
Correct |
5 ms |
6368 KB |
Output is correct |
27 |
Correct |
6 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
5888 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 |
7584 KB |
Output is correct |
32 |
Correct |
6 ms |
7836 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
7324 KB |
Output is correct |
35 |
Correct |
6 ms |
8268 KB |
Output is correct |
36 |
Correct |
7 ms |
8652 KB |
Output is correct |
37 |
Correct |
6 ms |
8216 KB |
Output is correct |
38 |
Correct |
6 ms |
6988 KB |
Output is correct |
39 |
Correct |
126 ms |
11716 KB |
Output is correct |
40 |
Correct |
120 ms |
11856 KB |
Output is correct |
41 |
Correct |
126 ms |
12148 KB |
Output is correct |
42 |
Correct |
114 ms |
12608 KB |
Output is correct |
43 |
Correct |
78 ms |
12020 KB |
Output is correct |
44 |
Correct |
71 ms |
11960 KB |
Output is correct |
45 |
Correct |
120 ms |
14492 KB |
Output is correct |
46 |
Correct |
105 ms |
18372 KB |
Output is correct |
47 |
Correct |
189 ms |
18812 KB |
Output is correct |
48 |
Correct |
274 ms |
30144 KB |
Output is correct |
49 |
Correct |
289 ms |
29236 KB |
Output is correct |
50 |
Correct |
177 ms |
18836 KB |
Output is correct |
51 |
Correct |
179 ms |
18876 KB |
Output is correct |
52 |
Correct |
245 ms |
18800 KB |
Output is correct |
53 |
Correct |
228 ms |
17544 KB |
Output is correct |
54 |
Correct |
202 ms |
18500 KB |
Output is correct |
55 |
Correct |
11 ms |
5816 KB |
Output is correct |
56 |
Correct |
12 ms |
5836 KB |
Output is correct |
57 |
Correct |
90 ms |
12772 KB |
Output is correct |
58 |
Correct |
42 ms |
12472 KB |
Output is correct |
59 |
Correct |
111 ms |
19140 KB |
Output is correct |
60 |
Correct |
458 ms |
35448 KB |
Output is correct |
61 |
Correct |
186 ms |
19148 KB |
Output is correct |
62 |
Correct |
204 ms |
23740 KB |
Output is correct |
63 |
Correct |
285 ms |
23752 KB |
Output is correct |
64 |
Correct |
454 ms |
23112 KB |
Output is correct |
65 |
Correct |
231 ms |
19652 KB |
Output is correct |
66 |
Correct |
380 ms |
31352 KB |
Output is correct |
67 |
Correct |
150 ms |
25140 KB |
Output is correct |
68 |
Correct |
282 ms |
23996 KB |
Output is correct |
69 |
Correct |
260 ms |
24212 KB |
Output is correct |
70 |
Correct |
304 ms |
23436 KB |
Output is correct |