#include "race.h"
#include<bits/stdc++.h>
using namespace std;
const int N = 200010;
map <int, int> m[N];
int shift[N];
int k;
vector <pair <int, int>> g[N];
int res[N];
int h[N];
void prep(int v, int p){
for(auto [x, _] : g[v]){
if(x == p) continue;
h[x] = h[v]+1;
prep(x, v);
}
return;
}
void dfs(int v, int p){
m[v][0] = h[v];
for(auto [x, w] : g[v]){
if(x == p) continue;
dfs(x, v);
shift[x] += w;
if(m[x].size() > m[v].size()){
swap(m[x], m[v]);
swap(shift[x], shift[v]);
}
for(auto [vvalor, altura] : m[x]){
int valor = vvalor;
valor = valor+shift[x];
valor = k-valor;
valor = valor-shift[v];
if(m[v].find(valor) != m[v].end()){
int altura2 = m[v][valor];
// cout << altura << ' ' << altura2 << ' ' << v << '\n';
res[v] = min(res[v], altura+altura2-2*h[v]);
}
}
for(auto [vvalor, altura] : m[x]){
int valor = vvalor;
valor = valor+shift[x]-shift[v];
if(m[v].find(valor) != m[v].end()) m[v][valor] = min(m[v][valor], altura);
else m[v][valor] = altura;
}
}
return;
}
int best_path(int n, int K, int h[][2], int l[]){
k = K;
for(int i = 0;i < n-1;i++){
int a = h[i][0], b = h[i][1];
g[a].push_back({b, l[i]});
g[b].push_back({a, l[i]});
}
for(int i = 0;i < n;i++) res[i] = 1e8;
prep(0,0);
dfs(0,0);
int resposta = 1e8;
for(int i= 0;i < n;i++) {
//cout << res[i] << ' ';
resposta = min(resposta, res[i]);
}
return (resposta == 1e8 ? -1 : resposta);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20312 KB |
Output is correct |
2 |
Correct |
3 ms |
20316 KB |
Output is correct |
3 |
Correct |
3 ms |
20316 KB |
Output is correct |
4 |
Correct |
3 ms |
20316 KB |
Output is correct |
5 |
Correct |
3 ms |
20312 KB |
Output is correct |
6 |
Correct |
3 ms |
20524 KB |
Output is correct |
7 |
Correct |
3 ms |
20316 KB |
Output is correct |
8 |
Correct |
4 ms |
20316 KB |
Output is correct |
9 |
Correct |
4 ms |
20504 KB |
Output is correct |
10 |
Correct |
3 ms |
20316 KB |
Output is correct |
11 |
Correct |
3 ms |
20520 KB |
Output is correct |
12 |
Correct |
3 ms |
20552 KB |
Output is correct |
13 |
Correct |
3 ms |
20312 KB |
Output is correct |
14 |
Correct |
3 ms |
20316 KB |
Output is correct |
15 |
Correct |
3 ms |
20316 KB |
Output is correct |
16 |
Correct |
4 ms |
20312 KB |
Output is correct |
17 |
Correct |
3 ms |
20316 KB |
Output is correct |
18 |
Correct |
4 ms |
20312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20312 KB |
Output is correct |
2 |
Correct |
3 ms |
20316 KB |
Output is correct |
3 |
Correct |
3 ms |
20316 KB |
Output is correct |
4 |
Correct |
3 ms |
20316 KB |
Output is correct |
5 |
Correct |
3 ms |
20312 KB |
Output is correct |
6 |
Correct |
3 ms |
20524 KB |
Output is correct |
7 |
Correct |
3 ms |
20316 KB |
Output is correct |
8 |
Correct |
4 ms |
20316 KB |
Output is correct |
9 |
Correct |
4 ms |
20504 KB |
Output is correct |
10 |
Correct |
3 ms |
20316 KB |
Output is correct |
11 |
Correct |
3 ms |
20520 KB |
Output is correct |
12 |
Correct |
3 ms |
20552 KB |
Output is correct |
13 |
Correct |
3 ms |
20312 KB |
Output is correct |
14 |
Correct |
3 ms |
20316 KB |
Output is correct |
15 |
Correct |
3 ms |
20316 KB |
Output is correct |
16 |
Correct |
4 ms |
20312 KB |
Output is correct |
17 |
Correct |
3 ms |
20316 KB |
Output is correct |
18 |
Correct |
4 ms |
20312 KB |
Output is correct |
19 |
Correct |
3 ms |
20316 KB |
Output is correct |
20 |
Correct |
4 ms |
20316 KB |
Output is correct |
21 |
Correct |
4 ms |
20572 KB |
Output is correct |
22 |
Correct |
4 ms |
20572 KB |
Output is correct |
23 |
Correct |
4 ms |
20568 KB |
Output is correct |
24 |
Correct |
5 ms |
20572 KB |
Output is correct |
25 |
Correct |
4 ms |
20572 KB |
Output is correct |
26 |
Correct |
5 ms |
20572 KB |
Output is correct |
27 |
Correct |
3 ms |
20568 KB |
Output is correct |
28 |
Correct |
4 ms |
20572 KB |
Output is correct |
29 |
Correct |
4 ms |
20572 KB |
Output is correct |
30 |
Correct |
4 ms |
20764 KB |
Output is correct |
31 |
Correct |
4 ms |
20572 KB |
Output is correct |
32 |
Correct |
4 ms |
20592 KB |
Output is correct |
33 |
Correct |
5 ms |
20572 KB |
Output is correct |
34 |
Correct |
3 ms |
20572 KB |
Output is correct |
35 |
Correct |
4 ms |
20572 KB |
Output is correct |
36 |
Correct |
5 ms |
20572 KB |
Output is correct |
37 |
Correct |
3 ms |
20572 KB |
Output is correct |
38 |
Correct |
4 ms |
20572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20312 KB |
Output is correct |
2 |
Correct |
3 ms |
20316 KB |
Output is correct |
3 |
Correct |
3 ms |
20316 KB |
Output is correct |
4 |
Correct |
3 ms |
20316 KB |
Output is correct |
5 |
Correct |
3 ms |
20312 KB |
Output is correct |
6 |
Correct |
3 ms |
20524 KB |
Output is correct |
7 |
Correct |
3 ms |
20316 KB |
Output is correct |
8 |
Correct |
4 ms |
20316 KB |
Output is correct |
9 |
Correct |
4 ms |
20504 KB |
Output is correct |
10 |
Correct |
3 ms |
20316 KB |
Output is correct |
11 |
Correct |
3 ms |
20520 KB |
Output is correct |
12 |
Correct |
3 ms |
20552 KB |
Output is correct |
13 |
Correct |
3 ms |
20312 KB |
Output is correct |
14 |
Correct |
3 ms |
20316 KB |
Output is correct |
15 |
Correct |
3 ms |
20316 KB |
Output is correct |
16 |
Correct |
4 ms |
20312 KB |
Output is correct |
17 |
Correct |
3 ms |
20316 KB |
Output is correct |
18 |
Correct |
4 ms |
20312 KB |
Output is correct |
19 |
Correct |
73 ms |
39288 KB |
Output is correct |
20 |
Correct |
87 ms |
39360 KB |
Output is correct |
21 |
Correct |
80 ms |
39112 KB |
Output is correct |
22 |
Correct |
84 ms |
38996 KB |
Output is correct |
23 |
Correct |
111 ms |
48468 KB |
Output is correct |
24 |
Correct |
84 ms |
41556 KB |
Output is correct |
25 |
Correct |
60 ms |
41488 KB |
Output is correct |
26 |
Correct |
42 ms |
49084 KB |
Output is correct |
27 |
Correct |
129 ms |
47552 KB |
Output is correct |
28 |
Correct |
194 ms |
85072 KB |
Output is correct |
29 |
Correct |
202 ms |
83348 KB |
Output is correct |
30 |
Correct |
128 ms |
47536 KB |
Output is correct |
31 |
Correct |
126 ms |
47404 KB |
Output is correct |
32 |
Correct |
154 ms |
47536 KB |
Output is correct |
33 |
Correct |
195 ms |
51024 KB |
Output is correct |
34 |
Correct |
247 ms |
76004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20312 KB |
Output is correct |
2 |
Correct |
3 ms |
20316 KB |
Output is correct |
3 |
Correct |
3 ms |
20316 KB |
Output is correct |
4 |
Correct |
3 ms |
20316 KB |
Output is correct |
5 |
Correct |
3 ms |
20312 KB |
Output is correct |
6 |
Correct |
3 ms |
20524 KB |
Output is correct |
7 |
Correct |
3 ms |
20316 KB |
Output is correct |
8 |
Correct |
4 ms |
20316 KB |
Output is correct |
9 |
Correct |
4 ms |
20504 KB |
Output is correct |
10 |
Correct |
3 ms |
20316 KB |
Output is correct |
11 |
Correct |
3 ms |
20520 KB |
Output is correct |
12 |
Correct |
3 ms |
20552 KB |
Output is correct |
13 |
Correct |
3 ms |
20312 KB |
Output is correct |
14 |
Correct |
3 ms |
20316 KB |
Output is correct |
15 |
Correct |
3 ms |
20316 KB |
Output is correct |
16 |
Correct |
4 ms |
20312 KB |
Output is correct |
17 |
Correct |
3 ms |
20316 KB |
Output is correct |
18 |
Correct |
4 ms |
20312 KB |
Output is correct |
19 |
Correct |
3 ms |
20316 KB |
Output is correct |
20 |
Correct |
4 ms |
20316 KB |
Output is correct |
21 |
Correct |
4 ms |
20572 KB |
Output is correct |
22 |
Correct |
4 ms |
20572 KB |
Output is correct |
23 |
Correct |
4 ms |
20568 KB |
Output is correct |
24 |
Correct |
5 ms |
20572 KB |
Output is correct |
25 |
Correct |
4 ms |
20572 KB |
Output is correct |
26 |
Correct |
5 ms |
20572 KB |
Output is correct |
27 |
Correct |
3 ms |
20568 KB |
Output is correct |
28 |
Correct |
4 ms |
20572 KB |
Output is correct |
29 |
Correct |
4 ms |
20572 KB |
Output is correct |
30 |
Correct |
4 ms |
20764 KB |
Output is correct |
31 |
Correct |
4 ms |
20572 KB |
Output is correct |
32 |
Correct |
4 ms |
20592 KB |
Output is correct |
33 |
Correct |
5 ms |
20572 KB |
Output is correct |
34 |
Correct |
3 ms |
20572 KB |
Output is correct |
35 |
Correct |
4 ms |
20572 KB |
Output is correct |
36 |
Correct |
5 ms |
20572 KB |
Output is correct |
37 |
Correct |
3 ms |
20572 KB |
Output is correct |
38 |
Correct |
4 ms |
20572 KB |
Output is correct |
39 |
Correct |
73 ms |
39288 KB |
Output is correct |
40 |
Correct |
87 ms |
39360 KB |
Output is correct |
41 |
Correct |
80 ms |
39112 KB |
Output is correct |
42 |
Correct |
84 ms |
38996 KB |
Output is correct |
43 |
Correct |
111 ms |
48468 KB |
Output is correct |
44 |
Correct |
84 ms |
41556 KB |
Output is correct |
45 |
Correct |
60 ms |
41488 KB |
Output is correct |
46 |
Correct |
42 ms |
49084 KB |
Output is correct |
47 |
Correct |
129 ms |
47552 KB |
Output is correct |
48 |
Correct |
194 ms |
85072 KB |
Output is correct |
49 |
Correct |
202 ms |
83348 KB |
Output is correct |
50 |
Correct |
128 ms |
47536 KB |
Output is correct |
51 |
Correct |
126 ms |
47404 KB |
Output is correct |
52 |
Correct |
154 ms |
47536 KB |
Output is correct |
53 |
Correct |
195 ms |
51024 KB |
Output is correct |
54 |
Correct |
247 ms |
76004 KB |
Output is correct |
55 |
Correct |
14 ms |
22876 KB |
Output is correct |
56 |
Correct |
8 ms |
21852 KB |
Output is correct |
57 |
Correct |
44 ms |
37464 KB |
Output is correct |
58 |
Correct |
38 ms |
32200 KB |
Output is correct |
59 |
Correct |
63 ms |
53588 KB |
Output is correct |
60 |
Correct |
196 ms |
83796 KB |
Output is correct |
61 |
Correct |
154 ms |
50112 KB |
Output is correct |
62 |
Correct |
117 ms |
47420 KB |
Output is correct |
63 |
Correct |
150 ms |
47700 KB |
Output is correct |
64 |
Correct |
317 ms |
89012 KB |
Output is correct |
65 |
Correct |
302 ms |
87600 KB |
Output is correct |
66 |
Correct |
202 ms |
79244 KB |
Output is correct |
67 |
Correct |
97 ms |
43560 KB |
Output is correct |
68 |
Correct |
219 ms |
60556 KB |
Output is correct |
69 |
Correct |
242 ms |
64248 KB |
Output is correct |
70 |
Correct |
212 ms |
58912 KB |
Output is correct |