Submission #649895

#TimeUsernameProblemLanguageResultExecution timeMemory
649895KoalaMuchRace (IOI11_race)C++14
0 / 100
5 ms8916 KiB
#include "race.h" #include<iostream> #include<utility> #include<vector> using namespace std; const int N = 2e5+5; int sz[N]; int st[N]; int ft[N]; int ver[N]; int orig_len[N]; int orig_sum[N]; int mem[N*5]; vector<pair<int,int>> g[N]; int t; int ans = 1e9; void preprocess(int u,int len=0,int sum=0,int p = -1){ ++t; st[u] = t; ver[t] = u; orig_sum[u] = sum; orig_len[u] = len; sz[u] = 1; for(auto &x:g[u]){ if(x.first!=p){ preprocess(x.first,len+1,sum+x.second,u); sz[u]+=sz[x.first]; } } ft[u] = t; } void solve(int u,int target,int p = -1,bool keep = false){ int mx = -1,big_child = -1,del_sum = orig_sum[u],del_len = orig_len[u]; for(auto &x:g[u]) if(x.first!=p&&sz[x.first]>mx) mx = sz[x.first],big_child = x.first; for(auto &x:g[u]){ if(x.first!=p&&x.first!=big_child){ solve(x.first,target,u,false); } } if(big_child!=-1){ solve(big_child,target,u,true); } int need = target-orig_sum[u]+2*del_sum; if(need>=0&&need<=1000000) ans = min(ans,mem[need]+orig_len[u]-2*del_len); mem[orig_sum[u]] = min(mem[orig_sum[u]],orig_len[u]); for(auto &x:g[u]){ if(x.first!=p&&x.first!=big_child){ for(int i=st[x.first];i<=ft[x.first];i++){ int v = ver[i]; need = target-orig_sum[v]+2*del_sum; if(need>=0&&need<=1000000) ans = min(ans,mem[need]+mem[v]-2*del_len); mem[orig_sum[v]] = min(mem[orig_sum[v]],orig_len[v]); } } } if(!keep){ for(int i=st[u];i<=ft[u];i++){ mem[orig_sum[ver[i]]] = 1e9; } } } int best_path(int N, int K, int H[][2], int L[]) { for(int i=0;i<N;i++) g[H[i][0]].push_back({H[i][1],L[i]}),g[H[i][1]].push_back({H[i][0],L[i]}); for(int i=1;i<=1000000;i++) mem[i] = 1e9; preprocess(0); solve(0,K); if(ans>=N) return -1; return ans; } /*int n, K; int H[N][2]; int L[N]; void read_input() { int i; scanf("%d %d",&n,&K); for(i=0; i<n-1; i++) scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]); } int main(){ int ans; read_input(); ans = best_path(n,K,H,L); cout << ans << "\n"; return 0; }*/ /* 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 3 3 0 1 1 1 2 1 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...