#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
const int MOD = 998244353;
const int maxN = 200000;
const int maxK = 1000001;
vector<pair<int, ll>> adj[maxN];
int curr[maxK];
int sub[maxN];
bool done[maxN];
vector<int> changed;
int n, k, ans;
int find_size(int x, int p){
int sz = 1;
for(auto pr: adj[x]){
if(done[pr.first] == 0 && p != pr.first){
sz += find_size(pr.first, x);
}
}
sub[x] = sz;
return sz;
}
int get_centroid(int x, int sz, int p){
for(auto pr : adj[x]){
if(done[pr.first] == 0 && p != pr.first && sub[pr.first] > sz / 2){
return get_centroid(pr.first, sz, x);
}
}
return x;
}
void gt(int x, int p, ll km, int edges){
if(km > k) return;
int need = k - km;
if(curr[need] != -1) ans = min(ans, edges + curr[need]);
for(auto pr : adj[x]){
if(done[pr.first] == 0 && p != pr.first){
gt(pr.first, x, km + pr.second, edges + 1);
}
}
}
void st(int x, int p, ll km, int edges){
if(km >= k) return;
if(curr[km] == -1){
curr[km] = edges;
changed.push_back(km);
}else curr[km] = min(curr[km], edges);
for(auto pr : adj[x]){
if(done[pr.first] == 0 && p != pr.first){
st(pr.first, x, km + pr.second, edges + 1);
}
}
}
void decompose(int x){
int sz = find_size(x, -1);
int centr = get_centroid(x, sz, -1);
done[centr] = 1;
curr[0] = 0; changed.push_back(0);
for(auto p : adj[centr]){
if(!done[p.first]){
gt(p.first, centr, p.second, 1);
st(p.first, centr, p.second, 1);
}
}
for(int y : changed) curr[y] = -1;
changed.clear();
for(auto p : adj[centr]){
if(!done[p.first]) {decompose(p.first);}
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
for(int i = 0; i < N - 1; i++){
int u = H[i][0]; int v = H[i][1];
adj[u].push_back({v, L[i]});
adj[v].push_back({u, L[i]});
}
for(int i = 0; i <= k; i++) curr[i] = -1;
for(int i = 0; i <= n; i++) {done[i] = 0; sub[i] = 0;}
ans = 1e9;
decompose(0);
return (ans == 1e9 ? -1 : ans);
}
//~ #define MAX_N 500000
//~ static int N, K;
//~ static int H[MAX_N][2];
//~ static int L[MAX_N];
//~ static int solution;
//~ inline
//~ void my_assert(int e) {if (!e) abort();}
//~ void read_input()
//~ {
//~ int i;
//~ my_assert(2==scanf("%d %d",&N,&K));
//~ for(i=0; i<N-1; i++)
//~ my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
//~ my_assert(1==scanf("%d",&solution));
//~ }
//~ int main()
//~ {
//~ int ans;
//~ read_input();
//~ ans = best_path(N,K,H,L);
//~ if(ans==solution)
//~ printf("Correct.\n");
//~ else
//~ printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);
//~ return 0;
//~ }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
3 ms |
5020 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
4 ms |
8020 KB |
Output is correct |
24 |
Correct |
5 ms |
8480 KB |
Output is correct |
25 |
Correct |
5 ms |
8356 KB |
Output is correct |
26 |
Correct |
4 ms |
6356 KB |
Output is correct |
27 |
Correct |
4 ms |
8224 KB |
Output is correct |
28 |
Correct |
3 ms |
5804 KB |
Output is correct |
29 |
Correct |
4 ms |
6356 KB |
Output is correct |
30 |
Correct |
4 ms |
6560 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7820 KB |
Output is correct |
33 |
Correct |
4 ms |
8148 KB |
Output is correct |
34 |
Correct |
4 ms |
7380 KB |
Output is correct |
35 |
Correct |
4 ms |
8276 KB |
Output is correct |
36 |
Correct |
5 ms |
8660 KB |
Output is correct |
37 |
Correct |
5 ms |
8220 KB |
Output is correct |
38 |
Correct |
4 ms |
6996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
116 ms |
13124 KB |
Output is correct |
20 |
Correct |
147 ms |
13124 KB |
Output is correct |
21 |
Correct |
124 ms |
13132 KB |
Output is correct |
22 |
Correct |
117 ms |
13100 KB |
Output is correct |
23 |
Correct |
68 ms |
13584 KB |
Output is correct |
24 |
Correct |
51 ms |
12704 KB |
Output is correct |
25 |
Correct |
115 ms |
16832 KB |
Output is correct |
26 |
Correct |
81 ms |
20512 KB |
Output is correct |
27 |
Correct |
155 ms |
21924 KB |
Output is correct |
28 |
Correct |
230 ms |
36384 KB |
Output is correct |
29 |
Correct |
225 ms |
35148 KB |
Output is correct |
30 |
Correct |
152 ms |
21980 KB |
Output is correct |
31 |
Correct |
176 ms |
21900 KB |
Output is correct |
32 |
Correct |
196 ms |
22060 KB |
Output is correct |
33 |
Correct |
176 ms |
20720 KB |
Output is correct |
34 |
Correct |
176 ms |
21736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
3 ms |
5020 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
4 ms |
8020 KB |
Output is correct |
24 |
Correct |
5 ms |
8480 KB |
Output is correct |
25 |
Correct |
5 ms |
8356 KB |
Output is correct |
26 |
Correct |
4 ms |
6356 KB |
Output is correct |
27 |
Correct |
4 ms |
8224 KB |
Output is correct |
28 |
Correct |
3 ms |
5804 KB |
Output is correct |
29 |
Correct |
4 ms |
6356 KB |
Output is correct |
30 |
Correct |
4 ms |
6560 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7820 KB |
Output is correct |
33 |
Correct |
4 ms |
8148 KB |
Output is correct |
34 |
Correct |
4 ms |
7380 KB |
Output is correct |
35 |
Correct |
4 ms |
8276 KB |
Output is correct |
36 |
Correct |
5 ms |
8660 KB |
Output is correct |
37 |
Correct |
5 ms |
8220 KB |
Output is correct |
38 |
Correct |
4 ms |
6996 KB |
Output is correct |
39 |
Correct |
116 ms |
13124 KB |
Output is correct |
40 |
Correct |
147 ms |
13124 KB |
Output is correct |
41 |
Correct |
124 ms |
13132 KB |
Output is correct |
42 |
Correct |
117 ms |
13100 KB |
Output is correct |
43 |
Correct |
68 ms |
13584 KB |
Output is correct |
44 |
Correct |
51 ms |
12704 KB |
Output is correct |
45 |
Correct |
115 ms |
16832 KB |
Output is correct |
46 |
Correct |
81 ms |
20512 KB |
Output is correct |
47 |
Correct |
155 ms |
21924 KB |
Output is correct |
48 |
Correct |
230 ms |
36384 KB |
Output is correct |
49 |
Correct |
225 ms |
35148 KB |
Output is correct |
50 |
Correct |
152 ms |
21980 KB |
Output is correct |
51 |
Correct |
176 ms |
21900 KB |
Output is correct |
52 |
Correct |
196 ms |
22060 KB |
Output is correct |
53 |
Correct |
176 ms |
20720 KB |
Output is correct |
54 |
Correct |
176 ms |
21736 KB |
Output is correct |
55 |
Correct |
13 ms |
5780 KB |
Output is correct |
56 |
Correct |
11 ms |
5716 KB |
Output is correct |
57 |
Correct |
64 ms |
13424 KB |
Output is correct |
58 |
Correct |
37 ms |
12476 KB |
Output is correct |
59 |
Correct |
93 ms |
21892 KB |
Output is correct |
60 |
Correct |
440 ms |
40512 KB |
Output is correct |
61 |
Correct |
167 ms |
22208 KB |
Output is correct |
62 |
Correct |
167 ms |
25932 KB |
Output is correct |
63 |
Correct |
227 ms |
26012 KB |
Output is correct |
64 |
Correct |
477 ms |
24652 KB |
Output is correct |
65 |
Correct |
209 ms |
22576 KB |
Output is correct |
66 |
Correct |
316 ms |
36812 KB |
Output is correct |
67 |
Correct |
101 ms |
25028 KB |
Output is correct |
68 |
Correct |
268 ms |
26664 KB |
Output is correct |
69 |
Correct |
204 ms |
26504 KB |
Output is correct |
70 |
Correct |
190 ms |
25884 KB |
Output is correct |