#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
vector<pair<int, int>> g[200005];
bool processed[200005];
ll sz[200005];
int getSize(int cur, int par = -1) {
sz[cur] = 1;
for (auto next : g[cur]) {
if (next.first != par && !processed[next.first]) sz[cur] += getSize(next.first, cur);
}
return sz[cur];
}
int getCentroid(int cur, int des, int par = -1) {
for (auto next : g[cur]) {
if (next.first != par && !processed[next.first] && sz[next.first] > des / 2) return getCentroid(next.first, des, cur);
}
return cur;
}
ll ans = 10e7, k, depths[1000005];
vector<int> v;
void solve(int cur, int par, ll depth, ll sum, int t) {
if (sum > k || depth >= ans) return;
if (t == 0) ans = min(ans, depth + depths[k - sum]);
if (t == 1) {
depths[sum] = min(depths[sum], depth);
v.push_back(sum);
}
for (auto next : g[cur]) {
if (next.first != par && !processed[next.first]) {
solve(next.first, cur, depth + 1, sum + next.second, t);
}
}
}
void decompose(int cur = 0) {
int centroid = getCentroid(cur, getSize(cur));
processed[centroid] = 1;
for (auto x : v) depths[x] = 10e7;
v.clear();
depths[0] = 0;
for (auto next : g[centroid]) {
if (!processed[next.first]) {
solve(next.first, -1, 1, next.second, 0);
solve(next.first, -1, 1, next.second, 1);
}
}
for (auto next : g[centroid]) {
if (!processed[next.first]) decompose(next.first);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
for (int i = 0; i <= k; i++) depths[i] = 10e7;
ans = N;
for (int i = 0; i < N - 1; i++) {
g[H[i][0]].push_back({H[i][1], L[i]});
g[H[i][1]].push_back({H[i][0], L[i]});
}
decompose();
if (ans == N) return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5012 KB |
Output is correct |
16 |
Correct |
3 ms |
5012 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5012 KB |
Output is correct |
16 |
Correct |
3 ms |
5012 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
5012 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5024 KB |
Output is correct |
22 |
Correct |
6 ms |
12244 KB |
Output is correct |
23 |
Correct |
6 ms |
10964 KB |
Output is correct |
24 |
Correct |
7 ms |
11752 KB |
Output is correct |
25 |
Correct |
8 ms |
11604 KB |
Output is correct |
26 |
Correct |
5 ms |
7716 KB |
Output is correct |
27 |
Correct |
6 ms |
11348 KB |
Output is correct |
28 |
Correct |
6 ms |
6740 KB |
Output is correct |
29 |
Correct |
4 ms |
7636 KB |
Output is correct |
30 |
Correct |
4 ms |
7892 KB |
Output is correct |
31 |
Correct |
5 ms |
10148 KB |
Output is correct |
32 |
Correct |
5 ms |
10656 KB |
Output is correct |
33 |
Correct |
6 ms |
11220 KB |
Output is correct |
34 |
Correct |
5 ms |
9628 KB |
Output is correct |
35 |
Correct |
6 ms |
11348 KB |
Output is correct |
36 |
Correct |
8 ms |
12372 KB |
Output is correct |
37 |
Correct |
6 ms |
11220 KB |
Output is correct |
38 |
Correct |
5 ms |
9044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5012 KB |
Output is correct |
16 |
Correct |
3 ms |
5012 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
79 ms |
11976 KB |
Output is correct |
20 |
Correct |
69 ms |
12088 KB |
Output is correct |
21 |
Correct |
88 ms |
12160 KB |
Output is correct |
22 |
Correct |
70 ms |
12388 KB |
Output is correct |
23 |
Correct |
46 ms |
12360 KB |
Output is correct |
24 |
Correct |
37 ms |
12236 KB |
Output is correct |
25 |
Correct |
105 ms |
14764 KB |
Output is correct |
26 |
Correct |
74 ms |
18388 KB |
Output is correct |
27 |
Correct |
111 ms |
19588 KB |
Output is correct |
28 |
Correct |
209 ms |
30920 KB |
Output is correct |
29 |
Correct |
162 ms |
29996 KB |
Output is correct |
30 |
Correct |
118 ms |
19532 KB |
Output is correct |
31 |
Correct |
113 ms |
19608 KB |
Output is correct |
32 |
Correct |
178 ms |
19692 KB |
Output is correct |
33 |
Correct |
135 ms |
18480 KB |
Output is correct |
34 |
Correct |
120 ms |
19268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5012 KB |
Output is correct |
16 |
Correct |
3 ms |
5012 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
5012 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5024 KB |
Output is correct |
22 |
Correct |
6 ms |
12244 KB |
Output is correct |
23 |
Correct |
6 ms |
10964 KB |
Output is correct |
24 |
Correct |
7 ms |
11752 KB |
Output is correct |
25 |
Correct |
8 ms |
11604 KB |
Output is correct |
26 |
Correct |
5 ms |
7716 KB |
Output is correct |
27 |
Correct |
6 ms |
11348 KB |
Output is correct |
28 |
Correct |
6 ms |
6740 KB |
Output is correct |
29 |
Correct |
4 ms |
7636 KB |
Output is correct |
30 |
Correct |
4 ms |
7892 KB |
Output is correct |
31 |
Correct |
5 ms |
10148 KB |
Output is correct |
32 |
Correct |
5 ms |
10656 KB |
Output is correct |
33 |
Correct |
6 ms |
11220 KB |
Output is correct |
34 |
Correct |
5 ms |
9628 KB |
Output is correct |
35 |
Correct |
6 ms |
11348 KB |
Output is correct |
36 |
Correct |
8 ms |
12372 KB |
Output is correct |
37 |
Correct |
6 ms |
11220 KB |
Output is correct |
38 |
Correct |
5 ms |
9044 KB |
Output is correct |
39 |
Correct |
79 ms |
11976 KB |
Output is correct |
40 |
Correct |
69 ms |
12088 KB |
Output is correct |
41 |
Correct |
88 ms |
12160 KB |
Output is correct |
42 |
Correct |
70 ms |
12388 KB |
Output is correct |
43 |
Correct |
46 ms |
12360 KB |
Output is correct |
44 |
Correct |
37 ms |
12236 KB |
Output is correct |
45 |
Correct |
105 ms |
14764 KB |
Output is correct |
46 |
Correct |
74 ms |
18388 KB |
Output is correct |
47 |
Correct |
111 ms |
19588 KB |
Output is correct |
48 |
Correct |
209 ms |
30920 KB |
Output is correct |
49 |
Correct |
162 ms |
29996 KB |
Output is correct |
50 |
Correct |
118 ms |
19532 KB |
Output is correct |
51 |
Correct |
113 ms |
19608 KB |
Output is correct |
52 |
Correct |
178 ms |
19692 KB |
Output is correct |
53 |
Correct |
135 ms |
18480 KB |
Output is correct |
54 |
Correct |
120 ms |
19268 KB |
Output is correct |
55 |
Correct |
8 ms |
5792 KB |
Output is correct |
56 |
Correct |
9 ms |
5716 KB |
Output is correct |
57 |
Correct |
56 ms |
12780 KB |
Output is correct |
58 |
Correct |
28 ms |
12948 KB |
Output is correct |
59 |
Correct |
71 ms |
19732 KB |
Output is correct |
60 |
Correct |
316 ms |
38980 KB |
Output is correct |
61 |
Correct |
110 ms |
19968 KB |
Output is correct |
62 |
Correct |
122 ms |
28364 KB |
Output is correct |
63 |
Correct |
150 ms |
28464 KB |
Output is correct |
64 |
Correct |
304 ms |
24780 KB |
Output is correct |
65 |
Correct |
125 ms |
20428 KB |
Output is correct |
66 |
Correct |
315 ms |
35956 KB |
Output is correct |
67 |
Correct |
77 ms |
29816 KB |
Output is correct |
68 |
Correct |
121 ms |
28108 KB |
Output is correct |
69 |
Correct |
127 ms |
28340 KB |
Output is correct |
70 |
Correct |
112 ms |
27468 KB |
Output is correct |