#include "race.h"
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int INF = 1e9, MAXN = 2e5+10;
int dep[MAXN], ans = INF;
long long qs[MAXN];
vector<pair<int,int>> G[MAXN];
map<long long, int> mp[MAXN];
void dfs1(int u, int p){
for(auto [v, w] : G[u]){
if(v == p) continue;
dep[v] = dep[u]+1;
qs[v] = qs[u]+w;
dfs1(v, u);
}
}
void dfs2(int u, int p, long long K){
for(auto [v, w] : G[u]){
if(v == p) continue;
dfs2(v, u, K);
if(mp[v].size() > mp[u].size()){
swap(mp[v], mp[u]);
}
}
for(auto [v, w] : G[u]){
if(v == p) continue;
for(auto &[key, val] : mp[v]){
long long tmp = K-key+2ll*qs[u];
if(mp[u].find(tmp) != mp[u].end()){
ans = min(ans, val+mp[u][tmp]-2*dep[u]);
}
}
for(auto &[key, val] : mp[v]){
if(mp[u].find(key) != mp[u].end()){
mp[u][key] = min(mp[u][key], val);
} else {
mp[u][key] = val;
}
}
}
if(mp[u].find(K+qs[u]) != mp[u].end()){
ans = min(ans, mp[u][K+qs[u]]-dep[u]);
}
mp[u][qs[u]] = dep[u];
}
int best_path(int N, int K, int H[][2], int L[])
{
for(int i = 0; i < N-1; i++){
int u = H[i][0], v = H[i][1];
G[u].emplace_back(v, L[i]);
G[v].emplace_back(u, L[i]);
}
dep[0] = 1;
dfs1(0, -1);
// for(int i = 0; i < N; i++) cout << dep[i] << " " << qs[i] << "\n";
dfs2(0, -1, K);
if(ans >= INF) ans = -1;
return ans;
}
/*
int main(void){
int N, K; cin >> N >> K;
int H[N-1][2], L[N-1];
for(int i = 0; i < N-1; i++) cin >> H[i][0] >> H[i][1] >> L[i];
int res; cin >> res;
int cal = best_path(N, K, H, L);
cout << cal << " " << res << "\n";
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14320 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14440 KB |
Output is correct |
6 |
Correct |
8 ms |
14436 KB |
Output is correct |
7 |
Correct |
8 ms |
14396 KB |
Output is correct |
8 |
Correct |
10 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14376 KB |
Output is correct |
10 |
Correct |
8 ms |
14364 KB |
Output is correct |
11 |
Correct |
7 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14432 KB |
Output is correct |
15 |
Correct |
7 ms |
14328 KB |
Output is correct |
16 |
Correct |
8 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14320 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14440 KB |
Output is correct |
6 |
Correct |
8 ms |
14436 KB |
Output is correct |
7 |
Correct |
8 ms |
14396 KB |
Output is correct |
8 |
Correct |
10 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14376 KB |
Output is correct |
10 |
Correct |
8 ms |
14364 KB |
Output is correct |
11 |
Correct |
7 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14432 KB |
Output is correct |
15 |
Correct |
7 ms |
14328 KB |
Output is correct |
16 |
Correct |
8 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
7 ms |
14412 KB |
Output is correct |
21 |
Correct |
8 ms |
14580 KB |
Output is correct |
22 |
Correct |
8 ms |
14652 KB |
Output is correct |
23 |
Correct |
9 ms |
14672 KB |
Output is correct |
24 |
Correct |
10 ms |
14600 KB |
Output is correct |
25 |
Correct |
8 ms |
14624 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 KB |
Output is correct |
27 |
Correct |
8 ms |
14412 KB |
Output is correct |
28 |
Correct |
8 ms |
14668 KB |
Output is correct |
29 |
Correct |
10 ms |
14600 KB |
Output is correct |
30 |
Correct |
9 ms |
14668 KB |
Output is correct |
31 |
Correct |
8 ms |
14652 KB |
Output is correct |
32 |
Correct |
11 ms |
14668 KB |
Output is correct |
33 |
Correct |
8 ms |
14696 KB |
Output is correct |
34 |
Correct |
8 ms |
14596 KB |
Output is correct |
35 |
Correct |
10 ms |
14540 KB |
Output is correct |
36 |
Correct |
8 ms |
14524 KB |
Output is correct |
37 |
Correct |
7 ms |
14540 KB |
Output is correct |
38 |
Correct |
8 ms |
14608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14320 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14440 KB |
Output is correct |
6 |
Correct |
8 ms |
14436 KB |
Output is correct |
7 |
Correct |
8 ms |
14396 KB |
Output is correct |
8 |
Correct |
10 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14376 KB |
Output is correct |
10 |
Correct |
8 ms |
14364 KB |
Output is correct |
11 |
Correct |
7 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14432 KB |
Output is correct |
15 |
Correct |
7 ms |
14328 KB |
Output is correct |
16 |
Correct |
8 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14412 KB |
Output is correct |
19 |
Correct |
118 ms |
31360 KB |
Output is correct |
20 |
Correct |
119 ms |
31328 KB |
Output is correct |
21 |
Correct |
108 ms |
31400 KB |
Output is correct |
22 |
Correct |
112 ms |
31584 KB |
Output is correct |
23 |
Correct |
147 ms |
44748 KB |
Output is correct |
24 |
Correct |
119 ms |
37756 KB |
Output is correct |
25 |
Correct |
84 ms |
30364 KB |
Output is correct |
26 |
Correct |
53 ms |
38672 KB |
Output is correct |
27 |
Correct |
193 ms |
39376 KB |
Output is correct |
28 |
Correct |
226 ms |
75212 KB |
Output is correct |
29 |
Correct |
276 ms |
73584 KB |
Output is correct |
30 |
Correct |
193 ms |
39388 KB |
Output is correct |
31 |
Correct |
188 ms |
39500 KB |
Output is correct |
32 |
Correct |
264 ms |
39548 KB |
Output is correct |
33 |
Correct |
229 ms |
38288 KB |
Output is correct |
34 |
Correct |
305 ms |
70212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14320 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14440 KB |
Output is correct |
6 |
Correct |
8 ms |
14436 KB |
Output is correct |
7 |
Correct |
8 ms |
14396 KB |
Output is correct |
8 |
Correct |
10 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14376 KB |
Output is correct |
10 |
Correct |
8 ms |
14364 KB |
Output is correct |
11 |
Correct |
7 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
9 ms |
14432 KB |
Output is correct |
15 |
Correct |
7 ms |
14328 KB |
Output is correct |
16 |
Correct |
8 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
7 ms |
14412 KB |
Output is correct |
21 |
Correct |
8 ms |
14580 KB |
Output is correct |
22 |
Correct |
8 ms |
14652 KB |
Output is correct |
23 |
Correct |
9 ms |
14672 KB |
Output is correct |
24 |
Correct |
10 ms |
14600 KB |
Output is correct |
25 |
Correct |
8 ms |
14624 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 KB |
Output is correct |
27 |
Correct |
8 ms |
14412 KB |
Output is correct |
28 |
Correct |
8 ms |
14668 KB |
Output is correct |
29 |
Correct |
10 ms |
14600 KB |
Output is correct |
30 |
Correct |
9 ms |
14668 KB |
Output is correct |
31 |
Correct |
8 ms |
14652 KB |
Output is correct |
32 |
Correct |
11 ms |
14668 KB |
Output is correct |
33 |
Correct |
8 ms |
14696 KB |
Output is correct |
34 |
Correct |
8 ms |
14596 KB |
Output is correct |
35 |
Correct |
10 ms |
14540 KB |
Output is correct |
36 |
Correct |
8 ms |
14524 KB |
Output is correct |
37 |
Correct |
7 ms |
14540 KB |
Output is correct |
38 |
Correct |
8 ms |
14608 KB |
Output is correct |
39 |
Correct |
118 ms |
31360 KB |
Output is correct |
40 |
Correct |
119 ms |
31328 KB |
Output is correct |
41 |
Correct |
108 ms |
31400 KB |
Output is correct |
42 |
Correct |
112 ms |
31584 KB |
Output is correct |
43 |
Correct |
147 ms |
44748 KB |
Output is correct |
44 |
Correct |
119 ms |
37756 KB |
Output is correct |
45 |
Correct |
84 ms |
30364 KB |
Output is correct |
46 |
Correct |
53 ms |
38672 KB |
Output is correct |
47 |
Correct |
193 ms |
39376 KB |
Output is correct |
48 |
Correct |
226 ms |
75212 KB |
Output is correct |
49 |
Correct |
276 ms |
73584 KB |
Output is correct |
50 |
Correct |
193 ms |
39388 KB |
Output is correct |
51 |
Correct |
188 ms |
39500 KB |
Output is correct |
52 |
Correct |
264 ms |
39548 KB |
Output is correct |
53 |
Correct |
229 ms |
38288 KB |
Output is correct |
54 |
Correct |
305 ms |
70212 KB |
Output is correct |
55 |
Correct |
20 ms |
17120 KB |
Output is correct |
56 |
Correct |
14 ms |
15820 KB |
Output is correct |
57 |
Correct |
72 ms |
29932 KB |
Output is correct |
58 |
Correct |
76 ms |
26908 KB |
Output is correct |
59 |
Correct |
87 ms |
44824 KB |
Output is correct |
60 |
Correct |
219 ms |
74052 KB |
Output is correct |
61 |
Correct |
220 ms |
42764 KB |
Output is correct |
62 |
Correct |
203 ms |
39456 KB |
Output is correct |
63 |
Correct |
233 ms |
39532 KB |
Output is correct |
64 |
Correct |
422 ms |
89468 KB |
Output is correct |
65 |
Correct |
414 ms |
89796 KB |
Output is correct |
66 |
Correct |
240 ms |
69168 KB |
Output is correct |
67 |
Correct |
185 ms |
39504 KB |
Output is correct |
68 |
Correct |
334 ms |
57632 KB |
Output is correct |
69 |
Correct |
320 ms |
58292 KB |
Output is correct |
70 |
Correct |
312 ms |
55748 KB |
Output is correct |