#include <bits/stdc++.h>
#define ll long long
#define pii pair<long long,long long>
using namespace std;
const ll maxn = 5e5 + 2;
ll sz[maxn],h[maxn],k;
vector<pii> a[maxn];
ll ans = LLONG_MAX;
map<ll,ll> mp[maxn];
void dfs(ll u, ll w, ll pa) {
assert(h[u]!=0);
for (pii tmp : a[u]) {
ll wv,v; tie(v,wv) = tmp;
if (v==pa) continue;
h[v] = h[u] + 1;
dfs(v,w+wv,u);
mp[v][w+wv] = h[v];
if (mp[u].size() < mp[v].size()) swap(mp[u],mp[v]);
for (pii val : mp[v]) {
if (val.second==0) continue;
if (val.first - w < k && mp[u][k-val.first+2*w] != 0) {
ans = min(ans, mp[u][k-val.first+2*w] - h[u] + val.second - h[u]);
}
}
for (pii val : mp[v]) {
if (val.second==0) continue;
mp[u][val.first] = mp[u][val.first] == 0 ? val.second : min(val.second,mp[u][val.first]);
}
if (mp[u][w + k] != 0) {
ans = min(ans, mp[u][w+k]-h[u]);
}
}
}
int best_path(int _n, int _k, int _h[][2], int _L[])
{
k = _k;
for (ll i = 0; i < _n - 1; i++){
ll x = _h[i][0], y = _h[i][1];
ll w = _L[i];
a[x].push_back({y,w});
a[y].push_back({x,w});
}
h[0] = 1;
dfs(0,0,-1);
assert(ans > 0);
return ans == LLONG_MAX ? -1 : ans;
}
//void file(string name) {
// if (fopen((name + ".inp").c_str(), "r")) {
// freopen((name + ".inp").c_str(), "r", stdin);
// freopen((name + ".out").c_str(), "w", stdout);
// }
//}
//signed main() {
// file("race");
// int N,K; cin >> N >> K;
// int h[N][2],l[N];
// for (int i = 0; i < N-1; i++) {
// int x,y,w; cin >> x >> y >> w;
// h[i][1] = x;
// h[i][0] = y;
// l[i] = w;
// }
// cout << best_path(N,K,h,l);
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
39248 KB |
Output is correct |
2 |
Correct |
7 ms |
39248 KB |
Output is correct |
3 |
Correct |
7 ms |
39248 KB |
Output is correct |
4 |
Correct |
7 ms |
39248 KB |
Output is correct |
5 |
Correct |
7 ms |
39248 KB |
Output is correct |
6 |
Correct |
7 ms |
39248 KB |
Output is correct |
7 |
Correct |
6 ms |
39248 KB |
Output is correct |
8 |
Correct |
7 ms |
39248 KB |
Output is correct |
9 |
Correct |
7 ms |
39364 KB |
Output is correct |
10 |
Correct |
7 ms |
39420 KB |
Output is correct |
11 |
Correct |
7 ms |
39248 KB |
Output is correct |
12 |
Correct |
7 ms |
39248 KB |
Output is correct |
13 |
Correct |
6 ms |
39248 KB |
Output is correct |
14 |
Correct |
6 ms |
39248 KB |
Output is correct |
15 |
Correct |
6 ms |
39248 KB |
Output is correct |
16 |
Correct |
8 ms |
39248 KB |
Output is correct |
17 |
Correct |
7 ms |
39248 KB |
Output is correct |
18 |
Correct |
7 ms |
39248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
39248 KB |
Output is correct |
2 |
Correct |
7 ms |
39248 KB |
Output is correct |
3 |
Correct |
7 ms |
39248 KB |
Output is correct |
4 |
Correct |
7 ms |
39248 KB |
Output is correct |
5 |
Correct |
7 ms |
39248 KB |
Output is correct |
6 |
Correct |
7 ms |
39248 KB |
Output is correct |
7 |
Correct |
6 ms |
39248 KB |
Output is correct |
8 |
Correct |
7 ms |
39248 KB |
Output is correct |
9 |
Correct |
7 ms |
39364 KB |
Output is correct |
10 |
Correct |
7 ms |
39420 KB |
Output is correct |
11 |
Correct |
7 ms |
39248 KB |
Output is correct |
12 |
Correct |
7 ms |
39248 KB |
Output is correct |
13 |
Correct |
6 ms |
39248 KB |
Output is correct |
14 |
Correct |
6 ms |
39248 KB |
Output is correct |
15 |
Correct |
6 ms |
39248 KB |
Output is correct |
16 |
Correct |
8 ms |
39248 KB |
Output is correct |
17 |
Correct |
7 ms |
39248 KB |
Output is correct |
18 |
Correct |
7 ms |
39248 KB |
Output is correct |
19 |
Correct |
7 ms |
39248 KB |
Output is correct |
20 |
Correct |
7 ms |
39248 KB |
Output is correct |
21 |
Correct |
10 ms |
39772 KB |
Output is correct |
22 |
Correct |
9 ms |
39776 KB |
Output is correct |
23 |
Correct |
9 ms |
39760 KB |
Output is correct |
24 |
Correct |
7 ms |
39760 KB |
Output is correct |
25 |
Correct |
7 ms |
39760 KB |
Output is correct |
26 |
Correct |
7 ms |
39760 KB |
Output is correct |
27 |
Correct |
7 ms |
39404 KB |
Output is correct |
28 |
Correct |
7 ms |
39760 KB |
Output is correct |
29 |
Correct |
7 ms |
39760 KB |
Output is correct |
30 |
Correct |
8 ms |
39760 KB |
Output is correct |
31 |
Correct |
8 ms |
39680 KB |
Output is correct |
32 |
Correct |
7 ms |
39760 KB |
Output is correct |
33 |
Correct |
8 ms |
39760 KB |
Output is correct |
34 |
Correct |
7 ms |
39592 KB |
Output is correct |
35 |
Correct |
8 ms |
39504 KB |
Output is correct |
36 |
Correct |
7 ms |
39608 KB |
Output is correct |
37 |
Correct |
7 ms |
39668 KB |
Output is correct |
38 |
Correct |
7 ms |
39760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
39248 KB |
Output is correct |
2 |
Correct |
7 ms |
39248 KB |
Output is correct |
3 |
Correct |
7 ms |
39248 KB |
Output is correct |
4 |
Correct |
7 ms |
39248 KB |
Output is correct |
5 |
Correct |
7 ms |
39248 KB |
Output is correct |
6 |
Correct |
7 ms |
39248 KB |
Output is correct |
7 |
Correct |
6 ms |
39248 KB |
Output is correct |
8 |
Correct |
7 ms |
39248 KB |
Output is correct |
9 |
Correct |
7 ms |
39364 KB |
Output is correct |
10 |
Correct |
7 ms |
39420 KB |
Output is correct |
11 |
Correct |
7 ms |
39248 KB |
Output is correct |
12 |
Correct |
7 ms |
39248 KB |
Output is correct |
13 |
Correct |
6 ms |
39248 KB |
Output is correct |
14 |
Correct |
6 ms |
39248 KB |
Output is correct |
15 |
Correct |
6 ms |
39248 KB |
Output is correct |
16 |
Correct |
8 ms |
39248 KB |
Output is correct |
17 |
Correct |
7 ms |
39248 KB |
Output is correct |
18 |
Correct |
7 ms |
39248 KB |
Output is correct |
19 |
Correct |
113 ms |
69448 KB |
Output is correct |
20 |
Correct |
121 ms |
70768 KB |
Output is correct |
21 |
Correct |
116 ms |
70728 KB |
Output is correct |
22 |
Correct |
126 ms |
69960 KB |
Output is correct |
23 |
Correct |
168 ms |
81992 KB |
Output is correct |
24 |
Correct |
131 ms |
74056 KB |
Output is correct |
25 |
Correct |
71 ms |
61000 KB |
Output is correct |
26 |
Correct |
57 ms |
69960 KB |
Output is correct |
27 |
Correct |
183 ms |
75336 KB |
Output is correct |
28 |
Correct |
186 ms |
121672 KB |
Output is correct |
29 |
Correct |
194 ms |
119556 KB |
Output is correct |
30 |
Correct |
170 ms |
75420 KB |
Output is correct |
31 |
Correct |
162 ms |
75336 KB |
Output is correct |
32 |
Correct |
199 ms |
76844 KB |
Output is correct |
33 |
Correct |
164 ms |
72736 KB |
Output is correct |
34 |
Correct |
298 ms |
114248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
39248 KB |
Output is correct |
2 |
Correct |
7 ms |
39248 KB |
Output is correct |
3 |
Correct |
7 ms |
39248 KB |
Output is correct |
4 |
Correct |
7 ms |
39248 KB |
Output is correct |
5 |
Correct |
7 ms |
39248 KB |
Output is correct |
6 |
Correct |
7 ms |
39248 KB |
Output is correct |
7 |
Correct |
6 ms |
39248 KB |
Output is correct |
8 |
Correct |
7 ms |
39248 KB |
Output is correct |
9 |
Correct |
7 ms |
39364 KB |
Output is correct |
10 |
Correct |
7 ms |
39420 KB |
Output is correct |
11 |
Correct |
7 ms |
39248 KB |
Output is correct |
12 |
Correct |
7 ms |
39248 KB |
Output is correct |
13 |
Correct |
6 ms |
39248 KB |
Output is correct |
14 |
Correct |
6 ms |
39248 KB |
Output is correct |
15 |
Correct |
6 ms |
39248 KB |
Output is correct |
16 |
Correct |
8 ms |
39248 KB |
Output is correct |
17 |
Correct |
7 ms |
39248 KB |
Output is correct |
18 |
Correct |
7 ms |
39248 KB |
Output is correct |
19 |
Correct |
7 ms |
39248 KB |
Output is correct |
20 |
Correct |
7 ms |
39248 KB |
Output is correct |
21 |
Correct |
10 ms |
39772 KB |
Output is correct |
22 |
Correct |
9 ms |
39776 KB |
Output is correct |
23 |
Correct |
9 ms |
39760 KB |
Output is correct |
24 |
Correct |
7 ms |
39760 KB |
Output is correct |
25 |
Correct |
7 ms |
39760 KB |
Output is correct |
26 |
Correct |
7 ms |
39760 KB |
Output is correct |
27 |
Correct |
7 ms |
39404 KB |
Output is correct |
28 |
Correct |
7 ms |
39760 KB |
Output is correct |
29 |
Correct |
7 ms |
39760 KB |
Output is correct |
30 |
Correct |
8 ms |
39760 KB |
Output is correct |
31 |
Correct |
8 ms |
39680 KB |
Output is correct |
32 |
Correct |
7 ms |
39760 KB |
Output is correct |
33 |
Correct |
8 ms |
39760 KB |
Output is correct |
34 |
Correct |
7 ms |
39592 KB |
Output is correct |
35 |
Correct |
8 ms |
39504 KB |
Output is correct |
36 |
Correct |
7 ms |
39608 KB |
Output is correct |
37 |
Correct |
7 ms |
39668 KB |
Output is correct |
38 |
Correct |
7 ms |
39760 KB |
Output is correct |
39 |
Correct |
113 ms |
69448 KB |
Output is correct |
40 |
Correct |
121 ms |
70768 KB |
Output is correct |
41 |
Correct |
116 ms |
70728 KB |
Output is correct |
42 |
Correct |
126 ms |
69960 KB |
Output is correct |
43 |
Correct |
168 ms |
81992 KB |
Output is correct |
44 |
Correct |
131 ms |
74056 KB |
Output is correct |
45 |
Correct |
71 ms |
61000 KB |
Output is correct |
46 |
Correct |
57 ms |
69960 KB |
Output is correct |
47 |
Correct |
183 ms |
75336 KB |
Output is correct |
48 |
Correct |
186 ms |
121672 KB |
Output is correct |
49 |
Correct |
194 ms |
119556 KB |
Output is correct |
50 |
Correct |
170 ms |
75420 KB |
Output is correct |
51 |
Correct |
162 ms |
75336 KB |
Output is correct |
52 |
Correct |
199 ms |
76844 KB |
Output is correct |
53 |
Correct |
164 ms |
72736 KB |
Output is correct |
54 |
Correct |
298 ms |
114248 KB |
Output is correct |
55 |
Correct |
25 ms |
43856 KB |
Output is correct |
56 |
Correct |
16 ms |
41552 KB |
Output is correct |
57 |
Correct |
84 ms |
68944 KB |
Output is correct |
58 |
Correct |
45 ms |
55992 KB |
Output is correct |
59 |
Correct |
97 ms |
82284 KB |
Output is correct |
60 |
Correct |
259 ms |
120420 KB |
Output is correct |
61 |
Correct |
246 ms |
81480 KB |
Output is correct |
62 |
Correct |
191 ms |
76912 KB |
Output is correct |
63 |
Correct |
213 ms |
76872 KB |
Output is correct |
64 |
Correct |
585 ms |
186036 KB |
Output is correct |
65 |
Correct |
382 ms |
129224 KB |
Output is correct |
66 |
Correct |
258 ms |
115224 KB |
Output is correct |
67 |
Correct |
135 ms |
70280 KB |
Output is correct |
68 |
Correct |
360 ms |
109896 KB |
Output is correct |
69 |
Correct |
427 ms |
118600 KB |
Output is correct |
70 |
Correct |
358 ms |
107336 KB |
Output is correct |