#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using lli = long long;
const int MAXN = 2e5 + 10;
const int MAXK = 1e6 + 10;
vector<pair<int, int>> graph[MAXN];
vector<int> subs[MAXN];
int sub[MAXN], dist[MAXK], aprof[MAXN];
bool marc[MAXN];
lli prof[MAXN];
void dfs_sub(int u, int p) {
sub[u] = 1;
for (auto [v, c]: graph[u]) {
if (v == p || marc[v]) continue;
dfs_sub(v, u);
sub[u] += sub[v];
}
}
void dfs_prof(int u, int p, int cor) {
subs[cor].push_back(u);
for (auto [v, c]: graph[u]) {
if (v == p || marc[v]) continue;
prof[v] = prof[u] + c;
aprof[v] = aprof[u] + 1;
dfs_prof(v, u, cor);
}
}
int dfs_cent(int u, int p, int n) {
for (auto [v, c]: graph[u]) {
if (v == p || marc[v]) continue;
if (sub[v] >= n/2) return dfs_cent(v, u, n);
}
return u;
}
int dfs(int u, int p, int n, int k) {
dfs_sub(u, p);
int c = dfs_cent(u, p, sub[u]);
marc[c] = true;
for (int i = 0; i < graph[c].size(); ++i) {
auto [v, cost] = graph[c][i];
if (!marc[v]) {
prof[v] = cost;
aprof[v] = 1;
dfs_prof(v, c, i);
}
}
int ans = 1e9;
dist[0] = 0;
for (int i = 0; i < graph[c].size(); ++i) {
for (int v: subs[i])
if (prof[v] <= k)
ans = min(ans, dist[k - prof[v]] + aprof[v]);
for (int v: subs[i])
if (prof[v] < MAXK) dist[prof[v]] = min(dist[prof[v]], aprof[v]);
}
for (int i = 0; i < graph[c].size(); ++i) {
for (int v: subs[i])
if (prof[v] < MAXK) dist[prof[v]] = 1e9;
subs[i].clear();
}
for (auto [v, cost]: graph[c])
if (!marc[v]) ans = min(ans, dfs(v, c, n, k));
return ans;
}
int best_path(int n, int k, int edges[][2], int costs[]) {
for (int i = 0; i < n-1; ++i) {
graph[edges[i][0]].emplace_back(edges[i][1], costs[i]);
graph[edges[i][1]].emplace_back(edges[i][0], costs[i]);
}
for (int i = 0; i < MAXK; ++i)
dist[i] = 1e9;
int ans = dfs(1, 1, n, k);
if (ans == 1e9) return -1;
return ans;
}
Compilation message
race.cpp: In function 'void dfs_sub(int, int)':
race.cpp:18:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for (auto [v, c]: graph[u]) {
| ^
race.cpp: In function 'void dfs_prof(int, int, int)':
race.cpp:27:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
27 | for (auto [v, c]: graph[u]) {
| ^
race.cpp: In function 'int dfs_cent(int, int, int)':
race.cpp:36:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for (auto [v, c]: graph[u]) {
| ^
race.cpp: In function 'int dfs(int, int, int, int)':
race.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < graph[c].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
race.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
49 | auto [v, cost] = graph[c][i];
| ^
race.cpp:59:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 0; i < graph[c].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
race.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < graph[c].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
race.cpp:73:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | for (auto [v, cost]: graph[c])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
13644 KB |
Output is correct |
2 |
Correct |
8 ms |
13568 KB |
Output is correct |
3 |
Correct |
8 ms |
13644 KB |
Output is correct |
4 |
Correct |
7 ms |
13644 KB |
Output is correct |
5 |
Correct |
7 ms |
13644 KB |
Output is correct |
6 |
Correct |
7 ms |
13772 KB |
Output is correct |
7 |
Correct |
8 ms |
13560 KB |
Output is correct |
8 |
Correct |
7 ms |
13624 KB |
Output is correct |
9 |
Correct |
7 ms |
13644 KB |
Output is correct |
10 |
Correct |
7 ms |
13644 KB |
Output is correct |
11 |
Correct |
8 ms |
13644 KB |
Output is correct |
12 |
Correct |
7 ms |
13644 KB |
Output is correct |
13 |
Correct |
8 ms |
13648 KB |
Output is correct |
14 |
Correct |
7 ms |
13644 KB |
Output is correct |
15 |
Correct |
8 ms |
13644 KB |
Output is correct |
16 |
Correct |
7 ms |
13644 KB |
Output is correct |
17 |
Correct |
10 ms |
13648 KB |
Output is correct |
18 |
Correct |
9 ms |
13644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
13644 KB |
Output is correct |
2 |
Correct |
8 ms |
13568 KB |
Output is correct |
3 |
Correct |
8 ms |
13644 KB |
Output is correct |
4 |
Correct |
7 ms |
13644 KB |
Output is correct |
5 |
Correct |
7 ms |
13644 KB |
Output is correct |
6 |
Correct |
7 ms |
13772 KB |
Output is correct |
7 |
Correct |
8 ms |
13560 KB |
Output is correct |
8 |
Correct |
7 ms |
13624 KB |
Output is correct |
9 |
Correct |
7 ms |
13644 KB |
Output is correct |
10 |
Correct |
7 ms |
13644 KB |
Output is correct |
11 |
Correct |
8 ms |
13644 KB |
Output is correct |
12 |
Correct |
7 ms |
13644 KB |
Output is correct |
13 |
Correct |
8 ms |
13648 KB |
Output is correct |
14 |
Correct |
7 ms |
13644 KB |
Output is correct |
15 |
Correct |
8 ms |
13644 KB |
Output is correct |
16 |
Correct |
7 ms |
13644 KB |
Output is correct |
17 |
Correct |
10 ms |
13648 KB |
Output is correct |
18 |
Correct |
9 ms |
13644 KB |
Output is correct |
19 |
Correct |
7 ms |
13592 KB |
Output is correct |
20 |
Correct |
9 ms |
13644 KB |
Output is correct |
21 |
Correct |
8 ms |
13644 KB |
Output is correct |
22 |
Correct |
7 ms |
13644 KB |
Output is correct |
23 |
Correct |
8 ms |
13644 KB |
Output is correct |
24 |
Correct |
7 ms |
13644 KB |
Output is correct |
25 |
Correct |
8 ms |
13712 KB |
Output is correct |
26 |
Correct |
8 ms |
13696 KB |
Output is correct |
27 |
Correct |
8 ms |
13644 KB |
Output is correct |
28 |
Correct |
8 ms |
13644 KB |
Output is correct |
29 |
Correct |
8 ms |
13644 KB |
Output is correct |
30 |
Correct |
8 ms |
13644 KB |
Output is correct |
31 |
Correct |
8 ms |
13644 KB |
Output is correct |
32 |
Correct |
8 ms |
13644 KB |
Output is correct |
33 |
Correct |
8 ms |
13644 KB |
Output is correct |
34 |
Correct |
9 ms |
13636 KB |
Output is correct |
35 |
Correct |
9 ms |
13644 KB |
Output is correct |
36 |
Correct |
8 ms |
13644 KB |
Output is correct |
37 |
Correct |
8 ms |
13644 KB |
Output is correct |
38 |
Correct |
10 ms |
13620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
13644 KB |
Output is correct |
2 |
Correct |
8 ms |
13568 KB |
Output is correct |
3 |
Correct |
8 ms |
13644 KB |
Output is correct |
4 |
Correct |
7 ms |
13644 KB |
Output is correct |
5 |
Correct |
7 ms |
13644 KB |
Output is correct |
6 |
Correct |
7 ms |
13772 KB |
Output is correct |
7 |
Correct |
8 ms |
13560 KB |
Output is correct |
8 |
Correct |
7 ms |
13624 KB |
Output is correct |
9 |
Correct |
7 ms |
13644 KB |
Output is correct |
10 |
Correct |
7 ms |
13644 KB |
Output is correct |
11 |
Correct |
8 ms |
13644 KB |
Output is correct |
12 |
Correct |
7 ms |
13644 KB |
Output is correct |
13 |
Correct |
8 ms |
13648 KB |
Output is correct |
14 |
Correct |
7 ms |
13644 KB |
Output is correct |
15 |
Correct |
8 ms |
13644 KB |
Output is correct |
16 |
Correct |
7 ms |
13644 KB |
Output is correct |
17 |
Correct |
10 ms |
13648 KB |
Output is correct |
18 |
Correct |
9 ms |
13644 KB |
Output is correct |
19 |
Correct |
159 ms |
20760 KB |
Output is correct |
20 |
Correct |
147 ms |
20836 KB |
Output is correct |
21 |
Correct |
150 ms |
20724 KB |
Output is correct |
22 |
Correct |
167 ms |
20736 KB |
Output is correct |
23 |
Correct |
120 ms |
20924 KB |
Output is correct |
24 |
Correct |
70 ms |
20792 KB |
Output is correct |
25 |
Correct |
200 ms |
27156 KB |
Output is correct |
26 |
Correct |
97 ms |
27816 KB |
Output is correct |
27 |
Correct |
180 ms |
27780 KB |
Output is correct |
28 |
Correct |
510 ms |
44092 KB |
Output is correct |
29 |
Correct |
498 ms |
38880 KB |
Output is correct |
30 |
Correct |
188 ms |
30788 KB |
Output is correct |
31 |
Correct |
185 ms |
30844 KB |
Output is correct |
32 |
Correct |
254 ms |
30788 KB |
Output is correct |
33 |
Correct |
340 ms |
29556 KB |
Output is correct |
34 |
Correct |
284 ms |
30488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
13644 KB |
Output is correct |
2 |
Correct |
8 ms |
13568 KB |
Output is correct |
3 |
Correct |
8 ms |
13644 KB |
Output is correct |
4 |
Correct |
7 ms |
13644 KB |
Output is correct |
5 |
Correct |
7 ms |
13644 KB |
Output is correct |
6 |
Correct |
7 ms |
13772 KB |
Output is correct |
7 |
Correct |
8 ms |
13560 KB |
Output is correct |
8 |
Correct |
7 ms |
13624 KB |
Output is correct |
9 |
Correct |
7 ms |
13644 KB |
Output is correct |
10 |
Correct |
7 ms |
13644 KB |
Output is correct |
11 |
Correct |
8 ms |
13644 KB |
Output is correct |
12 |
Correct |
7 ms |
13644 KB |
Output is correct |
13 |
Correct |
8 ms |
13648 KB |
Output is correct |
14 |
Correct |
7 ms |
13644 KB |
Output is correct |
15 |
Correct |
8 ms |
13644 KB |
Output is correct |
16 |
Correct |
7 ms |
13644 KB |
Output is correct |
17 |
Correct |
10 ms |
13648 KB |
Output is correct |
18 |
Correct |
9 ms |
13644 KB |
Output is correct |
19 |
Correct |
7 ms |
13592 KB |
Output is correct |
20 |
Correct |
9 ms |
13644 KB |
Output is correct |
21 |
Correct |
8 ms |
13644 KB |
Output is correct |
22 |
Correct |
7 ms |
13644 KB |
Output is correct |
23 |
Correct |
8 ms |
13644 KB |
Output is correct |
24 |
Correct |
7 ms |
13644 KB |
Output is correct |
25 |
Correct |
8 ms |
13712 KB |
Output is correct |
26 |
Correct |
8 ms |
13696 KB |
Output is correct |
27 |
Correct |
8 ms |
13644 KB |
Output is correct |
28 |
Correct |
8 ms |
13644 KB |
Output is correct |
29 |
Correct |
8 ms |
13644 KB |
Output is correct |
30 |
Correct |
8 ms |
13644 KB |
Output is correct |
31 |
Correct |
8 ms |
13644 KB |
Output is correct |
32 |
Correct |
8 ms |
13644 KB |
Output is correct |
33 |
Correct |
8 ms |
13644 KB |
Output is correct |
34 |
Correct |
9 ms |
13636 KB |
Output is correct |
35 |
Correct |
9 ms |
13644 KB |
Output is correct |
36 |
Correct |
8 ms |
13644 KB |
Output is correct |
37 |
Correct |
8 ms |
13644 KB |
Output is correct |
38 |
Correct |
10 ms |
13620 KB |
Output is correct |
39 |
Correct |
159 ms |
20760 KB |
Output is correct |
40 |
Correct |
147 ms |
20836 KB |
Output is correct |
41 |
Correct |
150 ms |
20724 KB |
Output is correct |
42 |
Correct |
167 ms |
20736 KB |
Output is correct |
43 |
Correct |
120 ms |
20924 KB |
Output is correct |
44 |
Correct |
70 ms |
20792 KB |
Output is correct |
45 |
Correct |
200 ms |
27156 KB |
Output is correct |
46 |
Correct |
97 ms |
27816 KB |
Output is correct |
47 |
Correct |
180 ms |
27780 KB |
Output is correct |
48 |
Correct |
510 ms |
44092 KB |
Output is correct |
49 |
Correct |
498 ms |
38880 KB |
Output is correct |
50 |
Correct |
188 ms |
30788 KB |
Output is correct |
51 |
Correct |
185 ms |
30844 KB |
Output is correct |
52 |
Correct |
254 ms |
30788 KB |
Output is correct |
53 |
Correct |
340 ms |
29556 KB |
Output is correct |
54 |
Correct |
284 ms |
30488 KB |
Output is correct |
55 |
Correct |
15 ms |
14436 KB |
Output is correct |
56 |
Correct |
20 ms |
14456 KB |
Output is correct |
57 |
Correct |
89 ms |
22232 KB |
Output is correct |
58 |
Correct |
46 ms |
24536 KB |
Output is correct |
59 |
Correct |
102 ms |
29272 KB |
Output is correct |
60 |
Correct |
564 ms |
37496 KB |
Output is correct |
61 |
Correct |
229 ms |
30852 KB |
Output is correct |
62 |
Correct |
219 ms |
30820 KB |
Output is correct |
63 |
Correct |
305 ms |
30876 KB |
Output is correct |
64 |
Correct |
545 ms |
31188 KB |
Output is correct |
65 |
Correct |
350 ms |
31540 KB |
Output is correct |
66 |
Correct |
542 ms |
42880 KB |
Output is correct |
67 |
Correct |
153 ms |
36492 KB |
Output is correct |
68 |
Correct |
260 ms |
31836 KB |
Output is correct |
69 |
Correct |
249 ms |
32076 KB |
Output is correct |
70 |
Correct |
276 ms |
31164 KB |
Output is correct |