#include <bits/stdc++.h>
using namespace std;
#include "race.h"
#define int long long
const int MX = 2e5 + 10;
int n, k, ans = 1e9, timer = 0, tin[MX], tout[MX], rev[MX], d[MX], ed[MX], siz[MX], bigc[MX];
vector<pair<int,int>> adj[MX];
map<int, int> mnedge;
void dfs2(int v,int p, bool keep){// keep
for(auto [e, w] : adj[v]){
if(p != e && bigc[v] != e)dfs2(e, v, false);
}
if(bigc[v] != -1)dfs2(bigc[v], v, true);
mnedge[d[v]] = ed[v];
if(mnedge.find(d[v] + k) != mnedge.end())
ans = min(ans, mnedge[d[v] + k] - ed[v]);
for(auto [e, w] : adj[v]){
if(e == p || e == bigc[v])continue;
for(int st = tin[e];st<=tout[e];st++){
int node = rev[st];
int c = k + 2*d[v] - d[node];
if(mnedge.find(c) != mnedge.end())ans = min(ans, mnedge[c] + ed[node] - 2*ed[v]);
}
for(int st = tin[e];st<=tout[e];st++){
if(mnedge[d[rev[st]]] == 0)
mnedge[d[rev[st]]] = ed[rev[st]];
else
mnedge[d[rev[st]]] = min(mnedge[d[rev[st]]], ed[rev[st]]);
}
}
if(!keep){
mnedge.clear();
}
}
void dfs1(int v,int p){
int mx = -1;
rev[++timer] = v;
tin[v] = timer;
bigc[v] = -1;
siz[v] = 1;
for(auto [e, w] : adj[v]){
if(e == p)continue;
d[e] = d[v] + w;
ed[e] = ed[v] + 1;
dfs1(e, v);
if(siz[e] > mx){
bigc[v] = e;
mx = siz[e];
}
siz[v] += siz[e];
}
tout[v] = timer;
}
#undef int
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
for(int i = 0;i + 1<n;i++){
int a = H[i][0], b = H[i][1], c = L[i];
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
dfs1(0, -1);
dfs2(0, -1, true);
if(ans > 1e8)
return -1;
return ans;
}
// signed main() {
// iostream::sync_with_stdio(0);cin.tie(0);
// }
Compilation message
race.cpp: In function 'void dfs2(long long int, long long int, bool)':
race.cpp:10:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
10 | for(auto [e, w] : adj[v]){
| ^
race.cpp:17:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
17 | for(auto [e, w] : adj[v]){
| ^
race.cpp: In function 'void dfs1(long long int, long long int)':
race.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for(auto [e, w] : adj[v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5096 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5096 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5204 KB |
Output is correct |
24 |
Correct |
3 ms |
5204 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
4 ms |
5204 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5188 KB |
Output is correct |
31 |
Correct |
3 ms |
5204 KB |
Output is correct |
32 |
Correct |
3 ms |
5204 KB |
Output is correct |
33 |
Correct |
4 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
5208 KB |
Output is correct |
37 |
Correct |
3 ms |
5208 KB |
Output is correct |
38 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5096 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
130 ms |
16804 KB |
Output is correct |
20 |
Correct |
135 ms |
16800 KB |
Output is correct |
21 |
Correct |
129 ms |
16812 KB |
Output is correct |
22 |
Correct |
127 ms |
16852 KB |
Output is correct |
23 |
Correct |
166 ms |
17224 KB |
Output is correct |
24 |
Correct |
122 ms |
17840 KB |
Output is correct |
25 |
Correct |
80 ms |
27140 KB |
Output is correct |
26 |
Correct |
50 ms |
34840 KB |
Output is correct |
27 |
Correct |
180 ms |
32352 KB |
Output is correct |
28 |
Correct |
220 ms |
77464 KB |
Output is correct |
29 |
Correct |
220 ms |
75084 KB |
Output is correct |
30 |
Correct |
171 ms |
32168 KB |
Output is correct |
31 |
Correct |
168 ms |
32096 KB |
Output is correct |
32 |
Correct |
222 ms |
32348 KB |
Output is correct |
33 |
Correct |
223 ms |
31148 KB |
Output is correct |
34 |
Correct |
352 ms |
44236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5096 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5204 KB |
Output is correct |
24 |
Correct |
3 ms |
5204 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
4 ms |
5204 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5188 KB |
Output is correct |
31 |
Correct |
3 ms |
5204 KB |
Output is correct |
32 |
Correct |
3 ms |
5204 KB |
Output is correct |
33 |
Correct |
4 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
5208 KB |
Output is correct |
37 |
Correct |
3 ms |
5208 KB |
Output is correct |
38 |
Correct |
3 ms |
5204 KB |
Output is correct |
39 |
Correct |
130 ms |
16804 KB |
Output is correct |
40 |
Correct |
135 ms |
16800 KB |
Output is correct |
41 |
Correct |
129 ms |
16812 KB |
Output is correct |
42 |
Correct |
127 ms |
16852 KB |
Output is correct |
43 |
Correct |
166 ms |
17224 KB |
Output is correct |
44 |
Correct |
122 ms |
17840 KB |
Output is correct |
45 |
Correct |
80 ms |
27140 KB |
Output is correct |
46 |
Correct |
50 ms |
34840 KB |
Output is correct |
47 |
Correct |
180 ms |
32352 KB |
Output is correct |
48 |
Correct |
220 ms |
77464 KB |
Output is correct |
49 |
Correct |
220 ms |
75084 KB |
Output is correct |
50 |
Correct |
171 ms |
32168 KB |
Output is correct |
51 |
Correct |
168 ms |
32096 KB |
Output is correct |
52 |
Correct |
222 ms |
32348 KB |
Output is correct |
53 |
Correct |
223 ms |
31148 KB |
Output is correct |
54 |
Correct |
352 ms |
44236 KB |
Output is correct |
55 |
Correct |
13 ms |
6732 KB |
Output is correct |
56 |
Correct |
10 ms |
6356 KB |
Output is correct |
57 |
Correct |
77 ms |
18504 KB |
Output is correct |
58 |
Correct |
47 ms |
17464 KB |
Output is correct |
59 |
Correct |
70 ms |
41168 KB |
Output is correct |
60 |
Correct |
218 ms |
75724 KB |
Output is correct |
61 |
Correct |
202 ms |
34004 KB |
Output is correct |
62 |
Correct |
172 ms |
32220 KB |
Output is correct |
63 |
Correct |
222 ms |
32332 KB |
Output is correct |
64 |
Correct |
436 ms |
38220 KB |
Output is correct |
65 |
Correct |
431 ms |
44876 KB |
Output is correct |
66 |
Correct |
239 ms |
69448 KB |
Output is correct |
67 |
Correct |
163 ms |
31320 KB |
Output is correct |
68 |
Correct |
310 ms |
42572 KB |
Output is correct |
69 |
Correct |
325 ms |
42736 KB |
Output is correct |
70 |
Correct |
291 ms |
40908 KB |
Output is correct |