#include "race.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<array<int, 2>>> adj;
int n, k, ans = 2e9, nodes;
vector<int> sub;
bitset<200000> is_cent;
map<int, int> mp2;
void precalc (int u, int p = -1) {
sub[u] = 1;
++nodes;
for (auto &v : adj[u]) {
if (!is_cent[v[0]] && v[0] ^ p) {
precalc(v[0], u);
sub[u] += sub[v[0]];
}
}
}
int centroid (int u, int p = -1) {
for (auto &v : adj[u]) {
if (!is_cent[v[0]] && v[0] ^ p) {
if (sub[v[0]] > nodes / 2) {
return centroid(v[0], u);
}
}
}
return u;
}
void update (map<int, int> &m, int k, int v) {
if (m.count(k)) {
m[k] = min(m[k], v);
}
else {
m[k] = v;
}
}
void dfs (int u, int p, int sum, int d) {
if (sum > k) {
return;
}
update(mp2, sum, d);
for (auto &v : adj[u]) {
if (!is_cent[v[0]] && v[0] ^ p) {
dfs(v[0], u, sum + v[1], d + 1);
}
}
}
void decompose (int u = 0) {
nodes = 0;
precalc(u);
int cent = centroid(u);
map<int, int> mp;
mp[0] = 0;
is_cent[cent] = 1;
for (auto &v : adj[cent]) {
if (is_cent[v[0]]) {
continue;
}
mp2.clear();
dfs(v[0], cent, v[1], 1);
for (auto &p : mp2) {
if (mp.count(k - p.first)) {
ans = min(ans, p.second + mp[k - p.first]);
}
}
for (auto &p : mp2) {
update(mp, p.first, p.second);
}
}
for (auto &v : adj[cent]) {
if (!is_cent[v[0]]) {
decompose(v[0]);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
adj.resize(n);
sub.resize(n);
for (int i = 0; i < n - 1; ++i) {
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
decompose();
return ans == 2e9 ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
304 KB |
Output is correct |
28 |
Correct |
3 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
4 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
476 KB |
Output is correct |
32 |
Correct |
3 ms |
444 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
332 KB |
Output is correct |
35 |
Correct |
2 ms |
332 KB |
Output is correct |
36 |
Correct |
2 ms |
332 KB |
Output is correct |
37 |
Correct |
2 ms |
436 KB |
Output is correct |
38 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
246 ms |
9288 KB |
Output is correct |
20 |
Correct |
217 ms |
9168 KB |
Output is correct |
21 |
Correct |
206 ms |
9320 KB |
Output is correct |
22 |
Correct |
221 ms |
9396 KB |
Output is correct |
23 |
Correct |
90 ms |
9588 KB |
Output is correct |
24 |
Correct |
111 ms |
9412 KB |
Output is correct |
25 |
Correct |
151 ms |
12044 KB |
Output is correct |
26 |
Correct |
95 ms |
14860 KB |
Output is correct |
27 |
Correct |
235 ms |
18500 KB |
Output is correct |
28 |
Correct |
279 ms |
29976 KB |
Output is correct |
29 |
Correct |
261 ms |
29000 KB |
Output is correct |
30 |
Correct |
233 ms |
18588 KB |
Output is correct |
31 |
Correct |
220 ms |
18588 KB |
Output is correct |
32 |
Correct |
235 ms |
18624 KB |
Output is correct |
33 |
Correct |
409 ms |
17592 KB |
Output is correct |
34 |
Correct |
218 ms |
18344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
304 KB |
Output is correct |
28 |
Correct |
3 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
4 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
476 KB |
Output is correct |
32 |
Correct |
3 ms |
444 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
332 KB |
Output is correct |
35 |
Correct |
2 ms |
332 KB |
Output is correct |
36 |
Correct |
2 ms |
332 KB |
Output is correct |
37 |
Correct |
2 ms |
436 KB |
Output is correct |
38 |
Correct |
3 ms |
460 KB |
Output is correct |
39 |
Correct |
246 ms |
9288 KB |
Output is correct |
40 |
Correct |
217 ms |
9168 KB |
Output is correct |
41 |
Correct |
206 ms |
9320 KB |
Output is correct |
42 |
Correct |
221 ms |
9396 KB |
Output is correct |
43 |
Correct |
90 ms |
9588 KB |
Output is correct |
44 |
Correct |
111 ms |
9412 KB |
Output is correct |
45 |
Correct |
151 ms |
12044 KB |
Output is correct |
46 |
Correct |
95 ms |
14860 KB |
Output is correct |
47 |
Correct |
235 ms |
18500 KB |
Output is correct |
48 |
Correct |
279 ms |
29976 KB |
Output is correct |
49 |
Correct |
261 ms |
29000 KB |
Output is correct |
50 |
Correct |
233 ms |
18588 KB |
Output is correct |
51 |
Correct |
220 ms |
18588 KB |
Output is correct |
52 |
Correct |
235 ms |
18624 KB |
Output is correct |
53 |
Correct |
409 ms |
17592 KB |
Output is correct |
54 |
Correct |
218 ms |
18344 KB |
Output is correct |
55 |
Correct |
25 ms |
1460 KB |
Output is correct |
56 |
Correct |
17 ms |
1104 KB |
Output is correct |
57 |
Correct |
148 ms |
9516 KB |
Output is correct |
58 |
Correct |
65 ms |
9112 KB |
Output is correct |
59 |
Correct |
469 ms |
22136 KB |
Output is correct |
60 |
Correct |
1264 ms |
46448 KB |
Output is correct |
61 |
Correct |
339 ms |
19012 KB |
Output is correct |
62 |
Correct |
367 ms |
18740 KB |
Output is correct |
63 |
Correct |
426 ms |
19004 KB |
Output is correct |
64 |
Correct |
1213 ms |
30056 KB |
Output is correct |
65 |
Correct |
223 ms |
19640 KB |
Output is correct |
66 |
Correct |
818 ms |
28612 KB |
Output is correct |
67 |
Correct |
133 ms |
19408 KB |
Output is correct |
68 |
Correct |
486 ms |
26084 KB |
Output is correct |
69 |
Correct |
471 ms |
26484 KB |
Output is correct |
70 |
Correct |
449 ms |
25408 KB |
Output is correct |