#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
#define xc3(x) (x*(x-1)*(x-2)/6)
#define xc2(x) ((x)*((x)-1)/2)
#define abs(x) ((x)>0?(x):-((x)))
#define min(x,y) ((x)>(y)?(y):(x))
//#define debug 1
using namespace std;
using namespace __gnu_pbds;
//void FILEREAD(){freopen("text_input.txt","r",stdin);freopen("output.txt","w",stdout);}
#define ll long long
#define ull unsigned long long
#define pi pair<int,int>
#define pll pair<long long,long long>
#define pli pair<long long,int>
#define pil pair<int,long long>
#define vi vector<int>
#define vll vector<long long>
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long,long long>>
#define vvi vector<vector<int>>
#define vvll vector<vector<long long>>
#define vvpi vector<vector<pair<int,int>>>
#define vvpll vector<vector<pair<long,long>>>
#define vec vector
#define pr pair
#define fi first
#define se second
#define fr(i,j) for(int i=0;i<j;i++)
#define testcase(x) int x;cin>>x;while(x--)solve()
#define vecinp(v) for(auto &in_vx:v)cin>>in_vx
#define pb push_back
typedef tree<ll,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_Set;
ordered_Set os;
#define MAX 99999997
#define MOD 92182
#define maxM 1000005
vvpi adj(maxM,vpi(0));
vec<bool> visited(maxM);
vi prec(maxM);
ll ans = LLONG_MAX;
int NN,KK;
void edge(int v1,int v2,int val){
adj[v1].pb({v2,val});
adj[v2].pb({v1,val});
}
int precf(int n,int p){
prec[n] = 1;
for(auto &x:adj[n]){
if(x.first!=p && !visited[x.first])prec[n] += precf(x.first,n);
}
return prec[n];
}
int centroid(int n){
int u = precf(n,-1);
int p = n, l = -1;
for(int i=0;i<adj[p].size();i++){
int x = adj[p][i].first;
if(x==l || visited[x])continue;
if(prec[x]*2>u){
i=-1;
l=p;
p=x;
}
}
return p;
}
int d = 0;
vi cnt(maxM,MAX);
void dfs(int type, int node , int parent , int complexity , int km){
if(km>KK)return;
d = max(d,km);
if(type) ans = min(ans,cnt[KK-km]+complexity);
else cnt[km] = min(cnt[km],complexity);
for(auto &x:adj[node]){
if(x.first == parent || visited[x.first])continue;
dfs(type,x.first,node,complexity+1,km+x.second);
}
}
void solve(int n){
int cen = centroid(n);
visited[cen] = 1;
cnt[0]=0;
// MAIN OPERATIONS
d = 0;
for(auto &x:adj[cen]){
if(visited[x.first])continue;
dfs(1,x.first,cen,1,x.second);
dfs(0,x.first,cen,1,x.second);
}
//cnt.assign(maxM,MAX);
fill(cnt.begin(),cnt.begin()+d+1,MAX);
//fill(cnt.begin()+1,cnt.begin()+d+5,MAX);
// GO TO MY CHILDREN AND REPEAT
for(auto &x:adj[cen]){
if(visited[x.first])continue;
solve(x.first);
}
}
int best_path(int N,int K,int H[][2], int L[]){
// OUTPUT && REMOVE LATER
fr(i,N-1){
edge(H[i][0],H[i][1],L[i]);
}
NN = N;
KK = K;
// MAIN CODE STARTS HERE
//
solve(0);
return (ans>N?-1:ans);
}
/*
int main(){
int N,K; N = 11; K = 12;
cin>>N>>K;
int H[N-1][2];
int L[N-1];
for(int i=0;i<N-1;i++){
cin >> H[i][0] >> H[i][1] >> L[i];
}
cout << best_path(N,K,H,L);
}*/
Compilation message
race.cpp: In function 'int centroid(int)':
race.cpp:59:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=0;i<adj[p].size();i++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33884 KB |
Output is correct |
2 |
Correct |
9 ms |
33884 KB |
Output is correct |
3 |
Correct |
9 ms |
33828 KB |
Output is correct |
4 |
Correct |
10 ms |
33884 KB |
Output is correct |
5 |
Correct |
10 ms |
33884 KB |
Output is correct |
6 |
Correct |
10 ms |
33884 KB |
Output is correct |
7 |
Correct |
10 ms |
33944 KB |
Output is correct |
8 |
Correct |
10 ms |
33884 KB |
Output is correct |
9 |
Correct |
10 ms |
33884 KB |
Output is correct |
10 |
Correct |
10 ms |
33884 KB |
Output is correct |
11 |
Correct |
10 ms |
33884 KB |
Output is correct |
12 |
Correct |
10 ms |
33824 KB |
Output is correct |
13 |
Correct |
10 ms |
33884 KB |
Output is correct |
14 |
Correct |
10 ms |
33880 KB |
Output is correct |
15 |
Correct |
9 ms |
34136 KB |
Output is correct |
16 |
Correct |
11 ms |
33884 KB |
Output is correct |
17 |
Correct |
10 ms |
33884 KB |
Output is correct |
18 |
Correct |
10 ms |
33884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33884 KB |
Output is correct |
2 |
Correct |
9 ms |
33884 KB |
Output is correct |
3 |
Correct |
9 ms |
33828 KB |
Output is correct |
4 |
Correct |
10 ms |
33884 KB |
Output is correct |
5 |
Correct |
10 ms |
33884 KB |
Output is correct |
6 |
Correct |
10 ms |
33884 KB |
Output is correct |
7 |
Correct |
10 ms |
33944 KB |
Output is correct |
8 |
Correct |
10 ms |
33884 KB |
Output is correct |
9 |
Correct |
10 ms |
33884 KB |
Output is correct |
10 |
Correct |
10 ms |
33884 KB |
Output is correct |
11 |
Correct |
10 ms |
33884 KB |
Output is correct |
12 |
Correct |
10 ms |
33824 KB |
Output is correct |
13 |
Correct |
10 ms |
33884 KB |
Output is correct |
14 |
Correct |
10 ms |
33880 KB |
Output is correct |
15 |
Correct |
9 ms |
34136 KB |
Output is correct |
16 |
Correct |
11 ms |
33884 KB |
Output is correct |
17 |
Correct |
10 ms |
33884 KB |
Output is correct |
18 |
Correct |
10 ms |
33884 KB |
Output is correct |
19 |
Correct |
10 ms |
33884 KB |
Output is correct |
20 |
Correct |
11 ms |
33884 KB |
Output is correct |
21 |
Correct |
11 ms |
33780 KB |
Output is correct |
22 |
Correct |
104 ms |
34136 KB |
Output is correct |
23 |
Correct |
74 ms |
33880 KB |
Output is correct |
24 |
Correct |
91 ms |
33880 KB |
Output is correct |
25 |
Correct |
22 ms |
33880 KB |
Output is correct |
26 |
Correct |
41 ms |
33976 KB |
Output is correct |
27 |
Correct |
10 ms |
34036 KB |
Output is correct |
28 |
Correct |
20 ms |
33884 KB |
Output is correct |
29 |
Correct |
20 ms |
33884 KB |
Output is correct |
30 |
Correct |
21 ms |
33884 KB |
Output is correct |
31 |
Correct |
21 ms |
33884 KB |
Output is correct |
32 |
Correct |
23 ms |
33772 KB |
Output is correct |
33 |
Correct |
25 ms |
33884 KB |
Output is correct |
34 |
Correct |
36 ms |
33964 KB |
Output is correct |
35 |
Correct |
28 ms |
33880 KB |
Output is correct |
36 |
Correct |
34 ms |
33880 KB |
Output is correct |
37 |
Correct |
13 ms |
33948 KB |
Output is correct |
38 |
Correct |
13 ms |
33880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33884 KB |
Output is correct |
2 |
Correct |
9 ms |
33884 KB |
Output is correct |
3 |
Correct |
9 ms |
33828 KB |
Output is correct |
4 |
Correct |
10 ms |
33884 KB |
Output is correct |
5 |
Correct |
10 ms |
33884 KB |
Output is correct |
6 |
Correct |
10 ms |
33884 KB |
Output is correct |
7 |
Correct |
10 ms |
33944 KB |
Output is correct |
8 |
Correct |
10 ms |
33884 KB |
Output is correct |
9 |
Correct |
10 ms |
33884 KB |
Output is correct |
10 |
Correct |
10 ms |
33884 KB |
Output is correct |
11 |
Correct |
10 ms |
33884 KB |
Output is correct |
12 |
Correct |
10 ms |
33824 KB |
Output is correct |
13 |
Correct |
10 ms |
33884 KB |
Output is correct |
14 |
Correct |
10 ms |
33880 KB |
Output is correct |
15 |
Correct |
9 ms |
34136 KB |
Output is correct |
16 |
Correct |
11 ms |
33884 KB |
Output is correct |
17 |
Correct |
10 ms |
33884 KB |
Output is correct |
18 |
Correct |
10 ms |
33884 KB |
Output is correct |
19 |
Correct |
83 ms |
39744 KB |
Output is correct |
20 |
Correct |
85 ms |
39764 KB |
Output is correct |
21 |
Correct |
91 ms |
39764 KB |
Output is correct |
22 |
Correct |
79 ms |
39756 KB |
Output is correct |
23 |
Correct |
59 ms |
40020 KB |
Output is correct |
24 |
Correct |
46 ms |
39736 KB |
Output is correct |
25 |
Correct |
90 ms |
43260 KB |
Output is correct |
26 |
Correct |
75 ms |
46932 KB |
Output is correct |
27 |
Correct |
110 ms |
43616 KB |
Output is correct |
28 |
Correct |
169 ms |
57936 KB |
Output is correct |
29 |
Correct |
168 ms |
56656 KB |
Output is correct |
30 |
Correct |
121 ms |
43836 KB |
Output is correct |
31 |
Correct |
107 ms |
43604 KB |
Output is correct |
32 |
Correct |
137 ms |
43744 KB |
Output is correct |
33 |
Correct |
134 ms |
42580 KB |
Output is correct |
34 |
Correct |
116 ms |
42560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33884 KB |
Output is correct |
2 |
Correct |
9 ms |
33884 KB |
Output is correct |
3 |
Correct |
9 ms |
33828 KB |
Output is correct |
4 |
Correct |
10 ms |
33884 KB |
Output is correct |
5 |
Correct |
10 ms |
33884 KB |
Output is correct |
6 |
Correct |
10 ms |
33884 KB |
Output is correct |
7 |
Correct |
10 ms |
33944 KB |
Output is correct |
8 |
Correct |
10 ms |
33884 KB |
Output is correct |
9 |
Correct |
10 ms |
33884 KB |
Output is correct |
10 |
Correct |
10 ms |
33884 KB |
Output is correct |
11 |
Correct |
10 ms |
33884 KB |
Output is correct |
12 |
Correct |
10 ms |
33824 KB |
Output is correct |
13 |
Correct |
10 ms |
33884 KB |
Output is correct |
14 |
Correct |
10 ms |
33880 KB |
Output is correct |
15 |
Correct |
9 ms |
34136 KB |
Output is correct |
16 |
Correct |
11 ms |
33884 KB |
Output is correct |
17 |
Correct |
10 ms |
33884 KB |
Output is correct |
18 |
Correct |
10 ms |
33884 KB |
Output is correct |
19 |
Correct |
10 ms |
33884 KB |
Output is correct |
20 |
Correct |
11 ms |
33884 KB |
Output is correct |
21 |
Correct |
11 ms |
33780 KB |
Output is correct |
22 |
Correct |
104 ms |
34136 KB |
Output is correct |
23 |
Correct |
74 ms |
33880 KB |
Output is correct |
24 |
Correct |
91 ms |
33880 KB |
Output is correct |
25 |
Correct |
22 ms |
33880 KB |
Output is correct |
26 |
Correct |
41 ms |
33976 KB |
Output is correct |
27 |
Correct |
10 ms |
34036 KB |
Output is correct |
28 |
Correct |
20 ms |
33884 KB |
Output is correct |
29 |
Correct |
20 ms |
33884 KB |
Output is correct |
30 |
Correct |
21 ms |
33884 KB |
Output is correct |
31 |
Correct |
21 ms |
33884 KB |
Output is correct |
32 |
Correct |
23 ms |
33772 KB |
Output is correct |
33 |
Correct |
25 ms |
33884 KB |
Output is correct |
34 |
Correct |
36 ms |
33964 KB |
Output is correct |
35 |
Correct |
28 ms |
33880 KB |
Output is correct |
36 |
Correct |
34 ms |
33880 KB |
Output is correct |
37 |
Correct |
13 ms |
33948 KB |
Output is correct |
38 |
Correct |
13 ms |
33880 KB |
Output is correct |
39 |
Correct |
83 ms |
39744 KB |
Output is correct |
40 |
Correct |
85 ms |
39764 KB |
Output is correct |
41 |
Correct |
91 ms |
39764 KB |
Output is correct |
42 |
Correct |
79 ms |
39756 KB |
Output is correct |
43 |
Correct |
59 ms |
40020 KB |
Output is correct |
44 |
Correct |
46 ms |
39736 KB |
Output is correct |
45 |
Correct |
90 ms |
43260 KB |
Output is correct |
46 |
Correct |
75 ms |
46932 KB |
Output is correct |
47 |
Correct |
110 ms |
43616 KB |
Output is correct |
48 |
Correct |
169 ms |
57936 KB |
Output is correct |
49 |
Correct |
168 ms |
56656 KB |
Output is correct |
50 |
Correct |
121 ms |
43836 KB |
Output is correct |
51 |
Correct |
107 ms |
43604 KB |
Output is correct |
52 |
Correct |
137 ms |
43744 KB |
Output is correct |
53 |
Correct |
134 ms |
42580 KB |
Output is correct |
54 |
Correct |
116 ms |
42560 KB |
Output is correct |
55 |
Correct |
18 ms |
34392 KB |
Output is correct |
56 |
Correct |
16 ms |
34396 KB |
Output is correct |
57 |
Correct |
63 ms |
41260 KB |
Output is correct |
58 |
Correct |
36 ms |
40900 KB |
Output is correct |
59 |
Correct |
78 ms |
48448 KB |
Output is correct |
60 |
Correct |
257 ms |
59984 KB |
Output is correct |
61 |
Correct |
125 ms |
46672 KB |
Output is correct |
62 |
Correct |
163 ms |
46708 KB |
Output is correct |
63 |
Correct |
148 ms |
46636 KB |
Output is correct |
64 |
Correct |
657 ms |
47008 KB |
Output is correct |
65 |
Correct |
179 ms |
47444 KB |
Output is correct |
66 |
Correct |
412 ms |
57424 KB |
Output is correct |
67 |
Correct |
76 ms |
47296 KB |
Output is correct |
68 |
Correct |
281 ms |
47440 KB |
Output is correct |
69 |
Correct |
247 ms |
47648 KB |
Output is correct |
70 |
Correct |
223 ms |
46928 KB |
Output is correct |