#include "race.h"
#include <bits/stdc++.h>
using namespace std;
constexpr int NMAX = 2e5 + 5;
constexpr int VALMAX = 1e6 + 5;
constexpr int INF = 1e9;
typedef pair <int, int> PII;
int ans;
vector <PII> G[NMAX];
int Dist;
int Sz[NMAX];
bool sel[NMAX];
int cnt = 0;
PII fr[VALMAX];
void AddToSolution (int Node, int dad, int cost, int Length) {
if (cost > Dist) return;
int remain = Dist - cost;
if (fr[remain].first == cnt)
ans = min(ans, fr[remain].second + Length);
for (auto it : G[Node]) {
int to = it.first;
int c = it.second;
if (to == dad || sel[to]) continue;
AddToSolution(to, Node, cost + c, Length+1);
}
}
void ChangeFR (int Node, int dad, int cost, int Length) {
if (cost > Dist) return;
if (fr[cost].first == cnt)
fr[cost].second = min(fr[cost].second, Length);
else {
fr[cost].first = cnt;
fr[cost].second = Length;
}
for (auto it : G[Node]) {
int to = it.first;
int c = it.second;
if (to == dad || sel[to]) continue;
ChangeFR(to, Node, cost + c, Length+1);
}
}
void Dfs (int Node, int dad = -1) {
Sz[Node] = 1;
for (auto it : G[Node]) {
int to = it.first;
if (to == dad || sel[to]) continue;
Dfs(to, Node);
Sz[Node] += Sz[to];
}
}
int Size;
int centr;
int Centroid (int Node, int dad = -1) {
int Max = Size - Sz[Node];
for (auto it : G[Node]) {
int to = it.first;
if (to == dad || sel[to]) continue;
int x = Centroid(to, Node);
if (x != -1) return x;
Max = max(Max, Sz[to]);
}
if (Max <= (Size + 1) / 2) return Node;
return -1;
}
void Get (int Root) {
Dfs(Root);
Size = Sz[Root];
centr = Centroid(Root);
sel[centr] = 1;
++ cnt;
fr[0].first = cnt;
fr[0].second = 0;
for (auto it : G[centr]) {
int to = it.first;
if (sel[to]) continue;
AddToSolution(to, centr, it.second, 1);
ChangeFR(to, centr, it.second, 1);
}
for (auto it : G[centr]) {
int to = it.first;
if (sel[to]) continue;
Get(to);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
ans = INF;
Dist = K;
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]});
}
Get(0);
if (ans == INF) return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
8412 KB |
Output is correct |
23 |
Correct |
6 ms |
8012 KB |
Output is correct |
24 |
Correct |
6 ms |
9164 KB |
Output is correct |
25 |
Correct |
5 ms |
8140 KB |
Output is correct |
26 |
Correct |
4 ms |
7372 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
6604 KB |
Output is correct |
29 |
Correct |
5 ms |
7372 KB |
Output is correct |
30 |
Correct |
5 ms |
7756 KB |
Output is correct |
31 |
Correct |
5 ms |
8348 KB |
Output is correct |
32 |
Correct |
5 ms |
8396 KB |
Output is correct |
33 |
Correct |
6 ms |
9804 KB |
Output is correct |
34 |
Correct |
6 ms |
9036 KB |
Output is correct |
35 |
Correct |
7 ms |
10112 KB |
Output is correct |
36 |
Correct |
6 ms |
10444 KB |
Output is correct |
37 |
Correct |
6 ms |
8268 KB |
Output is correct |
38 |
Correct |
5 ms |
7152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
132 ms |
10260 KB |
Output is correct |
20 |
Correct |
131 ms |
10308 KB |
Output is correct |
21 |
Correct |
139 ms |
10244 KB |
Output is correct |
22 |
Correct |
142 ms |
10448 KB |
Output is correct |
23 |
Correct |
74 ms |
10568 KB |
Output is correct |
24 |
Correct |
60 ms |
10472 KB |
Output is correct |
25 |
Correct |
149 ms |
13956 KB |
Output is correct |
26 |
Correct |
90 ms |
17684 KB |
Output is correct |
27 |
Correct |
215 ms |
15904 KB |
Output is correct |
28 |
Correct |
361 ms |
30152 KB |
Output is correct |
29 |
Correct |
354 ms |
29080 KB |
Output is correct |
30 |
Correct |
189 ms |
15876 KB |
Output is correct |
31 |
Correct |
172 ms |
15872 KB |
Output is correct |
32 |
Correct |
255 ms |
15896 KB |
Output is correct |
33 |
Correct |
278 ms |
14768 KB |
Output is correct |
34 |
Correct |
247 ms |
14620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
8412 KB |
Output is correct |
23 |
Correct |
6 ms |
8012 KB |
Output is correct |
24 |
Correct |
6 ms |
9164 KB |
Output is correct |
25 |
Correct |
5 ms |
8140 KB |
Output is correct |
26 |
Correct |
4 ms |
7372 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
6604 KB |
Output is correct |
29 |
Correct |
5 ms |
7372 KB |
Output is correct |
30 |
Correct |
5 ms |
7756 KB |
Output is correct |
31 |
Correct |
5 ms |
8348 KB |
Output is correct |
32 |
Correct |
5 ms |
8396 KB |
Output is correct |
33 |
Correct |
6 ms |
9804 KB |
Output is correct |
34 |
Correct |
6 ms |
9036 KB |
Output is correct |
35 |
Correct |
7 ms |
10112 KB |
Output is correct |
36 |
Correct |
6 ms |
10444 KB |
Output is correct |
37 |
Correct |
6 ms |
8268 KB |
Output is correct |
38 |
Correct |
5 ms |
7152 KB |
Output is correct |
39 |
Correct |
132 ms |
10260 KB |
Output is correct |
40 |
Correct |
131 ms |
10308 KB |
Output is correct |
41 |
Correct |
139 ms |
10244 KB |
Output is correct |
42 |
Correct |
142 ms |
10448 KB |
Output is correct |
43 |
Correct |
74 ms |
10568 KB |
Output is correct |
44 |
Correct |
60 ms |
10472 KB |
Output is correct |
45 |
Correct |
149 ms |
13956 KB |
Output is correct |
46 |
Correct |
90 ms |
17684 KB |
Output is correct |
47 |
Correct |
215 ms |
15904 KB |
Output is correct |
48 |
Correct |
361 ms |
30152 KB |
Output is correct |
49 |
Correct |
354 ms |
29080 KB |
Output is correct |
50 |
Correct |
189 ms |
15876 KB |
Output is correct |
51 |
Correct |
172 ms |
15872 KB |
Output is correct |
52 |
Correct |
255 ms |
15896 KB |
Output is correct |
53 |
Correct |
278 ms |
14768 KB |
Output is correct |
54 |
Correct |
247 ms |
14620 KB |
Output is correct |
55 |
Correct |
11 ms |
5580 KB |
Output is correct |
56 |
Correct |
13 ms |
5548 KB |
Output is correct |
57 |
Correct |
77 ms |
10476 KB |
Output is correct |
58 |
Correct |
32 ms |
10472 KB |
Output is correct |
59 |
Correct |
100 ms |
19740 KB |
Output is correct |
60 |
Correct |
442 ms |
37068 KB |
Output is correct |
61 |
Correct |
186 ms |
19004 KB |
Output is correct |
62 |
Correct |
190 ms |
18988 KB |
Output is correct |
63 |
Correct |
282 ms |
18848 KB |
Output is correct |
64 |
Correct |
518 ms |
21316 KB |
Output is correct |
65 |
Correct |
262 ms |
19704 KB |
Output is correct |
66 |
Correct |
417 ms |
37500 KB |
Output is correct |
67 |
Correct |
120 ms |
19628 KB |
Output is correct |
68 |
Correct |
317 ms |
24864 KB |
Output is correct |
69 |
Correct |
285 ms |
25132 KB |
Output is correct |
70 |
Correct |
281 ms |
24232 KB |
Output is correct |