#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <bitset>
#include <deque>
#include <stack>
#include <array>
#include <cassert>
#define ll long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define MK make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define ALL(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define SZ(x) (int)x.size()
#define CASE int t;cin>>t;for(int ca=1;ca<=t;ca++)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int MAX = 1 << 20, MOD = 1e9 + 7;
vector<pii> edge[MAX];
int cnt[MAX], vis[MAX], w[MAX];
int n, k, half;
int ans = 2e9;
map<ll, int> dis;
void dfs(int now, int from){
cnt[now] = 1;
for(pii y : edge[now]){
int i = y.F;
if(i == from || vis[i])
continue;
dfs(i, now);
cnt[now] += cnt[i];
}
}
int find(int now, int from){
for(pii y : edge[now]){
int i = y.F;
if(i == from || vis[i])
continue;
if(cnt[i] > half)
return find(i, now);
}
return now;
}
void calc1(int now, int from, int d, ll val){
if(val > k)
return;
if(dis.count(k - val)){
ans = min(ans, d + dis[k - val]);
}
for(pii y: edge[now]){
int i = y.F;
if(vis[i] || i == from)
continue;
calc1(i, now, d + 1, val + y.S);
}
}
void calc2(int now, int from, int d, ll val){
if(val > k){
return;
}
if(dis.count(val) != 0){
dis[val] = min(dis[val], d);
}else{
dis[val] = d;
}
for(pii y : edge[now]){
int i = y.F;
if(vis[i] || i == from)
continue;
calc2(i, now, d + 1, val + y.S);
}
}
void solve(int x){
dfs(x, x);
half = cnt[x] / 2;
int c = find(x, x);
vis[c] = 1;
dis[0] = 0;
for(pii i : edge[c]){
int y = i.F;
if(!vis[y]){
calc1(y, y, 1, i.S);
calc2(y, y, 1, i.S);
}
}
dis.clear();
for(pii i : edge[c]){
if(!vis[i.F])
solve(i.F);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for(int i = 0;i < n - 1;i++){
edge[H[i][0]].PB(MK(H[i][1], L[i]));
edge[H[i][1]].PB(MK(H[i][0], L[i]));
}
solve(0);
return (ans == 2e9 ? -1 : ans);
}
// int main(){
// IOS
// 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];
// }
// for(int i = 0;i < N - 1;i++)
// cin >> L[i];
// cout << best_path(N, K, H, L) << '\n';
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24916 KB |
Output is correct |
2 |
Correct |
16 ms |
24944 KB |
Output is correct |
3 |
Correct |
14 ms |
24860 KB |
Output is correct |
4 |
Correct |
13 ms |
24940 KB |
Output is correct |
5 |
Correct |
14 ms |
24916 KB |
Output is correct |
6 |
Correct |
14 ms |
24976 KB |
Output is correct |
7 |
Correct |
13 ms |
24916 KB |
Output is correct |
8 |
Correct |
14 ms |
24944 KB |
Output is correct |
9 |
Correct |
14 ms |
24936 KB |
Output is correct |
10 |
Correct |
14 ms |
24936 KB |
Output is correct |
11 |
Correct |
13 ms |
24936 KB |
Output is correct |
12 |
Correct |
13 ms |
24940 KB |
Output is correct |
13 |
Correct |
14 ms |
24916 KB |
Output is correct |
14 |
Correct |
14 ms |
24936 KB |
Output is correct |
15 |
Correct |
13 ms |
24920 KB |
Output is correct |
16 |
Correct |
17 ms |
24916 KB |
Output is correct |
17 |
Correct |
17 ms |
24972 KB |
Output is correct |
18 |
Correct |
17 ms |
24936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24916 KB |
Output is correct |
2 |
Correct |
16 ms |
24944 KB |
Output is correct |
3 |
Correct |
14 ms |
24860 KB |
Output is correct |
4 |
Correct |
13 ms |
24940 KB |
Output is correct |
5 |
Correct |
14 ms |
24916 KB |
Output is correct |
6 |
Correct |
14 ms |
24976 KB |
Output is correct |
7 |
Correct |
13 ms |
24916 KB |
Output is correct |
8 |
Correct |
14 ms |
24944 KB |
Output is correct |
9 |
Correct |
14 ms |
24936 KB |
Output is correct |
10 |
Correct |
14 ms |
24936 KB |
Output is correct |
11 |
Correct |
13 ms |
24936 KB |
Output is correct |
12 |
Correct |
13 ms |
24940 KB |
Output is correct |
13 |
Correct |
14 ms |
24916 KB |
Output is correct |
14 |
Correct |
14 ms |
24936 KB |
Output is correct |
15 |
Correct |
13 ms |
24920 KB |
Output is correct |
16 |
Correct |
17 ms |
24916 KB |
Output is correct |
17 |
Correct |
17 ms |
24972 KB |
Output is correct |
18 |
Correct |
17 ms |
24936 KB |
Output is correct |
19 |
Correct |
13 ms |
24916 KB |
Output is correct |
20 |
Correct |
13 ms |
24940 KB |
Output is correct |
21 |
Correct |
14 ms |
24956 KB |
Output is correct |
22 |
Correct |
14 ms |
24916 KB |
Output is correct |
23 |
Correct |
14 ms |
24956 KB |
Output is correct |
24 |
Correct |
14 ms |
24948 KB |
Output is correct |
25 |
Correct |
14 ms |
25044 KB |
Output is correct |
26 |
Correct |
15 ms |
24916 KB |
Output is correct |
27 |
Correct |
14 ms |
24916 KB |
Output is correct |
28 |
Correct |
16 ms |
24952 KB |
Output is correct |
29 |
Correct |
14 ms |
25044 KB |
Output is correct |
30 |
Correct |
15 ms |
25080 KB |
Output is correct |
31 |
Correct |
14 ms |
25060 KB |
Output is correct |
32 |
Correct |
15 ms |
25044 KB |
Output is correct |
33 |
Correct |
15 ms |
24988 KB |
Output is correct |
34 |
Correct |
14 ms |
25032 KB |
Output is correct |
35 |
Correct |
15 ms |
25044 KB |
Output is correct |
36 |
Correct |
15 ms |
25044 KB |
Output is correct |
37 |
Correct |
19 ms |
25172 KB |
Output is correct |
38 |
Correct |
15 ms |
25048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24916 KB |
Output is correct |
2 |
Correct |
16 ms |
24944 KB |
Output is correct |
3 |
Correct |
14 ms |
24860 KB |
Output is correct |
4 |
Correct |
13 ms |
24940 KB |
Output is correct |
5 |
Correct |
14 ms |
24916 KB |
Output is correct |
6 |
Correct |
14 ms |
24976 KB |
Output is correct |
7 |
Correct |
13 ms |
24916 KB |
Output is correct |
8 |
Correct |
14 ms |
24944 KB |
Output is correct |
9 |
Correct |
14 ms |
24936 KB |
Output is correct |
10 |
Correct |
14 ms |
24936 KB |
Output is correct |
11 |
Correct |
13 ms |
24936 KB |
Output is correct |
12 |
Correct |
13 ms |
24940 KB |
Output is correct |
13 |
Correct |
14 ms |
24916 KB |
Output is correct |
14 |
Correct |
14 ms |
24936 KB |
Output is correct |
15 |
Correct |
13 ms |
24920 KB |
Output is correct |
16 |
Correct |
17 ms |
24916 KB |
Output is correct |
17 |
Correct |
17 ms |
24972 KB |
Output is correct |
18 |
Correct |
17 ms |
24936 KB |
Output is correct |
19 |
Correct |
199 ms |
31088 KB |
Output is correct |
20 |
Correct |
234 ms |
31092 KB |
Output is correct |
21 |
Correct |
246 ms |
31148 KB |
Output is correct |
22 |
Correct |
230 ms |
31152 KB |
Output is correct |
23 |
Correct |
108 ms |
31420 KB |
Output is correct |
24 |
Correct |
89 ms |
31220 KB |
Output is correct |
25 |
Correct |
180 ms |
34668 KB |
Output is correct |
26 |
Correct |
120 ms |
38360 KB |
Output is correct |
27 |
Correct |
207 ms |
36888 KB |
Output is correct |
28 |
Correct |
310 ms |
51156 KB |
Output is correct |
29 |
Correct |
280 ms |
50244 KB |
Output is correct |
30 |
Correct |
254 ms |
36908 KB |
Output is correct |
31 |
Correct |
235 ms |
37068 KB |
Output is correct |
32 |
Correct |
245 ms |
37068 KB |
Output is correct |
33 |
Correct |
297 ms |
35764 KB |
Output is correct |
34 |
Correct |
199 ms |
35792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24916 KB |
Output is correct |
2 |
Correct |
16 ms |
24944 KB |
Output is correct |
3 |
Correct |
14 ms |
24860 KB |
Output is correct |
4 |
Correct |
13 ms |
24940 KB |
Output is correct |
5 |
Correct |
14 ms |
24916 KB |
Output is correct |
6 |
Correct |
14 ms |
24976 KB |
Output is correct |
7 |
Correct |
13 ms |
24916 KB |
Output is correct |
8 |
Correct |
14 ms |
24944 KB |
Output is correct |
9 |
Correct |
14 ms |
24936 KB |
Output is correct |
10 |
Correct |
14 ms |
24936 KB |
Output is correct |
11 |
Correct |
13 ms |
24936 KB |
Output is correct |
12 |
Correct |
13 ms |
24940 KB |
Output is correct |
13 |
Correct |
14 ms |
24916 KB |
Output is correct |
14 |
Correct |
14 ms |
24936 KB |
Output is correct |
15 |
Correct |
13 ms |
24920 KB |
Output is correct |
16 |
Correct |
17 ms |
24916 KB |
Output is correct |
17 |
Correct |
17 ms |
24972 KB |
Output is correct |
18 |
Correct |
17 ms |
24936 KB |
Output is correct |
19 |
Correct |
13 ms |
24916 KB |
Output is correct |
20 |
Correct |
13 ms |
24940 KB |
Output is correct |
21 |
Correct |
14 ms |
24956 KB |
Output is correct |
22 |
Correct |
14 ms |
24916 KB |
Output is correct |
23 |
Correct |
14 ms |
24956 KB |
Output is correct |
24 |
Correct |
14 ms |
24948 KB |
Output is correct |
25 |
Correct |
14 ms |
25044 KB |
Output is correct |
26 |
Correct |
15 ms |
24916 KB |
Output is correct |
27 |
Correct |
14 ms |
24916 KB |
Output is correct |
28 |
Correct |
16 ms |
24952 KB |
Output is correct |
29 |
Correct |
14 ms |
25044 KB |
Output is correct |
30 |
Correct |
15 ms |
25080 KB |
Output is correct |
31 |
Correct |
14 ms |
25060 KB |
Output is correct |
32 |
Correct |
15 ms |
25044 KB |
Output is correct |
33 |
Correct |
15 ms |
24988 KB |
Output is correct |
34 |
Correct |
14 ms |
25032 KB |
Output is correct |
35 |
Correct |
15 ms |
25044 KB |
Output is correct |
36 |
Correct |
15 ms |
25044 KB |
Output is correct |
37 |
Correct |
19 ms |
25172 KB |
Output is correct |
38 |
Correct |
15 ms |
25048 KB |
Output is correct |
39 |
Correct |
199 ms |
31088 KB |
Output is correct |
40 |
Correct |
234 ms |
31092 KB |
Output is correct |
41 |
Correct |
246 ms |
31148 KB |
Output is correct |
42 |
Correct |
230 ms |
31152 KB |
Output is correct |
43 |
Correct |
108 ms |
31420 KB |
Output is correct |
44 |
Correct |
89 ms |
31220 KB |
Output is correct |
45 |
Correct |
180 ms |
34668 KB |
Output is correct |
46 |
Correct |
120 ms |
38360 KB |
Output is correct |
47 |
Correct |
207 ms |
36888 KB |
Output is correct |
48 |
Correct |
310 ms |
51156 KB |
Output is correct |
49 |
Correct |
280 ms |
50244 KB |
Output is correct |
50 |
Correct |
254 ms |
36908 KB |
Output is correct |
51 |
Correct |
235 ms |
37068 KB |
Output is correct |
52 |
Correct |
245 ms |
37068 KB |
Output is correct |
53 |
Correct |
297 ms |
35764 KB |
Output is correct |
54 |
Correct |
199 ms |
35792 KB |
Output is correct |
55 |
Correct |
33 ms |
25792 KB |
Output is correct |
56 |
Correct |
26 ms |
25560 KB |
Output is correct |
57 |
Correct |
150 ms |
31244 KB |
Output is correct |
58 |
Correct |
58 ms |
31296 KB |
Output is correct |
59 |
Correct |
299 ms |
43052 KB |
Output is correct |
60 |
Correct |
874 ms |
62020 KB |
Output is correct |
61 |
Correct |
303 ms |
37312 KB |
Output is correct |
62 |
Correct |
290 ms |
37096 KB |
Output is correct |
63 |
Correct |
366 ms |
37168 KB |
Output is correct |
64 |
Correct |
988 ms |
42876 KB |
Output is correct |
65 |
Correct |
242 ms |
36988 KB |
Output is correct |
66 |
Correct |
602 ms |
47740 KB |
Output is correct |
67 |
Correct |
140 ms |
37304 KB |
Output is correct |
68 |
Correct |
415 ms |
41852 KB |
Output is correct |
69 |
Correct |
449 ms |
41792 KB |
Output is correct |
70 |
Correct |
382 ms |
41260 KB |
Output is correct |