#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
const int maxn = 2e5 + 2;
const int maxk = 1000001;
const int INF=2e9;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//-----------------------------------
int n, k, sz[maxn], done[maxn], ans[maxk], tot=maxk;
vector<pi> adj[maxn];
void getSize(int u, int pre) {
sz[u] = 1;
for (auto it : adj[u]) {
int v=it.F, w=it.S;
if (!done[v] && v != pre) {
getSize(v, u);
sz[u] += sz[v];
}
}
}
int centroid(int u, int pre, int tot) {
for (auto it : adj[u]) {
int v=it.F, w=it.S;
if (v != pre && !done[v] && sz[v]*2 >= tot)
return centroid(v, u, tot);
}
return u;
}
void getPaths(int u, int pre, int d, int e, vector<pi> &tmp) {
if (d <= k) {
tot = min(tot, ans[k-d] + e);
tmp.PB({d, e});
}
for (auto it : adj[u]) {
int v=it.F, w=it.S;
if (!done[v] && v!=pre)
getPaths(v, u, d+w, e+1, tmp);
}
}
void solve(int u) {
getSize(u, -1);
u = centroid(u, -1, sz[u]);
done[u] = 1;
vi vec;
for (auto it : adj[u]) {
int v=it.F, w=it.S;
if (!done[v]){
vector<pi> tmp;
getPaths(v, u, w, 1, tmp);
for (auto it : tmp){
int d=it.F, e=it.S;
ans[d] = min(ans[d], e);
vec.PB(d);
}
}
}
for(int x: vec) ans[x]=INF;
ans[0]=0;
for (auto it : adj[u]) {
int v=it.F, w=it.S;
if (!done[v]) solve(v);
}
}
/*int main() {
IO();
memset(ans, 0x3f, sizeof ans);
ans[0] = 0;
cin >> n >> k;
for (int u,v,w,i=0; i<n-1; i++) {
cin >> u >> v >> w;
adj[u].PB({v, w}); adj[v].PB({u, w});
}
solve(1);
cout << (tot == maxk ? -1 : tot) << endl;
}*/
int best_path(int n, int k, int H[][2], int L[]){
::n=n; ::k=k;
memset(ans, 0x3f, sizeof ans);
ans[0] = 0;
for (int u,v,w,i=0; i<n-1; i++) {
u = H[i][0], v=H[i][1], w=L[i];
adj[u].PB({v, w}); adj[v].PB({u, w});
}
solve(0);
return (tot == maxk ? -1 : tot);
}
Compilation message
race.cpp: In function 'void getSize(int, int)':
race.cpp:31:22: warning: unused variable 'w' [-Wunused-variable]
31 | int v=it.F, w=it.S;
| ^
race.cpp: In function 'int centroid(int, int, int)':
race.cpp:40:22: warning: unused variable 'w' [-Wunused-variable]
40 | int v=it.F, w=it.S;
| ^
race.cpp: In function 'void solve(int)':
race.cpp:91:22: warning: unused variable 'w' [-Wunused-variable]
91 | int v=it.F, w=it.S;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
4 ms |
8940 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
4 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8940 KB |
Output is correct |
13 |
Correct |
4 ms |
8916 KB |
Output is correct |
14 |
Correct |
6 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
4 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
4 ms |
8940 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
4 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8940 KB |
Output is correct |
13 |
Correct |
4 ms |
8916 KB |
Output is correct |
14 |
Correct |
6 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
4 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8840 KB |
Output is correct |
19 |
Correct |
6 ms |
8916 KB |
Output is correct |
20 |
Correct |
4 ms |
8916 KB |
Output is correct |
21 |
Correct |
6 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
8916 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8916 KB |
Output is correct |
28 |
Correct |
5 ms |
8916 KB |
Output is correct |
29 |
Correct |
5 ms |
8916 KB |
Output is correct |
30 |
Correct |
5 ms |
8916 KB |
Output is correct |
31 |
Correct |
5 ms |
8916 KB |
Output is correct |
32 |
Correct |
6 ms |
8916 KB |
Output is correct |
33 |
Correct |
5 ms |
8916 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
6 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8916 KB |
Output is correct |
38 |
Correct |
5 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
4 ms |
8940 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
4 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8940 KB |
Output is correct |
13 |
Correct |
4 ms |
8916 KB |
Output is correct |
14 |
Correct |
6 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
4 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8840 KB |
Output is correct |
19 |
Correct |
130 ms |
14712 KB |
Output is correct |
20 |
Correct |
122 ms |
16012 KB |
Output is correct |
21 |
Correct |
131 ms |
16576 KB |
Output is correct |
22 |
Correct |
136 ms |
16776 KB |
Output is correct |
23 |
Correct |
103 ms |
16180 KB |
Output is correct |
24 |
Correct |
60 ms |
16156 KB |
Output is correct |
25 |
Correct |
136 ms |
20044 KB |
Output is correct |
26 |
Correct |
106 ms |
24756 KB |
Output is correct |
27 |
Correct |
251 ms |
23316 KB |
Output is correct |
28 |
Correct |
327 ms |
37740 KB |
Output is correct |
29 |
Correct |
392 ms |
36624 KB |
Output is correct |
30 |
Correct |
172 ms |
23280 KB |
Output is correct |
31 |
Correct |
171 ms |
23316 KB |
Output is correct |
32 |
Correct |
238 ms |
23504 KB |
Output is correct |
33 |
Correct |
301 ms |
22088 KB |
Output is correct |
34 |
Correct |
226 ms |
22984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
4 ms |
8940 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
4 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
4 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8940 KB |
Output is correct |
13 |
Correct |
4 ms |
8916 KB |
Output is correct |
14 |
Correct |
6 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
4 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8840 KB |
Output is correct |
19 |
Correct |
6 ms |
8916 KB |
Output is correct |
20 |
Correct |
4 ms |
8916 KB |
Output is correct |
21 |
Correct |
6 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
8916 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8916 KB |
Output is correct |
28 |
Correct |
5 ms |
8916 KB |
Output is correct |
29 |
Correct |
5 ms |
8916 KB |
Output is correct |
30 |
Correct |
5 ms |
8916 KB |
Output is correct |
31 |
Correct |
5 ms |
8916 KB |
Output is correct |
32 |
Correct |
6 ms |
8916 KB |
Output is correct |
33 |
Correct |
5 ms |
8916 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
6 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8916 KB |
Output is correct |
38 |
Correct |
5 ms |
8916 KB |
Output is correct |
39 |
Correct |
130 ms |
14712 KB |
Output is correct |
40 |
Correct |
122 ms |
16012 KB |
Output is correct |
41 |
Correct |
131 ms |
16576 KB |
Output is correct |
42 |
Correct |
136 ms |
16776 KB |
Output is correct |
43 |
Correct |
103 ms |
16180 KB |
Output is correct |
44 |
Correct |
60 ms |
16156 KB |
Output is correct |
45 |
Correct |
136 ms |
20044 KB |
Output is correct |
46 |
Correct |
106 ms |
24756 KB |
Output is correct |
47 |
Correct |
251 ms |
23316 KB |
Output is correct |
48 |
Correct |
327 ms |
37740 KB |
Output is correct |
49 |
Correct |
392 ms |
36624 KB |
Output is correct |
50 |
Correct |
172 ms |
23280 KB |
Output is correct |
51 |
Correct |
171 ms |
23316 KB |
Output is correct |
52 |
Correct |
238 ms |
23504 KB |
Output is correct |
53 |
Correct |
301 ms |
22088 KB |
Output is correct |
54 |
Correct |
226 ms |
22984 KB |
Output is correct |
55 |
Correct |
12 ms |
9684 KB |
Output is correct |
56 |
Correct |
13 ms |
9744 KB |
Output is correct |
57 |
Correct |
93 ms |
17252 KB |
Output is correct |
58 |
Correct |
39 ms |
16716 KB |
Output is correct |
59 |
Correct |
114 ms |
24768 KB |
Output is correct |
60 |
Correct |
392 ms |
40224 KB |
Output is correct |
61 |
Correct |
223 ms |
23664 KB |
Output is correct |
62 |
Correct |
181 ms |
24260 KB |
Output is correct |
63 |
Correct |
236 ms |
24256 KB |
Output is correct |
64 |
Correct |
481 ms |
25808 KB |
Output is correct |
65 |
Correct |
272 ms |
24168 KB |
Output is correct |
66 |
Correct |
466 ms |
34344 KB |
Output is correct |
67 |
Correct |
116 ms |
25732 KB |
Output is correct |
68 |
Correct |
248 ms |
25368 KB |
Output is correct |
69 |
Correct |
296 ms |
25412 KB |
Output is correct |
70 |
Correct |
234 ms |
24628 KB |
Output is correct |