#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 12, mod = 998244353, inf = 1e9 + 12 ;
long long n, k, ans = maxn, h[maxn], sz[maxn], cost[maxn], U[maxn], V[maxn], dist[maxn];
int H2[maxn][2], L2[maxn];
vector <int> conn[maxn];
map <int, int> mp[maxn];
bitset <maxn> marked;
void dfs_set(int u){
marked[u] = 1;
sz[u] = 1;
for(int e: conn[u]){
int v = u ^ U[e] ^ V[e];
if(!marked[v]){
h[v] = h[u] + 1;
dist[v] = dist[u] + cost[e];
dfs_set(v);
sz[u] += sz[v];
}
}
}
void dfs(int u, int p = -1){
marked[u] = 1;
for(int e: conn[u]){
int v = u ^ U[e] ^ V[e];
if(!marked[v]){
dfs(v, u);
if(mp[v].find(k + dist[u]) != mp[v].end())
ans = min(ans, mp[v][k + dist[u]] - h[u]);
}
}
pair<long long, int> mx = make_pair(0, -1);
for(int e: conn[u]){
int v = u ^ U[e] ^ V[e];
if(v != p)
mx = max(mx, make_pair(sz[v], v));
}
int f = mx.second;
if(f != -1){
swap(mp[f], mp[u]);
for(int e: conn[u]){
int v = u ^ U[e] ^ V[e];
if(v != p && v != f){
for(auto len: mp[v]){
if(mp[u].find(k + dist[u] * 2 - len.first) != mp[u].end())
ans = min(ans, mp[u][k + dist[u] * 2 - len.first] + len.second - 2 * h[u]);
}
for(auto len: mp[v]){
int x = mp[u][len.first];
if(x == 0)
mp[u][len.first] = len.second;
else
mp[u][len.first] = min(x, len.second);
}
}
}
}
long long x = mp[u][dist[u]];
if(x == 0)
mp[u][dist[u]] = h[u];
else
mp[u][dist[u]] = min(x, h[u]);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for(int i = 0; i < n - 1; i++){
cost[i] = L[i];
U[i] = H[i][0];
V[i] = H[i][1];
conn[U[i]].push_back(i);
conn[V[i]].push_back(i);
}
// cout << n << ' ' << k << endl;
dfs_set(1);
marked = 0;
dfs(1);
if(ans == maxn)
ans = -1;
return ans;
}
/*
int main(){
int N, K;
cin >> N >> K;
for(int i = 0; i < N - 1; i++)
cin >> H2[i][0] >> H2[i][0];
for(int i = 0; i < N - 1; i++)
cin >> L2[i];
cout << best_path(N, K, H2, L2) << endl;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14548 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14536 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14536 KB |
Output is correct |
11 |
Correct |
9 ms |
14536 KB |
Output is correct |
12 |
Correct |
8 ms |
14532 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14432 KB |
Output is correct |
15 |
Correct |
8 ms |
14528 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14548 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14536 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14536 KB |
Output is correct |
11 |
Correct |
9 ms |
14536 KB |
Output is correct |
12 |
Correct |
8 ms |
14532 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14432 KB |
Output is correct |
15 |
Correct |
8 ms |
14528 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14540 KB |
Output is correct |
20 |
Correct |
8 ms |
14408 KB |
Output is correct |
21 |
Correct |
11 ms |
14748 KB |
Output is correct |
22 |
Correct |
9 ms |
14680 KB |
Output is correct |
23 |
Correct |
10 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14728 KB |
Output is correct |
25 |
Correct |
9 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14572 KB |
Output is correct |
28 |
Correct |
10 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14676 KB |
Output is correct |
30 |
Correct |
12 ms |
14764 KB |
Output is correct |
31 |
Correct |
10 ms |
14676 KB |
Output is correct |
32 |
Correct |
12 ms |
14672 KB |
Output is correct |
33 |
Correct |
9 ms |
14664 KB |
Output is correct |
34 |
Correct |
9 ms |
14672 KB |
Output is correct |
35 |
Correct |
10 ms |
14764 KB |
Output is correct |
36 |
Correct |
11 ms |
14676 KB |
Output is correct |
37 |
Correct |
9 ms |
14672 KB |
Output is correct |
38 |
Correct |
9 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14548 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14536 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14536 KB |
Output is correct |
11 |
Correct |
9 ms |
14536 KB |
Output is correct |
12 |
Correct |
8 ms |
14532 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14432 KB |
Output is correct |
15 |
Correct |
8 ms |
14528 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
142 ms |
33236 KB |
Output is correct |
20 |
Correct |
145 ms |
33128 KB |
Output is correct |
21 |
Correct |
137 ms |
33176 KB |
Output is correct |
22 |
Correct |
150 ms |
33360 KB |
Output is correct |
23 |
Correct |
157 ms |
43200 KB |
Output is correct |
24 |
Correct |
129 ms |
38016 KB |
Output is correct |
25 |
Correct |
100 ms |
42268 KB |
Output is correct |
26 |
Correct |
59 ms |
43616 KB |
Output is correct |
27 |
Correct |
248 ms |
45628 KB |
Output is correct |
28 |
Correct |
302 ms |
80384 KB |
Output is correct |
29 |
Correct |
306 ms |
70944 KB |
Output is correct |
30 |
Correct |
249 ms |
45644 KB |
Output is correct |
31 |
Correct |
253 ms |
45636 KB |
Output is correct |
32 |
Correct |
286 ms |
45772 KB |
Output is correct |
33 |
Correct |
246 ms |
45140 KB |
Output is correct |
34 |
Correct |
348 ms |
70760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14548 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14536 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14536 KB |
Output is correct |
11 |
Correct |
9 ms |
14536 KB |
Output is correct |
12 |
Correct |
8 ms |
14532 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14432 KB |
Output is correct |
15 |
Correct |
8 ms |
14528 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14540 KB |
Output is correct |
20 |
Correct |
8 ms |
14408 KB |
Output is correct |
21 |
Correct |
11 ms |
14748 KB |
Output is correct |
22 |
Correct |
9 ms |
14680 KB |
Output is correct |
23 |
Correct |
10 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14728 KB |
Output is correct |
25 |
Correct |
9 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14572 KB |
Output is correct |
28 |
Correct |
10 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14676 KB |
Output is correct |
30 |
Correct |
12 ms |
14764 KB |
Output is correct |
31 |
Correct |
10 ms |
14676 KB |
Output is correct |
32 |
Correct |
12 ms |
14672 KB |
Output is correct |
33 |
Correct |
9 ms |
14664 KB |
Output is correct |
34 |
Correct |
9 ms |
14672 KB |
Output is correct |
35 |
Correct |
10 ms |
14764 KB |
Output is correct |
36 |
Correct |
11 ms |
14676 KB |
Output is correct |
37 |
Correct |
9 ms |
14672 KB |
Output is correct |
38 |
Correct |
9 ms |
14676 KB |
Output is correct |
39 |
Correct |
142 ms |
33236 KB |
Output is correct |
40 |
Correct |
145 ms |
33128 KB |
Output is correct |
41 |
Correct |
137 ms |
33176 KB |
Output is correct |
42 |
Correct |
150 ms |
33360 KB |
Output is correct |
43 |
Correct |
157 ms |
43200 KB |
Output is correct |
44 |
Correct |
129 ms |
38016 KB |
Output is correct |
45 |
Correct |
100 ms |
42268 KB |
Output is correct |
46 |
Correct |
59 ms |
43616 KB |
Output is correct |
47 |
Correct |
248 ms |
45628 KB |
Output is correct |
48 |
Correct |
302 ms |
80384 KB |
Output is correct |
49 |
Correct |
306 ms |
70944 KB |
Output is correct |
50 |
Correct |
249 ms |
45644 KB |
Output is correct |
51 |
Correct |
253 ms |
45636 KB |
Output is correct |
52 |
Correct |
286 ms |
45772 KB |
Output is correct |
53 |
Correct |
246 ms |
45140 KB |
Output is correct |
54 |
Correct |
348 ms |
70760 KB |
Output is correct |
55 |
Correct |
23 ms |
17116 KB |
Output is correct |
56 |
Correct |
16 ms |
16128 KB |
Output is correct |
57 |
Correct |
84 ms |
32076 KB |
Output is correct |
58 |
Correct |
56 ms |
29620 KB |
Output is correct |
59 |
Correct |
85 ms |
48292 KB |
Output is correct |
60 |
Correct |
270 ms |
68012 KB |
Output is correct |
61 |
Correct |
269 ms |
48312 KB |
Output is correct |
62 |
Correct |
217 ms |
45636 KB |
Output is correct |
63 |
Correct |
278 ms |
45644 KB |
Output is correct |
64 |
Correct |
481 ms |
84088 KB |
Output is correct |
65 |
Correct |
482 ms |
83824 KB |
Output is correct |
66 |
Correct |
288 ms |
78412 KB |
Output is correct |
67 |
Correct |
201 ms |
46016 KB |
Output is correct |
68 |
Correct |
450 ms |
59800 KB |
Output is correct |
69 |
Correct |
398 ms |
60356 KB |
Output is correct |
70 |
Correct |
361 ms |
57800 KB |
Output is correct |