This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int inf=1e9+17;
const int MAX_N=2e5+14;
const int KK=1e6+1;
int n,k;
int ans;
int mx_depth;
int cnt[KK]={0};
bool removed[MAX_N];
bool subtree[MAX_N];
vector<vector<pair<int,int>>>graph;
int treesize(int node,int parent){
subtree[node]=1;
for(auto [w,child]:graph[node]){
if(child!=parent && !removed[child]){
subtree[node]+=treesize(child,node);
}
}
return subtree[node];
}
int findcentroid(int node,int parent,int treesz){
for(auto[w,child]:graph[node]){
if(child!=parent && !removed[child]&& treesize(child,node)>(treesz/2)){
findcentroid(child,node,treesz);
}
}
return node;
}
void getans(int sum,int node,int parent,int depth,bool filling){
if (sum>k)return;
mx_depth=max(mx_depth,sum);
if(filling)cnt[sum]=min(cnt[sum],depth);
else{
ans=min(ans,depth+cnt[k-depth]);
}
for(auto[w,child]:graph[node]){
if(child!=parent && ! removed[child]){
getans(sum+w,child,node,depth+1,filling);
}
}
}
void centroid_decomp(int node,int parent,int treesz){
int centroid=findcentroid( node, parent, treesz);
removed[centroid]=true;
for(auto [w,child]:graph[centroid]){
if(!removed[child]){
getans(w,child,centroid,1,false);
getans(w,child,centroid,1,true);}
}
fill(cnt+1,cnt+mx_depth+1,inf);
for(auto[w,i]:graph[centroid]){
if(!removed[i]){
centroid_decomp(i,centroid,treesize(centroid,centroid));}
}
}
int best_path(int N,int K,int H[][2],int L[]){
mx_depth=0;
n=N;
k=k;
graph.assign(n,vector<pair<int,int>>());
for(int i=0;i<n-1;i++){
int u=H[i][0];
int v=H[i][1];
int w=L[i];
graph[u].push_back({w,v});
graph[v].push_back({w,u});
}
ans=inf;
memset(cnt,inf,sizeof(cnt));
memset(removed,false,sizeof(removed));
centroid_decomp(0,-1,n/2);
ans=cnt[k];
return ans;
}
Compilation message (stderr)
race.cpp: In function 'int treesize(int, int)':
race.cpp:15:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
15 | for(auto [w,child]:graph[node]){
| ^
race.cpp: In function 'int findcentroid(int, int, int)':
race.cpp:23:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for(auto[w,child]:graph[node]){
| ^
race.cpp: In function 'void getans(int, int, int, int, bool)':
race.cpp:37:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto[w,child]:graph[node]){
| ^
race.cpp: In function 'void centroid_decomp(int, int, int)':
race.cpp:46:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for(auto [w,child]:graph[centroid]){
| ^
race.cpp:52:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for(auto[w,i]:graph[centroid]){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |