#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;
struct Edge {
int to, w;
};
int k;
vector<vector<Edge>> g;
vector<bool> used;
vector<int> sz;
map<ll, int> mp;
int ans = inf;
void dfs_sz(int v, int p = -1) {
sz[v] = 1;
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_sz(u, v);
sz[v] += sz[u];
}
}
}
void dfs_ans(int v, int p = -1, int sum = 0, int d = 0) {
if (mp.find(k - sum) != mp.end()) {
ans = min(ans, mp[k - sum] + d);
}
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_ans(u, v, sum + w, d + 1);
}
}
}
void dfs_mp(int v, int p = -1, int sum = 0, int d = 0) {
if (mp.find(sum) == mp.end()) {
mp[sum] = d;
} else {
mp[sum] = min(mp[sum], d);
}
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_mp(u, v, sum + w, d + 1);
}
}
}
int centroid(int v, int p, int n) {
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
if (sz[u] * 2 > n) {
return centroid(u, v, n);
}
}
}
return v;
}
void build(int v) {
dfs_sz(v);
mp[0] = 0;
for (auto [u, w] : g[v]) {
if (!used[u]) {
dfs_ans(u, v, w, 1);
dfs_mp(u, v, w, 1);
}
}
mp.clear();
used[v] = 1;
for (auto [u, w] : g[v]) {
if (!used[u]) {
build(centroid(u, v, sz[u]));
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
g.resize(N);
for (int i = 0; i < N - 1; ++i) {
int u = H[i][0], v = H[i][1], w = L[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
used.resize(N);
sz.resize(N);
dfs_sz(0);
build(centroid(0, -1, sz[0]));
if (ans == inf) {
return -1;
} else {
return ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2504 KB |
Output is correct |
24 |
Correct |
2 ms |
2392 KB |
Output is correct |
25 |
Correct |
2 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2500 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
2 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
2400 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
165 ms |
12472 KB |
Output is correct |
20 |
Correct |
159 ms |
12736 KB |
Output is correct |
21 |
Correct |
154 ms |
12376 KB |
Output is correct |
22 |
Correct |
137 ms |
12372 KB |
Output is correct |
23 |
Correct |
199 ms |
12624 KB |
Output is correct |
24 |
Correct |
90 ms |
12628 KB |
Output is correct |
25 |
Correct |
140 ms |
17744 KB |
Output is correct |
26 |
Correct |
71 ms |
18012 KB |
Output is correct |
27 |
Correct |
199 ms |
20652 KB |
Output is correct |
28 |
Correct |
569 ms |
44372 KB |
Output is correct |
29 |
Correct |
544 ms |
43348 KB |
Output is correct |
30 |
Correct |
187 ms |
20816 KB |
Output is correct |
31 |
Correct |
189 ms |
20816 KB |
Output is correct |
32 |
Correct |
221 ms |
20820 KB |
Output is correct |
33 |
Correct |
296 ms |
19536 KB |
Output is correct |
34 |
Correct |
562 ms |
32596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2504 KB |
Output is correct |
24 |
Correct |
2 ms |
2392 KB |
Output is correct |
25 |
Correct |
2 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2500 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
2 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
2400 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2496 KB |
Output is correct |
39 |
Correct |
165 ms |
12472 KB |
Output is correct |
40 |
Correct |
159 ms |
12736 KB |
Output is correct |
41 |
Correct |
154 ms |
12376 KB |
Output is correct |
42 |
Correct |
137 ms |
12372 KB |
Output is correct |
43 |
Correct |
199 ms |
12624 KB |
Output is correct |
44 |
Correct |
90 ms |
12628 KB |
Output is correct |
45 |
Correct |
140 ms |
17744 KB |
Output is correct |
46 |
Correct |
71 ms |
18012 KB |
Output is correct |
47 |
Correct |
199 ms |
20652 KB |
Output is correct |
48 |
Correct |
569 ms |
44372 KB |
Output is correct |
49 |
Correct |
544 ms |
43348 KB |
Output is correct |
50 |
Correct |
187 ms |
20816 KB |
Output is correct |
51 |
Correct |
189 ms |
20816 KB |
Output is correct |
52 |
Correct |
221 ms |
20820 KB |
Output is correct |
53 |
Correct |
296 ms |
19536 KB |
Output is correct |
54 |
Correct |
562 ms |
32596 KB |
Output is correct |
55 |
Correct |
16 ms |
3672 KB |
Output is correct |
56 |
Correct |
9 ms |
3140 KB |
Output is correct |
57 |
Correct |
91 ms |
12704 KB |
Output is correct |
58 |
Correct |
33 ms |
12232 KB |
Output is correct |
59 |
Correct |
222 ms |
23124 KB |
Output is correct |
60 |
Correct |
566 ms |
42636 KB |
Output is correct |
61 |
Correct |
247 ms |
22868 KB |
Output is correct |
62 |
Correct |
195 ms |
20820 KB |
Output is correct |
63 |
Correct |
220 ms |
20820 KB |
Output is correct |
64 |
Correct |
684 ms |
26960 KB |
Output is correct |
65 |
Correct |
580 ms |
33896 KB |
Output is correct |
66 |
Correct |
564 ms |
43140 KB |
Output is correct |
67 |
Correct |
74 ms |
21444 KB |
Output is correct |
68 |
Correct |
309 ms |
31820 KB |
Output is correct |
69 |
Correct |
334 ms |
32332 KB |
Output is correct |
70 |
Correct |
309 ms |
30848 KB |
Output is correct |