#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
const int N = 200002;
vector<pair<int,int>> adj[N];
int k;
int siz[N];
bool vis[N];
int cnt[1000003];
int ans;
vector<int> v;
void subtree(int node , int par = -1){
siz[node] = 1;
for(auto i : adj[node]){
if(!vis[i.vf] && i.vf != par){
subtree(i.vf , node);
siz[node] += siz[i.vf];
}
}
}
int get(int node , int par , int need){
for(auto i : adj[node]){
if(!vis[i.vf] && i.vf != par && siz[i.vf] > need){
return get(i.vf , node , need);
}
}
return node;
}
void cal(int node , int par , int depth , int cur , int fill){
if(cur > k)return;
if(fill){
ans = min(ans , depth + cnt[k - cur]);
}
else {
cnt[cur] = min(cnt[cur] , depth);
v.pb(cur);
}
for(auto i : adj[node]){
if(!vis[i.vf] && i.vf != par){
cal(i.vf , node , depth + 1 , i.vs + cur , fill);
}
}
}
void centriod(int node){
subtree(node);
int cen = get(node , -1 , siz[node]/2);
cnt[0] = 0;
v.clear();
for(auto i : adj[cen]){
if(!vis[i.vf]){
cal(i.vf , cen , 1 , i.vs , 1);
cal(i.vf , cen , 1 , i.vs , 0);
}
}
//cout << cen << ' ' << ans << '\n';
for(int i : v)cnt[i] = 1e9;
vis[cen] = 1;
for(auto i : adj[cen]){
if(!vis[i.vf]){
centriod(i.vf);
}
}
}
int best_path(int n , int k1 , int h[][2], int l[]){
for(int i = 0; i < n-1; i++){
adj[h[i][0]].pb(mp(h[i][1] , l[i]));
adj[h[i][1]].pb(mp(h[i][0] , l[i]));
}
k = k1;
memset(vis,0,sizeof vis);
for(int i = 0; i <= k; i++){
cnt[i] = 1e9;
}
ans = 1e9;
centriod(0);
return(ans == 1e9 ? -1 : ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5196 KB |
Output is correct |
2 |
Correct |
2 ms |
5196 KB |
Output is correct |
3 |
Correct |
2 ms |
5196 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
2 ms |
5196 KB |
Output is correct |
6 |
Correct |
3 ms |
5196 KB |
Output is correct |
7 |
Correct |
2 ms |
5196 KB |
Output is correct |
8 |
Correct |
3 ms |
5196 KB |
Output is correct |
9 |
Correct |
2 ms |
5196 KB |
Output is correct |
10 |
Correct |
3 ms |
5232 KB |
Output is correct |
11 |
Correct |
2 ms |
5196 KB |
Output is correct |
12 |
Correct |
2 ms |
5196 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
5196 KB |
Output is correct |
15 |
Correct |
2 ms |
5196 KB |
Output is correct |
16 |
Correct |
3 ms |
5196 KB |
Output is correct |
17 |
Correct |
2 ms |
5196 KB |
Output is correct |
18 |
Correct |
3 ms |
5196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5196 KB |
Output is correct |
2 |
Correct |
2 ms |
5196 KB |
Output is correct |
3 |
Correct |
2 ms |
5196 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
2 ms |
5196 KB |
Output is correct |
6 |
Correct |
3 ms |
5196 KB |
Output is correct |
7 |
Correct |
2 ms |
5196 KB |
Output is correct |
8 |
Correct |
3 ms |
5196 KB |
Output is correct |
9 |
Correct |
2 ms |
5196 KB |
Output is correct |
10 |
Correct |
3 ms |
5232 KB |
Output is correct |
11 |
Correct |
2 ms |
5196 KB |
Output is correct |
12 |
Correct |
2 ms |
5196 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
5196 KB |
Output is correct |
15 |
Correct |
2 ms |
5196 KB |
Output is correct |
16 |
Correct |
3 ms |
5196 KB |
Output is correct |
17 |
Correct |
2 ms |
5196 KB |
Output is correct |
18 |
Correct |
3 ms |
5196 KB |
Output is correct |
19 |
Correct |
2 ms |
5196 KB |
Output is correct |
20 |
Correct |
3 ms |
5196 KB |
Output is correct |
21 |
Correct |
3 ms |
5196 KB |
Output is correct |
22 |
Correct |
5 ms |
8780 KB |
Output is correct |
23 |
Correct |
4 ms |
8140 KB |
Output is correct |
24 |
Correct |
5 ms |
8652 KB |
Output is correct |
25 |
Correct |
5 ms |
8524 KB |
Output is correct |
26 |
Correct |
3 ms |
6476 KB |
Output is correct |
27 |
Correct |
4 ms |
8396 KB |
Output is correct |
28 |
Correct |
3 ms |
5964 KB |
Output is correct |
29 |
Correct |
4 ms |
6476 KB |
Output is correct |
30 |
Correct |
4 ms |
6604 KB |
Output is correct |
31 |
Correct |
4 ms |
7756 KB |
Output is correct |
32 |
Correct |
5 ms |
8012 KB |
Output is correct |
33 |
Correct |
5 ms |
8268 KB |
Output is correct |
34 |
Correct |
4 ms |
7500 KB |
Output is correct |
35 |
Correct |
4 ms |
8396 KB |
Output is correct |
36 |
Correct |
5 ms |
8908 KB |
Output is correct |
37 |
Correct |
5 ms |
8268 KB |
Output is correct |
38 |
Correct |
5 ms |
7244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5196 KB |
Output is correct |
2 |
Correct |
2 ms |
5196 KB |
Output is correct |
3 |
Correct |
2 ms |
5196 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
2 ms |
5196 KB |
Output is correct |
6 |
Correct |
3 ms |
5196 KB |
Output is correct |
7 |
Correct |
2 ms |
5196 KB |
Output is correct |
8 |
Correct |
3 ms |
5196 KB |
Output is correct |
9 |
Correct |
2 ms |
5196 KB |
Output is correct |
10 |
Correct |
3 ms |
5232 KB |
Output is correct |
11 |
Correct |
2 ms |
5196 KB |
Output is correct |
12 |
Correct |
2 ms |
5196 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
5196 KB |
Output is correct |
15 |
Correct |
2 ms |
5196 KB |
Output is correct |
16 |
Correct |
3 ms |
5196 KB |
Output is correct |
17 |
Correct |
2 ms |
5196 KB |
Output is correct |
18 |
Correct |
3 ms |
5196 KB |
Output is correct |
19 |
Correct |
101 ms |
10440 KB |
Output is correct |
20 |
Correct |
98 ms |
10460 KB |
Output is correct |
21 |
Correct |
116 ms |
10816 KB |
Output is correct |
22 |
Correct |
98 ms |
11056 KB |
Output is correct |
23 |
Correct |
63 ms |
10636 KB |
Output is correct |
24 |
Correct |
49 ms |
10564 KB |
Output is correct |
25 |
Correct |
107 ms |
14020 KB |
Output is correct |
26 |
Correct |
87 ms |
18376 KB |
Output is correct |
27 |
Correct |
142 ms |
15840 KB |
Output is correct |
28 |
Correct |
231 ms |
30152 KB |
Output is correct |
29 |
Correct |
212 ms |
28940 KB |
Output is correct |
30 |
Correct |
151 ms |
15888 KB |
Output is correct |
31 |
Correct |
140 ms |
15944 KB |
Output is correct |
32 |
Correct |
172 ms |
15932 KB |
Output is correct |
33 |
Correct |
192 ms |
14788 KB |
Output is correct |
34 |
Correct |
142 ms |
14736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5196 KB |
Output is correct |
2 |
Correct |
2 ms |
5196 KB |
Output is correct |
3 |
Correct |
2 ms |
5196 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
2 ms |
5196 KB |
Output is correct |
6 |
Correct |
3 ms |
5196 KB |
Output is correct |
7 |
Correct |
2 ms |
5196 KB |
Output is correct |
8 |
Correct |
3 ms |
5196 KB |
Output is correct |
9 |
Correct |
2 ms |
5196 KB |
Output is correct |
10 |
Correct |
3 ms |
5232 KB |
Output is correct |
11 |
Correct |
2 ms |
5196 KB |
Output is correct |
12 |
Correct |
2 ms |
5196 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
5196 KB |
Output is correct |
15 |
Correct |
2 ms |
5196 KB |
Output is correct |
16 |
Correct |
3 ms |
5196 KB |
Output is correct |
17 |
Correct |
2 ms |
5196 KB |
Output is correct |
18 |
Correct |
3 ms |
5196 KB |
Output is correct |
19 |
Correct |
2 ms |
5196 KB |
Output is correct |
20 |
Correct |
3 ms |
5196 KB |
Output is correct |
21 |
Correct |
3 ms |
5196 KB |
Output is correct |
22 |
Correct |
5 ms |
8780 KB |
Output is correct |
23 |
Correct |
4 ms |
8140 KB |
Output is correct |
24 |
Correct |
5 ms |
8652 KB |
Output is correct |
25 |
Correct |
5 ms |
8524 KB |
Output is correct |
26 |
Correct |
3 ms |
6476 KB |
Output is correct |
27 |
Correct |
4 ms |
8396 KB |
Output is correct |
28 |
Correct |
3 ms |
5964 KB |
Output is correct |
29 |
Correct |
4 ms |
6476 KB |
Output is correct |
30 |
Correct |
4 ms |
6604 KB |
Output is correct |
31 |
Correct |
4 ms |
7756 KB |
Output is correct |
32 |
Correct |
5 ms |
8012 KB |
Output is correct |
33 |
Correct |
5 ms |
8268 KB |
Output is correct |
34 |
Correct |
4 ms |
7500 KB |
Output is correct |
35 |
Correct |
4 ms |
8396 KB |
Output is correct |
36 |
Correct |
5 ms |
8908 KB |
Output is correct |
37 |
Correct |
5 ms |
8268 KB |
Output is correct |
38 |
Correct |
5 ms |
7244 KB |
Output is correct |
39 |
Correct |
101 ms |
10440 KB |
Output is correct |
40 |
Correct |
98 ms |
10460 KB |
Output is correct |
41 |
Correct |
116 ms |
10816 KB |
Output is correct |
42 |
Correct |
98 ms |
11056 KB |
Output is correct |
43 |
Correct |
63 ms |
10636 KB |
Output is correct |
44 |
Correct |
49 ms |
10564 KB |
Output is correct |
45 |
Correct |
107 ms |
14020 KB |
Output is correct |
46 |
Correct |
87 ms |
18376 KB |
Output is correct |
47 |
Correct |
142 ms |
15840 KB |
Output is correct |
48 |
Correct |
231 ms |
30152 KB |
Output is correct |
49 |
Correct |
212 ms |
28940 KB |
Output is correct |
50 |
Correct |
151 ms |
15888 KB |
Output is correct |
51 |
Correct |
140 ms |
15944 KB |
Output is correct |
52 |
Correct |
172 ms |
15932 KB |
Output is correct |
53 |
Correct |
192 ms |
14788 KB |
Output is correct |
54 |
Correct |
142 ms |
14736 KB |
Output is correct |
55 |
Correct |
9 ms |
5708 KB |
Output is correct |
56 |
Correct |
10 ms |
5836 KB |
Output is correct |
57 |
Correct |
74 ms |
11312 KB |
Output is correct |
58 |
Correct |
34 ms |
11712 KB |
Output is correct |
59 |
Correct |
86 ms |
19068 KB |
Output is correct |
60 |
Correct |
409 ms |
34420 KB |
Output is correct |
61 |
Correct |
154 ms |
16184 KB |
Output is correct |
62 |
Correct |
173 ms |
20880 KB |
Output is correct |
63 |
Correct |
220 ms |
20892 KB |
Output is correct |
64 |
Correct |
385 ms |
18784 KB |
Output is correct |
65 |
Correct |
202 ms |
15808 KB |
Output is correct |
66 |
Correct |
310 ms |
30336 KB |
Output is correct |
67 |
Correct |
94 ms |
21812 KB |
Output is correct |
68 |
Correct |
222 ms |
20800 KB |
Output is correct |
69 |
Correct |
228 ms |
20604 KB |
Output is correct |
70 |
Correct |
206 ms |
20092 KB |
Output is correct |