#ifndef LOCAL
#include "race.h"
#endif // LOCAL
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 2e5 + 5;
map<ll, int> sub[maxn];
vector<ii> adj[maxn];
int N, K;
int res = 1e9;
void dfs(int u, int p = -1, int h = 0, ll w = 0)
{
sub[u][w] = h;
for(auto it : adj[u]) if(it.fi != p){
dfs(it.fi, u, h + 1, w + it.se);
int v = it.fi;
if(sub[u].size() < sub[v].size())
swap(sub[u], sub[v]);
for(auto & all : sub[v]){
if(sub[u].count(K + 2 * w - all.fi)){
res = min(res, sub[u][K + 2 * w - all.fi] + all.se - 2 * h);
}
}
for(auto & all : sub[v]){
if(sub[u].count(all.fi)){
sub[u][all.fi] = min(sub[u][all.fi], all.se);
}
else{
sub[u][all.fi] = all.se;
}
}
sub[v].clear();
}
}
int best_path(int _N, int _K, int H[][2], int L[])
{
N = _N;
K = _K;
for(int i = 0; i < N - 1; ++i){
adj[H[i][0]].eb(H[i][1], L[i]);
adj[H[i][1]].eb(H[i][0], L[i]);
//cerr << H[i][0] << ' ' << H[i][1] << ' ' << L[i] << '\n';
}
dfs(0);
if(res == 1e9) res = -1;
return res;
}
#ifdef LOCAL
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
int _N, _K; cin >> _N >> _K;
int H[_N][2];
int L[_N];
for(int i = 0; i < _N - 1; ++i)
cin >> H[i][0] >> H[i][1] >> L[i];
cout << best_path(_N, _K, H, L);
}
#endif //LOCAL
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
12 ms |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
12 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
12 ms |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
12 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
12 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14464 KB |
Output is correct |
22 |
Correct |
13 ms |
14592 KB |
Output is correct |
23 |
Correct |
14 ms |
14592 KB |
Output is correct |
24 |
Correct |
14 ms |
14592 KB |
Output is correct |
25 |
Correct |
13 ms |
14592 KB |
Output is correct |
26 |
Correct |
14 ms |
14592 KB |
Output is correct |
27 |
Correct |
14 ms |
14464 KB |
Output is correct |
28 |
Correct |
13 ms |
14592 KB |
Output is correct |
29 |
Correct |
14 ms |
14592 KB |
Output is correct |
30 |
Correct |
13 ms |
14592 KB |
Output is correct |
31 |
Correct |
13 ms |
14592 KB |
Output is correct |
32 |
Correct |
13 ms |
14592 KB |
Output is correct |
33 |
Correct |
15 ms |
14592 KB |
Output is correct |
34 |
Correct |
13 ms |
14592 KB |
Output is correct |
35 |
Correct |
14 ms |
14592 KB |
Output is correct |
36 |
Correct |
14 ms |
14592 KB |
Output is correct |
37 |
Correct |
17 ms |
14592 KB |
Output is correct |
38 |
Correct |
13 ms |
14592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
12 ms |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
12 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
125 ms |
20728 KB |
Output is correct |
20 |
Correct |
135 ms |
20728 KB |
Output is correct |
21 |
Correct |
134 ms |
20856 KB |
Output is correct |
22 |
Correct |
138 ms |
20832 KB |
Output is correct |
23 |
Correct |
168 ms |
21216 KB |
Output is correct |
24 |
Correct |
134 ms |
20856 KB |
Output is correct |
25 |
Correct |
112 ms |
31160 KB |
Output is correct |
26 |
Correct |
78 ms |
40444 KB |
Output is correct |
27 |
Correct |
250 ms |
27512 KB |
Output is correct |
28 |
Correct |
330 ms |
66704 KB |
Output is correct |
29 |
Correct |
318 ms |
65528 KB |
Output is correct |
30 |
Correct |
224 ms |
27512 KB |
Output is correct |
31 |
Correct |
230 ms |
27512 KB |
Output is correct |
32 |
Correct |
263 ms |
27768 KB |
Output is correct |
33 |
Correct |
253 ms |
26872 KB |
Output is correct |
34 |
Correct |
392 ms |
40568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
12 ms |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
12 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
12 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14464 KB |
Output is correct |
22 |
Correct |
13 ms |
14592 KB |
Output is correct |
23 |
Correct |
14 ms |
14592 KB |
Output is correct |
24 |
Correct |
14 ms |
14592 KB |
Output is correct |
25 |
Correct |
13 ms |
14592 KB |
Output is correct |
26 |
Correct |
14 ms |
14592 KB |
Output is correct |
27 |
Correct |
14 ms |
14464 KB |
Output is correct |
28 |
Correct |
13 ms |
14592 KB |
Output is correct |
29 |
Correct |
14 ms |
14592 KB |
Output is correct |
30 |
Correct |
13 ms |
14592 KB |
Output is correct |
31 |
Correct |
13 ms |
14592 KB |
Output is correct |
32 |
Correct |
13 ms |
14592 KB |
Output is correct |
33 |
Correct |
15 ms |
14592 KB |
Output is correct |
34 |
Correct |
13 ms |
14592 KB |
Output is correct |
35 |
Correct |
14 ms |
14592 KB |
Output is correct |
36 |
Correct |
14 ms |
14592 KB |
Output is correct |
37 |
Correct |
17 ms |
14592 KB |
Output is correct |
38 |
Correct |
13 ms |
14592 KB |
Output is correct |
39 |
Correct |
125 ms |
20728 KB |
Output is correct |
40 |
Correct |
135 ms |
20728 KB |
Output is correct |
41 |
Correct |
134 ms |
20856 KB |
Output is correct |
42 |
Correct |
138 ms |
20832 KB |
Output is correct |
43 |
Correct |
168 ms |
21216 KB |
Output is correct |
44 |
Correct |
134 ms |
20856 KB |
Output is correct |
45 |
Correct |
112 ms |
31160 KB |
Output is correct |
46 |
Correct |
78 ms |
40444 KB |
Output is correct |
47 |
Correct |
250 ms |
27512 KB |
Output is correct |
48 |
Correct |
330 ms |
66704 KB |
Output is correct |
49 |
Correct |
318 ms |
65528 KB |
Output is correct |
50 |
Correct |
224 ms |
27512 KB |
Output is correct |
51 |
Correct |
230 ms |
27512 KB |
Output is correct |
52 |
Correct |
263 ms |
27768 KB |
Output is correct |
53 |
Correct |
253 ms |
26872 KB |
Output is correct |
54 |
Correct |
392 ms |
40568 KB |
Output is correct |
55 |
Correct |
27 ms |
15488 KB |
Output is correct |
56 |
Correct |
19 ms |
14976 KB |
Output is correct |
57 |
Correct |
80 ms |
20904 KB |
Output is correct |
58 |
Correct |
65 ms |
20588 KB |
Output is correct |
59 |
Correct |
114 ms |
40440 KB |
Output is correct |
60 |
Correct |
303 ms |
65784 KB |
Output is correct |
61 |
Correct |
252 ms |
29304 KB |
Output is correct |
62 |
Correct |
208 ms |
27640 KB |
Output is correct |
63 |
Correct |
259 ms |
27640 KB |
Output is correct |
64 |
Correct |
503 ms |
36472 KB |
Output is correct |
65 |
Correct |
495 ms |
40824 KB |
Output is correct |
66 |
Correct |
344 ms |
63356 KB |
Output is correct |
67 |
Correct |
183 ms |
28012 KB |
Output is correct |
68 |
Correct |
368 ms |
39032 KB |
Output is correct |
69 |
Correct |
408 ms |
43256 KB |
Output is correct |
70 |
Correct |
351 ms |
38008 KB |
Output is correct |