#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
const int N = 200000 + 7;
int n, k, best, sz[N], total;
ll dist[N];
bool vis[N];
vector<pair<int, int>> g[N];
void build(int a, int p = -1) {
sz[a] = 1;
for (auto &edge : g[a]) {
int b = edge.first, cost = edge.second;
if (b != p && !vis[b]) {
build(b, a);
sz[a] += sz[b];
}
}
}
int fcen(int a, int p = -1) {
int mx = total - sz[a];
for (auto &edge : g[a]) {
int b = edge.first, cost = edge.second;
if (b != p && !vis[b]) {
mx = max(mx, sz[b]);
}
}
if (mx <= total / 2) {
return a;
}
for (auto &edge : g[a]) {
int b = edge.first, cost = edge.second;
if (b != p && !vis[b] && mx == sz[b]) {
return fcen(b, a);
}
}
assert(0);
}
vector<pair<ll, int>> dists;
void dfs(int a, int p, int step) {
dists.push_back({dist[a], step});
for (auto &edge : g[a]) {
int b = edge.first, cost = edge.second;
if (b != p && !vis[b]) {
dist[b] = dist[a] + cost;
dfs(b, a, step + 1);
}
}
}
void solve(int a) {
build(a);
total = sz[a];
a = fcen(a);
vis[a] = 1;
map<ll, int> mn;
for (auto &edge : g[a]) {
int b = edge.first, cost = edge.second;
if (!vis[b]) {
dists.clear();
dist[b] = cost;
dfs(b, a, 1);
for (auto &val : dists) {
int other = k - val.first;
if (val.first == k) {
best = min(best, val.second);
}
if (mn.count(other)) {
best = min(best, mn[other] + val.second);
}
}
for (auto &val : dists) {
if (!mn.count(val.first)) {
mn[val.first] = val.second;
} else {
mn[val.first] = min(mn[val.first], val.second);
}
}
}
}
for (auto &edge : g[a]) {
int b = edge.first, cost = edge.second;
if (!vis[b]) {
solve(b);
}
}
}
int best_path(int nodes, int target, int edges[][2], int len_edges[]) {
best = N;
n = nodes;
k = target;
for (int i = 0; i < n; i++) {
vis[i] = 0;
g[i].clear();
}
for (int i = 0; i < n - 1; i++) {
g[edges[i][0]].push_back({edges[i][1], len_edges[i]});
g[edges[i][1]].push_back({edges[i][0], len_edges[i]});
}
solve(0);
if (best == N) {
best = -1;
}
return best;
}
Compilation message
race.cpp: In function 'void build(int, int)':
race.cpp:16:25: warning: unused variable 'cost' [-Wunused-variable]
16 | int b = edge.first, cost = edge.second;
| ^~~~
race.cpp: In function 'int fcen(int, int)':
race.cpp:27:25: warning: unused variable 'cost' [-Wunused-variable]
27 | int b = edge.first, cost = edge.second;
| ^~~~
race.cpp:36:25: warning: unused variable 'cost' [-Wunused-variable]
36 | int b = edge.first, cost = edge.second;
| ^~~~
race.cpp: In function 'void solve(int)':
race.cpp:88:25: warning: unused variable 'cost' [-Wunused-variable]
88 | int b = edge.first, cost = edge.second;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5008 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4980 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5008 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5008 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4980 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5008 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
5152 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5152 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
5 ms |
5024 KB |
Output is correct |
28 |
Correct |
5 ms |
5152 KB |
Output is correct |
29 |
Correct |
6 ms |
5196 KB |
Output is correct |
30 |
Correct |
5 ms |
5148 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5196 KB |
Output is correct |
33 |
Correct |
5 ms |
5196 KB |
Output is correct |
34 |
Correct |
5 ms |
5196 KB |
Output is correct |
35 |
Correct |
5 ms |
5196 KB |
Output is correct |
36 |
Correct |
5 ms |
5196 KB |
Output is correct |
37 |
Correct |
5 ms |
5196 KB |
Output is correct |
38 |
Correct |
5 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5008 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4980 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5008 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
261 ms |
13632 KB |
Output is correct |
20 |
Correct |
253 ms |
13588 KB |
Output is correct |
21 |
Correct |
234 ms |
13632 KB |
Output is correct |
22 |
Correct |
256 ms |
13696 KB |
Output is correct |
23 |
Correct |
319 ms |
13888 KB |
Output is correct |
24 |
Correct |
149 ms |
13328 KB |
Output is correct |
25 |
Correct |
236 ms |
18112 KB |
Output is correct |
26 |
Correct |
122 ms |
20620 KB |
Output is correct |
27 |
Correct |
322 ms |
20676 KB |
Output is correct |
28 |
Correct |
966 ms |
61288 KB |
Output is correct |
29 |
Correct |
1010 ms |
60104 KB |
Output is correct |
30 |
Correct |
324 ms |
20644 KB |
Output is correct |
31 |
Correct |
322 ms |
20644 KB |
Output is correct |
32 |
Correct |
396 ms |
20868 KB |
Output is correct |
33 |
Correct |
556 ms |
21320 KB |
Output is correct |
34 |
Correct |
909 ms |
43316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5008 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5012 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4980 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5008 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
5152 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5152 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
5 ms |
5024 KB |
Output is correct |
28 |
Correct |
5 ms |
5152 KB |
Output is correct |
29 |
Correct |
6 ms |
5196 KB |
Output is correct |
30 |
Correct |
5 ms |
5148 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5196 KB |
Output is correct |
33 |
Correct |
5 ms |
5196 KB |
Output is correct |
34 |
Correct |
5 ms |
5196 KB |
Output is correct |
35 |
Correct |
5 ms |
5196 KB |
Output is correct |
36 |
Correct |
5 ms |
5196 KB |
Output is correct |
37 |
Correct |
5 ms |
5196 KB |
Output is correct |
38 |
Correct |
5 ms |
5196 KB |
Output is correct |
39 |
Correct |
261 ms |
13632 KB |
Output is correct |
40 |
Correct |
253 ms |
13588 KB |
Output is correct |
41 |
Correct |
234 ms |
13632 KB |
Output is correct |
42 |
Correct |
256 ms |
13696 KB |
Output is correct |
43 |
Correct |
319 ms |
13888 KB |
Output is correct |
44 |
Correct |
149 ms |
13328 KB |
Output is correct |
45 |
Correct |
236 ms |
18112 KB |
Output is correct |
46 |
Correct |
122 ms |
20620 KB |
Output is correct |
47 |
Correct |
322 ms |
20676 KB |
Output is correct |
48 |
Correct |
966 ms |
61288 KB |
Output is correct |
49 |
Correct |
1010 ms |
60104 KB |
Output is correct |
50 |
Correct |
324 ms |
20644 KB |
Output is correct |
51 |
Correct |
322 ms |
20644 KB |
Output is correct |
52 |
Correct |
396 ms |
20868 KB |
Output is correct |
53 |
Correct |
556 ms |
21320 KB |
Output is correct |
54 |
Correct |
909 ms |
43316 KB |
Output is correct |
55 |
Correct |
25 ms |
6560 KB |
Output is correct |
56 |
Correct |
17 ms |
5964 KB |
Output is correct |
57 |
Correct |
150 ms |
13272 KB |
Output is correct |
58 |
Correct |
52 ms |
12356 KB |
Output is correct |
59 |
Correct |
336 ms |
29952 KB |
Output is correct |
60 |
Correct |
1037 ms |
58484 KB |
Output is correct |
61 |
Correct |
363 ms |
22468 KB |
Output is correct |
62 |
Correct |
353 ms |
20628 KB |
Output is correct |
63 |
Correct |
385 ms |
20676 KB |
Output is correct |
64 |
Correct |
1006 ms |
36864 KB |
Output is correct |
65 |
Correct |
891 ms |
44596 KB |
Output is correct |
66 |
Correct |
1006 ms |
57724 KB |
Output is correct |
67 |
Correct |
157 ms |
21128 KB |
Output is correct |
68 |
Correct |
527 ms |
38336 KB |
Output is correct |
69 |
Correct |
537 ms |
39128 KB |
Output is correct |
70 |
Correct |
507 ms |
37112 KB |
Output is correct |