#include "race.h"
/*
* Author : shora
*/
#include <bits/stdc++.h>
#define print(_v) for (auto &_ : _v) {cerr << _ << ' ';} cerr << endl;
using namespace std;
using ll = long long;
const int oo = 1e9;
const int N = 5e5, MAXK = 1e6;
struct C {
int v, w;
C(int v, int w): v(v), w(w) {}
};
vector<C> G[N];
int vis[N], size[N], f[MAXK + 1], dep[N];
int k;
int res = oo;
void find_size(int u, int e = -1) {
size[u] = 1;
for (C& c : G[u]) if (c.v != e && !vis[c.v]) {
find_size(c.v, u);
size[u] += size[c.v];
}
}
int centroid(int s, int u, int e = -1) {
for (C& c : G[u]) if (c.v != e && !vis[c.v])
if (size[c.v] > s / 2)
return centroid(s, c.v, u);
return u;
}
void get(int u, int e, int dis) {
if (k < dis)
return;
dep[u] = dep[e] + 1;
if (f[k - dis] != oo)
res = min(res, f[k - dis] + dep[u]);
for (C& c : G[u]) if (c.v != e && !vis[c.v]) {
get(c.v, u, dis + c.w);
}
}
void update(int u, int e, int dis) {
if (k < dis)
return;
f[dis] = min(f[dis], dep[u]);
for (C& c : G[u]) if (c.v != e && !vis[c.v]) {
update(c.v, u, dis + c.w);
}
}
void clear(int u, int e = -1, int dis = 0) {
if (k < dis)
return;
dep[u] = 0;
for (C& c : G[u]) if (c.v != e && !vis[c.v]) {
clear(c.v, u, dis + c.w);
}
f[dis] = oo;
}
void solve(int u) {
find_size(u);
u = centroid(size[u], u);
vis[u] = 1;
f[0] = 0;
for (C& c : G[u]) if (!vis[c.v]) {
get(c.v, u, c.w);
update(c.v, u, c.w);
}
clear(u);
for (C& c : G[u]) if (!vis[c.v])
solve(c.v);
}
int best_path(int n, int K, int H[][2], int L[]) {
k = K;
for (int i = 0, u, v, w; i < n-1; ++i) {
u = H[i][0]; v = H[i][1]; w = L[i];
G[u].push_back(C(v, w));
G[v].push_back(C(u, w));
}
for (int i = 1; i <= k; ++i)
f[i] = oo;
solve(0);
return res == oo ? -1 : res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12044 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12080 KB |
Output is correct |
4 |
Correct |
7 ms |
12048 KB |
Output is correct |
5 |
Correct |
7 ms |
12044 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
8 ms |
12052 KB |
Output is correct |
10 |
Correct |
8 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
12040 KB |
Output is correct |
12 |
Correct |
7 ms |
12040 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
11992 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12044 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12080 KB |
Output is correct |
4 |
Correct |
7 ms |
12048 KB |
Output is correct |
5 |
Correct |
7 ms |
12044 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
8 ms |
12052 KB |
Output is correct |
10 |
Correct |
8 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
12040 KB |
Output is correct |
12 |
Correct |
7 ms |
12040 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
11992 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12044 KB |
Output is correct |
19 |
Correct |
7 ms |
12044 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
8 ms |
12108 KB |
Output is correct |
22 |
Correct |
10 ms |
15692 KB |
Output is correct |
23 |
Correct |
9 ms |
15004 KB |
Output is correct |
24 |
Correct |
12 ms |
15464 KB |
Output is correct |
25 |
Correct |
10 ms |
15384 KB |
Output is correct |
26 |
Correct |
9 ms |
13380 KB |
Output is correct |
27 |
Correct |
10 ms |
15180 KB |
Output is correct |
28 |
Correct |
8 ms |
12864 KB |
Output is correct |
29 |
Correct |
8 ms |
13388 KB |
Output is correct |
30 |
Correct |
11 ms |
13540 KB |
Output is correct |
31 |
Correct |
9 ms |
14668 KB |
Output is correct |
32 |
Correct |
10 ms |
14868 KB |
Output is correct |
33 |
Correct |
10 ms |
15180 KB |
Output is correct |
34 |
Correct |
9 ms |
14452 KB |
Output is correct |
35 |
Correct |
9 ms |
15308 KB |
Output is correct |
36 |
Correct |
10 ms |
15740 KB |
Output is correct |
37 |
Correct |
9 ms |
15212 KB |
Output is correct |
38 |
Correct |
9 ms |
14028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12044 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12080 KB |
Output is correct |
4 |
Correct |
7 ms |
12048 KB |
Output is correct |
5 |
Correct |
7 ms |
12044 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
8 ms |
12052 KB |
Output is correct |
10 |
Correct |
8 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
12040 KB |
Output is correct |
12 |
Correct |
7 ms |
12040 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
11992 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12044 KB |
Output is correct |
19 |
Correct |
162 ms |
19132 KB |
Output is correct |
20 |
Correct |
150 ms |
19320 KB |
Output is correct |
21 |
Correct |
170 ms |
19352 KB |
Output is correct |
22 |
Correct |
157 ms |
19344 KB |
Output is correct |
23 |
Correct |
91 ms |
19524 KB |
Output is correct |
24 |
Correct |
68 ms |
19380 KB |
Output is correct |
25 |
Correct |
152 ms |
22084 KB |
Output is correct |
26 |
Correct |
112 ms |
25028 KB |
Output is correct |
27 |
Correct |
188 ms |
25632 KB |
Output is correct |
28 |
Correct |
298 ms |
36748 KB |
Output is correct |
29 |
Correct |
302 ms |
35852 KB |
Output is correct |
30 |
Correct |
191 ms |
25652 KB |
Output is correct |
31 |
Correct |
187 ms |
25540 KB |
Output is correct |
32 |
Correct |
231 ms |
25668 KB |
Output is correct |
33 |
Correct |
280 ms |
24428 KB |
Output is correct |
34 |
Correct |
212 ms |
24264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12044 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12080 KB |
Output is correct |
4 |
Correct |
7 ms |
12048 KB |
Output is correct |
5 |
Correct |
7 ms |
12044 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
8 ms |
12052 KB |
Output is correct |
10 |
Correct |
8 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
12040 KB |
Output is correct |
12 |
Correct |
7 ms |
12040 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
11992 KB |
Output is correct |
16 |
Correct |
7 ms |
11980 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
12044 KB |
Output is correct |
19 |
Correct |
7 ms |
12044 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
8 ms |
12108 KB |
Output is correct |
22 |
Correct |
10 ms |
15692 KB |
Output is correct |
23 |
Correct |
9 ms |
15004 KB |
Output is correct |
24 |
Correct |
12 ms |
15464 KB |
Output is correct |
25 |
Correct |
10 ms |
15384 KB |
Output is correct |
26 |
Correct |
9 ms |
13380 KB |
Output is correct |
27 |
Correct |
10 ms |
15180 KB |
Output is correct |
28 |
Correct |
8 ms |
12864 KB |
Output is correct |
29 |
Correct |
8 ms |
13388 KB |
Output is correct |
30 |
Correct |
11 ms |
13540 KB |
Output is correct |
31 |
Correct |
9 ms |
14668 KB |
Output is correct |
32 |
Correct |
10 ms |
14868 KB |
Output is correct |
33 |
Correct |
10 ms |
15180 KB |
Output is correct |
34 |
Correct |
9 ms |
14452 KB |
Output is correct |
35 |
Correct |
9 ms |
15308 KB |
Output is correct |
36 |
Correct |
10 ms |
15740 KB |
Output is correct |
37 |
Correct |
9 ms |
15212 KB |
Output is correct |
38 |
Correct |
9 ms |
14028 KB |
Output is correct |
39 |
Correct |
162 ms |
19132 KB |
Output is correct |
40 |
Correct |
150 ms |
19320 KB |
Output is correct |
41 |
Correct |
170 ms |
19352 KB |
Output is correct |
42 |
Correct |
157 ms |
19344 KB |
Output is correct |
43 |
Correct |
91 ms |
19524 KB |
Output is correct |
44 |
Correct |
68 ms |
19380 KB |
Output is correct |
45 |
Correct |
152 ms |
22084 KB |
Output is correct |
46 |
Correct |
112 ms |
25028 KB |
Output is correct |
47 |
Correct |
188 ms |
25632 KB |
Output is correct |
48 |
Correct |
298 ms |
36748 KB |
Output is correct |
49 |
Correct |
302 ms |
35852 KB |
Output is correct |
50 |
Correct |
191 ms |
25652 KB |
Output is correct |
51 |
Correct |
187 ms |
25540 KB |
Output is correct |
52 |
Correct |
231 ms |
25668 KB |
Output is correct |
53 |
Correct |
280 ms |
24428 KB |
Output is correct |
54 |
Correct |
212 ms |
24264 KB |
Output is correct |
55 |
Correct |
17 ms |
12824 KB |
Output is correct |
56 |
Correct |
18 ms |
12696 KB |
Output is correct |
57 |
Correct |
100 ms |
19656 KB |
Output is correct |
58 |
Correct |
46 ms |
19348 KB |
Output is correct |
59 |
Correct |
130 ms |
25756 KB |
Output is correct |
60 |
Correct |
604 ms |
41732 KB |
Output is correct |
61 |
Correct |
210 ms |
27332 KB |
Output is correct |
62 |
Correct |
235 ms |
31140 KB |
Output is correct |
63 |
Correct |
345 ms |
31384 KB |
Output is correct |
64 |
Correct |
662 ms |
29548 KB |
Output is correct |
65 |
Correct |
276 ms |
28104 KB |
Output is correct |
66 |
Correct |
502 ms |
39748 KB |
Output is correct |
67 |
Correct |
195 ms |
31748 KB |
Output is correct |
68 |
Correct |
323 ms |
31772 KB |
Output is correct |
69 |
Correct |
334 ms |
32136 KB |
Output is correct |
70 |
Correct |
320 ms |
31200 KB |
Output is correct |