#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) (v).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int NN = 2e5+7;
vector <vector <pii>> graph(NN);
vector <int> dis(1e6+7, 2e9), dis2(1e6+7, 2e9);
vector <int> saizu(NN), vis(NN), par(NN), cnt(1e6+7);
int ans = 2e9, need;
void calculate(int v, int p, int dista, int val, int flag){
if(dista > need) return;
if(flag){
cnt[dista]++;
if(dis[dista] == 2e9){
dis[dista] = val;
}
else if(dis[dista] >= val){
dis2[dista] = dis[dista];
dis[dista] = val;
}
}
else{
cnt[dista]--;
if(dis[dista] == val){
dis[dista] = dis2[dista];
dis2[dista] = 2e9;
}
else if(dis2[dista] == val){
dis2[dista] = 2e9;
}
}
for(auto to : graph[v]){
if(!vis[to.first] && to.first != p){
calculate(to.first, v, dista+to.second, val+1, flag);
}
}
}
void solve(int v, int p, int dista, int val){
if(dista > need) return;
if(cnt[need-dista]){
ans = min(ans, val+dis[need-dista]);
}
for(auto to : graph[v]){
if(!vis[to.first] && to.first != p){
solve(to.first, v, dista+to.second, val+1);
}
}
}
void find_size(int v, int p = -1){
if(vis[v]) return;
saizu[v] = 1;
for(auto to : graph[v]){
if(to.first != p && !vis[to.first]){
find_size(to.first, v);
saizu[v] += saizu[to.first];
}
}
}
int find_centroid(int v, int p, int n){
for(auto to : graph[v]){
if(to.first != p && !vis[to.first]){
if(!vis[to.first] && saizu[to.first] > n/2){
return find_centroid(to.first, v, n);
}
}
}
return v;
}
void init_centroid(int v = 0, int p = -1){
find_size(v);
int c = find_centroid(v, -1, saizu[v]);
vis[c] = 1;
par[c] = p;
calculate(c, par[c], 0, 0, 1);
if(cnt[need]){
ans = min(ans, dis[need]);
}
for(auto to : graph[c]){
if(!vis[to.first]){
calculate(to.first, c, to.second, 1, 0);
solve(to.first, c, to.second, 1);
calculate(to.first, c, to.second, 1, 1);
}
}
calculate(c, par[c], 0, 0, 0);
for(auto to : graph[c]){
if(!vis[to.first]){
init_centroid(to.first, c);
}
}
vis[c] = 0;
}
int best_path(int n, int k, int h[][2], int l[]){
int i;
need = k;
for(i = 0; i < n-1; i++){
graph[h[i][0]].pb(mp(h[i][1], l[i]));
graph[h[i][1]].pb(mp(h[i][0], l[i]));
}
init_centroid();
return ans == 2e9 ? -1 : ans;
}
/*
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
4 3
0 1 1
1 2 2
1 3 4
2
3 3
0 1 1
1 2 1
-1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19200 KB |
Output is correct |
2 |
Correct |
14 ms |
19072 KB |
Output is correct |
3 |
Correct |
14 ms |
19200 KB |
Output is correct |
4 |
Correct |
14 ms |
19200 KB |
Output is correct |
5 |
Correct |
14 ms |
19200 KB |
Output is correct |
6 |
Correct |
14 ms |
19200 KB |
Output is correct |
7 |
Correct |
14 ms |
19200 KB |
Output is correct |
8 |
Correct |
14 ms |
19200 KB |
Output is correct |
9 |
Correct |
14 ms |
19200 KB |
Output is correct |
10 |
Correct |
14 ms |
19200 KB |
Output is correct |
11 |
Correct |
14 ms |
19200 KB |
Output is correct |
12 |
Correct |
15 ms |
19200 KB |
Output is correct |
13 |
Correct |
14 ms |
19200 KB |
Output is correct |
14 |
Correct |
14 ms |
19200 KB |
Output is correct |
15 |
Correct |
14 ms |
19072 KB |
Output is correct |
16 |
Correct |
14 ms |
19168 KB |
Output is correct |
17 |
Correct |
14 ms |
19200 KB |
Output is correct |
18 |
Correct |
14 ms |
19200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19200 KB |
Output is correct |
2 |
Correct |
14 ms |
19072 KB |
Output is correct |
3 |
Correct |
14 ms |
19200 KB |
Output is correct |
4 |
Correct |
14 ms |
19200 KB |
Output is correct |
5 |
Correct |
14 ms |
19200 KB |
Output is correct |
6 |
Correct |
14 ms |
19200 KB |
Output is correct |
7 |
Correct |
14 ms |
19200 KB |
Output is correct |
8 |
Correct |
14 ms |
19200 KB |
Output is correct |
9 |
Correct |
14 ms |
19200 KB |
Output is correct |
10 |
Correct |
14 ms |
19200 KB |
Output is correct |
11 |
Correct |
14 ms |
19200 KB |
Output is correct |
12 |
Correct |
15 ms |
19200 KB |
Output is correct |
13 |
Correct |
14 ms |
19200 KB |
Output is correct |
14 |
Correct |
14 ms |
19200 KB |
Output is correct |
15 |
Correct |
14 ms |
19072 KB |
Output is correct |
16 |
Correct |
14 ms |
19168 KB |
Output is correct |
17 |
Correct |
14 ms |
19200 KB |
Output is correct |
18 |
Correct |
14 ms |
19200 KB |
Output is correct |
19 |
Correct |
14 ms |
19072 KB |
Output is correct |
20 |
Correct |
15 ms |
19072 KB |
Output is correct |
21 |
Correct |
15 ms |
19200 KB |
Output is correct |
22 |
Correct |
15 ms |
19200 KB |
Output is correct |
23 |
Correct |
15 ms |
19200 KB |
Output is correct |
24 |
Correct |
15 ms |
19200 KB |
Output is correct |
25 |
Correct |
16 ms |
19200 KB |
Output is correct |
26 |
Correct |
15 ms |
19200 KB |
Output is correct |
27 |
Correct |
14 ms |
19200 KB |
Output is correct |
28 |
Correct |
15 ms |
19200 KB |
Output is correct |
29 |
Correct |
16 ms |
19200 KB |
Output is correct |
30 |
Correct |
16 ms |
19200 KB |
Output is correct |
31 |
Correct |
16 ms |
19200 KB |
Output is correct |
32 |
Correct |
16 ms |
19328 KB |
Output is correct |
33 |
Correct |
16 ms |
19232 KB |
Output is correct |
34 |
Correct |
15 ms |
19200 KB |
Output is correct |
35 |
Correct |
15 ms |
19200 KB |
Output is correct |
36 |
Correct |
15 ms |
19232 KB |
Output is correct |
37 |
Correct |
15 ms |
19200 KB |
Output is correct |
38 |
Correct |
15 ms |
19200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19200 KB |
Output is correct |
2 |
Correct |
14 ms |
19072 KB |
Output is correct |
3 |
Correct |
14 ms |
19200 KB |
Output is correct |
4 |
Correct |
14 ms |
19200 KB |
Output is correct |
5 |
Correct |
14 ms |
19200 KB |
Output is correct |
6 |
Correct |
14 ms |
19200 KB |
Output is correct |
7 |
Correct |
14 ms |
19200 KB |
Output is correct |
8 |
Correct |
14 ms |
19200 KB |
Output is correct |
9 |
Correct |
14 ms |
19200 KB |
Output is correct |
10 |
Correct |
14 ms |
19200 KB |
Output is correct |
11 |
Correct |
14 ms |
19200 KB |
Output is correct |
12 |
Correct |
15 ms |
19200 KB |
Output is correct |
13 |
Correct |
14 ms |
19200 KB |
Output is correct |
14 |
Correct |
14 ms |
19200 KB |
Output is correct |
15 |
Correct |
14 ms |
19072 KB |
Output is correct |
16 |
Correct |
14 ms |
19168 KB |
Output is correct |
17 |
Correct |
14 ms |
19200 KB |
Output is correct |
18 |
Correct |
14 ms |
19200 KB |
Output is correct |
19 |
Correct |
294 ms |
24080 KB |
Output is correct |
20 |
Correct |
269 ms |
23928 KB |
Output is correct |
21 |
Correct |
320 ms |
24056 KB |
Output is correct |
22 |
Correct |
307 ms |
24184 KB |
Output is correct |
23 |
Correct |
128 ms |
24184 KB |
Output is correct |
24 |
Correct |
104 ms |
24184 KB |
Output is correct |
25 |
Correct |
233 ms |
26872 KB |
Output is correct |
26 |
Correct |
177 ms |
31100 KB |
Output is correct |
27 |
Correct |
277 ms |
31864 KB |
Output is correct |
28 |
Correct |
430 ms |
43384 KB |
Output is correct |
29 |
Correct |
417 ms |
42360 KB |
Output is correct |
30 |
Correct |
274 ms |
31864 KB |
Output is correct |
31 |
Correct |
289 ms |
31864 KB |
Output is correct |
32 |
Correct |
346 ms |
31992 KB |
Output is correct |
33 |
Correct |
401 ms |
30716 KB |
Output is correct |
34 |
Correct |
307 ms |
31864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19200 KB |
Output is correct |
2 |
Correct |
14 ms |
19072 KB |
Output is correct |
3 |
Correct |
14 ms |
19200 KB |
Output is correct |
4 |
Correct |
14 ms |
19200 KB |
Output is correct |
5 |
Correct |
14 ms |
19200 KB |
Output is correct |
6 |
Correct |
14 ms |
19200 KB |
Output is correct |
7 |
Correct |
14 ms |
19200 KB |
Output is correct |
8 |
Correct |
14 ms |
19200 KB |
Output is correct |
9 |
Correct |
14 ms |
19200 KB |
Output is correct |
10 |
Correct |
14 ms |
19200 KB |
Output is correct |
11 |
Correct |
14 ms |
19200 KB |
Output is correct |
12 |
Correct |
15 ms |
19200 KB |
Output is correct |
13 |
Correct |
14 ms |
19200 KB |
Output is correct |
14 |
Correct |
14 ms |
19200 KB |
Output is correct |
15 |
Correct |
14 ms |
19072 KB |
Output is correct |
16 |
Correct |
14 ms |
19168 KB |
Output is correct |
17 |
Correct |
14 ms |
19200 KB |
Output is correct |
18 |
Correct |
14 ms |
19200 KB |
Output is correct |
19 |
Correct |
14 ms |
19072 KB |
Output is correct |
20 |
Correct |
15 ms |
19072 KB |
Output is correct |
21 |
Correct |
15 ms |
19200 KB |
Output is correct |
22 |
Correct |
15 ms |
19200 KB |
Output is correct |
23 |
Correct |
15 ms |
19200 KB |
Output is correct |
24 |
Correct |
15 ms |
19200 KB |
Output is correct |
25 |
Correct |
16 ms |
19200 KB |
Output is correct |
26 |
Correct |
15 ms |
19200 KB |
Output is correct |
27 |
Correct |
14 ms |
19200 KB |
Output is correct |
28 |
Correct |
15 ms |
19200 KB |
Output is correct |
29 |
Correct |
16 ms |
19200 KB |
Output is correct |
30 |
Correct |
16 ms |
19200 KB |
Output is correct |
31 |
Correct |
16 ms |
19200 KB |
Output is correct |
32 |
Correct |
16 ms |
19328 KB |
Output is correct |
33 |
Correct |
16 ms |
19232 KB |
Output is correct |
34 |
Correct |
15 ms |
19200 KB |
Output is correct |
35 |
Correct |
15 ms |
19200 KB |
Output is correct |
36 |
Correct |
15 ms |
19232 KB |
Output is correct |
37 |
Correct |
15 ms |
19200 KB |
Output is correct |
38 |
Correct |
15 ms |
19200 KB |
Output is correct |
39 |
Correct |
294 ms |
24080 KB |
Output is correct |
40 |
Correct |
269 ms |
23928 KB |
Output is correct |
41 |
Correct |
320 ms |
24056 KB |
Output is correct |
42 |
Correct |
307 ms |
24184 KB |
Output is correct |
43 |
Correct |
128 ms |
24184 KB |
Output is correct |
44 |
Correct |
104 ms |
24184 KB |
Output is correct |
45 |
Correct |
233 ms |
26872 KB |
Output is correct |
46 |
Correct |
177 ms |
31100 KB |
Output is correct |
47 |
Correct |
277 ms |
31864 KB |
Output is correct |
48 |
Correct |
430 ms |
43384 KB |
Output is correct |
49 |
Correct |
417 ms |
42360 KB |
Output is correct |
50 |
Correct |
274 ms |
31864 KB |
Output is correct |
51 |
Correct |
289 ms |
31864 KB |
Output is correct |
52 |
Correct |
346 ms |
31992 KB |
Output is correct |
53 |
Correct |
401 ms |
30716 KB |
Output is correct |
54 |
Correct |
307 ms |
31864 KB |
Output is correct |
55 |
Correct |
28 ms |
19712 KB |
Output is correct |
56 |
Correct |
32 ms |
19712 KB |
Output is correct |
57 |
Correct |
173 ms |
25616 KB |
Output is correct |
58 |
Correct |
63 ms |
25452 KB |
Output is correct |
59 |
Correct |
200 ms |
31096 KB |
Output is correct |
60 |
Correct |
1066 ms |
42648 KB |
Output is correct |
61 |
Correct |
324 ms |
32120 KB |
Output is correct |
62 |
Correct |
371 ms |
31992 KB |
Output is correct |
63 |
Correct |
541 ms |
32120 KB |
Output is correct |
64 |
Correct |
1259 ms |
32376 KB |
Output is correct |
65 |
Correct |
357 ms |
33144 KB |
Output is correct |
66 |
Correct |
853 ms |
40568 KB |
Output is correct |
67 |
Correct |
201 ms |
32744 KB |
Output is correct |
68 |
Correct |
555 ms |
32888 KB |
Output is correct |
69 |
Correct |
550 ms |
33016 KB |
Output is correct |
70 |
Correct |
512 ms |
32376 KB |
Output is correct |