// solution
#include <bits/stdc++.h>
#define N_ 200005
#define INF ((ll)1e18+5)
using namespace std;
using ll = long long;
struct Edge { ll to, cost; };
ll n, k, len[N_], dep[N_], ans;
vector<Edge> tree[N_];
map<int, ll> mp[N_];
void dfs(int nd, int par) {
mp[nd][len[nd]] = dep[nd];
for (Edge e : tree[nd]) {
if (e.to == par) continue;
dep[e.to] = dep[nd] + 1;
len[e.to] = len[nd] + e.cost;
dfs(e.to, nd);
if (mp[nd].size() < mp[e.to].size()) {
swap(mp[nd], mp[e.to]);
}
for (auto [x, d] : mp[e.to]) {
ll y = k + 2 * len[nd] - x;
if (mp[nd].count(y)) {
ans = min(ans, d + mp[nd][y] - 2 * dep[nd]);
}
if (!mp[nd].count(x)) mp[nd][x] = d;
else mp[nd][x] = min(mp[nd][x], d);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
for (int i=0; i<n-1; i++) {
tree[H[i][0]].push_back({H[i][1], L[i]});
tree[H[i][1]].push_back({H[i][0], L[i]});
}
ans = INF;
dfs(0, -1);
return ans == INF ? -1 : ans;
}
// grader
// // #include "race.h"
// #include <stdio.h>
// #include <stdlib.h>
// #define MAX_N 500000
// static int N, K;
// static int H[MAX_N][2];
// static int L[MAX_N];
// static int solution;
// inline
// void my_assert(int e) {if (!e) abort();}
// void read_input()
// {
// int i;
// my_assert(2==scanf("%d %d",&N,&K));
// for(i=0; i<N-1; i++)
// my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
// my_assert(1==scanf("%d",&solution));
// }
// int main()
// {
// freopen("race.gir", "r", stdin);
// int ans;
// read_input();
// ans = best_path(N,K,H,L);
// if(ans==solution)
// printf("Correct.\n");
// else
// printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);
// return 0;
// }
Compilation message
race.cpp: In function 'void dfs(int, int)':
race.cpp:28:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
28 | for (auto [x, d] : mp[e.to]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14328 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
9 ms |
14412 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14448 KB |
Output is correct |
9 |
Correct |
9 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
9 ms |
14360 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14412 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14328 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
9 ms |
14412 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14448 KB |
Output is correct |
9 |
Correct |
9 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
9 ms |
14360 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14412 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14440 KB |
Output is correct |
21 |
Correct |
10 ms |
14668 KB |
Output is correct |
22 |
Correct |
10 ms |
14644 KB |
Output is correct |
23 |
Correct |
11 ms |
14668 KB |
Output is correct |
24 |
Correct |
10 ms |
14668 KB |
Output is correct |
25 |
Correct |
10 ms |
14668 KB |
Output is correct |
26 |
Correct |
10 ms |
14668 KB |
Output is correct |
27 |
Correct |
10 ms |
14548 KB |
Output is correct |
28 |
Correct |
10 ms |
14668 KB |
Output is correct |
29 |
Correct |
11 ms |
14616 KB |
Output is correct |
30 |
Correct |
10 ms |
14744 KB |
Output is correct |
31 |
Correct |
10 ms |
14668 KB |
Output is correct |
32 |
Correct |
10 ms |
14668 KB |
Output is correct |
33 |
Correct |
10 ms |
14668 KB |
Output is correct |
34 |
Correct |
10 ms |
14640 KB |
Output is correct |
35 |
Correct |
9 ms |
14668 KB |
Output is correct |
36 |
Correct |
9 ms |
14668 KB |
Output is correct |
37 |
Correct |
9 ms |
14636 KB |
Output is correct |
38 |
Correct |
10 ms |
14688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14328 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
9 ms |
14412 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14448 KB |
Output is correct |
9 |
Correct |
9 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
9 ms |
14360 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14412 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
19 |
Correct |
151 ms |
37752 KB |
Output is correct |
20 |
Correct |
150 ms |
37788 KB |
Output is correct |
21 |
Correct |
148 ms |
37568 KB |
Output is correct |
22 |
Correct |
151 ms |
38472 KB |
Output is correct |
23 |
Correct |
191 ms |
51344 KB |
Output is correct |
24 |
Correct |
152 ms |
41356 KB |
Output is correct |
25 |
Correct |
125 ms |
40020 KB |
Output is correct |
26 |
Correct |
74 ms |
48148 KB |
Output is correct |
27 |
Correct |
230 ms |
52524 KB |
Output is correct |
28 |
Correct |
345 ms |
94908 KB |
Output is correct |
29 |
Correct |
346 ms |
92980 KB |
Output is correct |
30 |
Correct |
230 ms |
52548 KB |
Output is correct |
31 |
Correct |
236 ms |
52500 KB |
Output is correct |
32 |
Correct |
316 ms |
52676 KB |
Output is correct |
33 |
Correct |
282 ms |
57540 KB |
Output is correct |
34 |
Correct |
406 ms |
90436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14328 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
9 ms |
14412 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14448 KB |
Output is correct |
9 |
Correct |
9 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
9 ms |
14360 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14412 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14440 KB |
Output is correct |
21 |
Correct |
10 ms |
14668 KB |
Output is correct |
22 |
Correct |
10 ms |
14644 KB |
Output is correct |
23 |
Correct |
11 ms |
14668 KB |
Output is correct |
24 |
Correct |
10 ms |
14668 KB |
Output is correct |
25 |
Correct |
10 ms |
14668 KB |
Output is correct |
26 |
Correct |
10 ms |
14668 KB |
Output is correct |
27 |
Correct |
10 ms |
14548 KB |
Output is correct |
28 |
Correct |
10 ms |
14668 KB |
Output is correct |
29 |
Correct |
11 ms |
14616 KB |
Output is correct |
30 |
Correct |
10 ms |
14744 KB |
Output is correct |
31 |
Correct |
10 ms |
14668 KB |
Output is correct |
32 |
Correct |
10 ms |
14668 KB |
Output is correct |
33 |
Correct |
10 ms |
14668 KB |
Output is correct |
34 |
Correct |
10 ms |
14640 KB |
Output is correct |
35 |
Correct |
9 ms |
14668 KB |
Output is correct |
36 |
Correct |
9 ms |
14668 KB |
Output is correct |
37 |
Correct |
9 ms |
14636 KB |
Output is correct |
38 |
Correct |
10 ms |
14688 KB |
Output is correct |
39 |
Correct |
151 ms |
37752 KB |
Output is correct |
40 |
Correct |
150 ms |
37788 KB |
Output is correct |
41 |
Correct |
148 ms |
37568 KB |
Output is correct |
42 |
Correct |
151 ms |
38472 KB |
Output is correct |
43 |
Correct |
191 ms |
51344 KB |
Output is correct |
44 |
Correct |
152 ms |
41356 KB |
Output is correct |
45 |
Correct |
125 ms |
40020 KB |
Output is correct |
46 |
Correct |
74 ms |
48148 KB |
Output is correct |
47 |
Correct |
230 ms |
52524 KB |
Output is correct |
48 |
Correct |
345 ms |
94908 KB |
Output is correct |
49 |
Correct |
346 ms |
92980 KB |
Output is correct |
50 |
Correct |
230 ms |
52548 KB |
Output is correct |
51 |
Correct |
236 ms |
52500 KB |
Output is correct |
52 |
Correct |
316 ms |
52676 KB |
Output is correct |
53 |
Correct |
282 ms |
57540 KB |
Output is correct |
54 |
Correct |
406 ms |
90436 KB |
Output is correct |
55 |
Correct |
25 ms |
17868 KB |
Output is correct |
56 |
Correct |
19 ms |
16580 KB |
Output is correct |
57 |
Incorrect |
90 ms |
36648 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |