#include <bits/stdc++.h>
//#include "factories.h"
//#include "wall.h"
#include "race.h"
#define f first
#define s second
using namespace std;
const int maxn = 2e5 + 69;
vector<pair<int, int>> g[maxn];
map<int, int> m[maxn];
vector<int> dep(maxn), sum(maxn);
int ans = 1e9;
void dfs(int v,int p) {
for(auto u : g[v]) {
if(u.f != p) {
dep[u.f] = dep[v] + 1;
sum[u.f] = sum[v] + u.s;
dfs(u.f, v);
}
}
}
void dfs2(int v, int p, int t) {
m[v][sum[v]] = dep[v];
for(auto u : g[v]) {
if(u.f != p) {
dfs2(u.f, v, t);
if(m[u.f].size() > m[v].size())
swap(m[u.f], m[v]);
for(auto it : m[u.f]) {
int trazim = t + 2 * sum[v] - it.f;
if(m[v].count(trazim))
ans = min(ans, it.s + m[v][trazim] - 2 * dep[v]);
}
for(auto it : m[u.f]) {
if(m[v].count(it.f))
m[v][it.f] = min(m[v][it.f], it.s);
else
m[v][it.f] = it.s;
}
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
for(int i = 0;i < N - 1;i++) {
g[H[i][1]].push_back({H[i][0], L[i]});
g[H[i][0]].push_back({H[i][1], L[i]});
}
dfs(0, -1);
dfs2(0, -1, K);
if(ans == 1e9)
ans = -1;
return ans;
}
// signed main()
// {
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
// int tt = 1;
// //cin >> tt;
// while(tt--) {
// 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];
// cout << best_path(n, k, h, l);
// }
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15972 KB |
Output is correct |
3 |
Correct |
7 ms |
15956 KB |
Output is correct |
4 |
Correct |
8 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
15988 KB |
Output is correct |
6 |
Correct |
8 ms |
15952 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
10 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15956 KB |
Output is correct |
10 |
Correct |
8 ms |
15956 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
8 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15928 KB |
Output is correct |
17 |
Correct |
10 ms |
15996 KB |
Output is correct |
18 |
Correct |
8 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15972 KB |
Output is correct |
3 |
Correct |
7 ms |
15956 KB |
Output is correct |
4 |
Correct |
8 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
15988 KB |
Output is correct |
6 |
Correct |
8 ms |
15952 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
10 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15956 KB |
Output is correct |
10 |
Correct |
8 ms |
15956 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
8 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15928 KB |
Output is correct |
17 |
Correct |
10 ms |
15996 KB |
Output is correct |
18 |
Correct |
8 ms |
15956 KB |
Output is correct |
19 |
Correct |
9 ms |
15996 KB |
Output is correct |
20 |
Correct |
9 ms |
15956 KB |
Output is correct |
21 |
Correct |
9 ms |
16108 KB |
Output is correct |
22 |
Correct |
9 ms |
16212 KB |
Output is correct |
23 |
Correct |
10 ms |
16212 KB |
Output is correct |
24 |
Correct |
10 ms |
16128 KB |
Output is correct |
25 |
Correct |
9 ms |
16128 KB |
Output is correct |
26 |
Correct |
9 ms |
16168 KB |
Output is correct |
27 |
Correct |
8 ms |
16084 KB |
Output is correct |
28 |
Correct |
9 ms |
16212 KB |
Output is correct |
29 |
Correct |
9 ms |
16128 KB |
Output is correct |
30 |
Correct |
9 ms |
16124 KB |
Output is correct |
31 |
Correct |
8 ms |
16128 KB |
Output is correct |
32 |
Correct |
8 ms |
16124 KB |
Output is correct |
33 |
Correct |
9 ms |
16212 KB |
Output is correct |
34 |
Correct |
8 ms |
16084 KB |
Output is correct |
35 |
Correct |
9 ms |
16084 KB |
Output is correct |
36 |
Correct |
10 ms |
16084 KB |
Output is correct |
37 |
Correct |
8 ms |
16096 KB |
Output is correct |
38 |
Correct |
8 ms |
16084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15972 KB |
Output is correct |
3 |
Correct |
7 ms |
15956 KB |
Output is correct |
4 |
Correct |
8 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
15988 KB |
Output is correct |
6 |
Correct |
8 ms |
15952 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
10 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15956 KB |
Output is correct |
10 |
Correct |
8 ms |
15956 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
8 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15928 KB |
Output is correct |
17 |
Correct |
10 ms |
15996 KB |
Output is correct |
18 |
Correct |
8 ms |
15956 KB |
Output is correct |
19 |
Correct |
104 ms |
33888 KB |
Output is correct |
20 |
Correct |
110 ms |
33808 KB |
Output is correct |
21 |
Correct |
99 ms |
33652 KB |
Output is correct |
22 |
Correct |
108 ms |
33392 KB |
Output is correct |
23 |
Correct |
141 ms |
42968 KB |
Output is correct |
24 |
Correct |
120 ms |
35944 KB |
Output is correct |
25 |
Correct |
81 ms |
35020 KB |
Output is correct |
26 |
Correct |
63 ms |
41932 KB |
Output is correct |
27 |
Correct |
161 ms |
43112 KB |
Output is correct |
28 |
Correct |
232 ms |
77636 KB |
Output is correct |
29 |
Correct |
247 ms |
76060 KB |
Output is correct |
30 |
Correct |
167 ms |
43096 KB |
Output is correct |
31 |
Correct |
174 ms |
43100 KB |
Output is correct |
32 |
Correct |
251 ms |
43264 KB |
Output is correct |
33 |
Correct |
189 ms |
46460 KB |
Output is correct |
34 |
Correct |
285 ms |
71504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15972 KB |
Output is correct |
3 |
Correct |
7 ms |
15956 KB |
Output is correct |
4 |
Correct |
8 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
15988 KB |
Output is correct |
6 |
Correct |
8 ms |
15952 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
10 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15956 KB |
Output is correct |
10 |
Correct |
8 ms |
15956 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
8 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15928 KB |
Output is correct |
17 |
Correct |
10 ms |
15996 KB |
Output is correct |
18 |
Correct |
8 ms |
15956 KB |
Output is correct |
19 |
Correct |
9 ms |
15996 KB |
Output is correct |
20 |
Correct |
9 ms |
15956 KB |
Output is correct |
21 |
Correct |
9 ms |
16108 KB |
Output is correct |
22 |
Correct |
9 ms |
16212 KB |
Output is correct |
23 |
Correct |
10 ms |
16212 KB |
Output is correct |
24 |
Correct |
10 ms |
16128 KB |
Output is correct |
25 |
Correct |
9 ms |
16128 KB |
Output is correct |
26 |
Correct |
9 ms |
16168 KB |
Output is correct |
27 |
Correct |
8 ms |
16084 KB |
Output is correct |
28 |
Correct |
9 ms |
16212 KB |
Output is correct |
29 |
Correct |
9 ms |
16128 KB |
Output is correct |
30 |
Correct |
9 ms |
16124 KB |
Output is correct |
31 |
Correct |
8 ms |
16128 KB |
Output is correct |
32 |
Correct |
8 ms |
16124 KB |
Output is correct |
33 |
Correct |
9 ms |
16212 KB |
Output is correct |
34 |
Correct |
8 ms |
16084 KB |
Output is correct |
35 |
Correct |
9 ms |
16084 KB |
Output is correct |
36 |
Correct |
10 ms |
16084 KB |
Output is correct |
37 |
Correct |
8 ms |
16096 KB |
Output is correct |
38 |
Correct |
8 ms |
16084 KB |
Output is correct |
39 |
Correct |
104 ms |
33888 KB |
Output is correct |
40 |
Correct |
110 ms |
33808 KB |
Output is correct |
41 |
Correct |
99 ms |
33652 KB |
Output is correct |
42 |
Correct |
108 ms |
33392 KB |
Output is correct |
43 |
Correct |
141 ms |
42968 KB |
Output is correct |
44 |
Correct |
120 ms |
35944 KB |
Output is correct |
45 |
Correct |
81 ms |
35020 KB |
Output is correct |
46 |
Correct |
63 ms |
41932 KB |
Output is correct |
47 |
Correct |
161 ms |
43112 KB |
Output is correct |
48 |
Correct |
232 ms |
77636 KB |
Output is correct |
49 |
Correct |
247 ms |
76060 KB |
Output is correct |
50 |
Correct |
167 ms |
43096 KB |
Output is correct |
51 |
Correct |
174 ms |
43100 KB |
Output is correct |
52 |
Correct |
251 ms |
43264 KB |
Output is correct |
53 |
Correct |
189 ms |
46460 KB |
Output is correct |
54 |
Correct |
285 ms |
71504 KB |
Output is correct |
55 |
Correct |
19 ms |
18516 KB |
Output is correct |
56 |
Correct |
17 ms |
17388 KB |
Output is correct |
57 |
Correct |
57 ms |
31864 KB |
Output is correct |
58 |
Correct |
45 ms |
26832 KB |
Output is correct |
59 |
Correct |
72 ms |
46652 KB |
Output is correct |
60 |
Correct |
235 ms |
76368 KB |
Output is correct |
61 |
Correct |
192 ms |
45788 KB |
Output is correct |
62 |
Correct |
150 ms |
43148 KB |
Output is correct |
63 |
Correct |
219 ms |
43228 KB |
Output is correct |
64 |
Correct |
424 ms |
84560 KB |
Output is correct |
65 |
Correct |
397 ms |
83344 KB |
Output is correct |
66 |
Correct |
360 ms |
72564 KB |
Output is correct |
67 |
Correct |
138 ms |
39068 KB |
Output is correct |
68 |
Correct |
321 ms |
56104 KB |
Output is correct |
69 |
Correct |
341 ms |
59928 KB |
Output is correct |
70 |
Correct |
293 ms |
54432 KB |
Output is correct |