| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1282917 | mlecio | Race (IOI11_race) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
int n,k,
vector<pair<int,int>>Ver[200'005];
int naj[1'000'006];
bool kil[200'005];
vector<int>czy;
int sz[200'005];
int ans=1e9+1;
int dfs_sz(int u,int v){
sz[u]=1;
void upd(int u,bool cz,int suma,int ile,int v){
if(suma>k)return;
czy.emplace_back(suma);
if(cz){
ans=min(res,naj[k-suma]+ile);
}
else {
naj[suma]=min(naj[suma],ile);
}
for(auto &x:Ver[u]){
if(!kil[x.first] &&x.first!=v){
upd(x,cz,suma+x.second,ile+1,u);
}
}
}
for(auto &x:Ver[u]){
if(x.first!=v && !kill[x.first]){
sz[u]+=dfs_sz(x,u);
}
}
return sz[u];
}
int cent(int u,int v,int nk){
for(auto &x:Ver[u]){
if(x.first!=v && !kill[x.first] && 2*sz[x]>nk)
return cent(x,u,nk);
}
return u;
}
void centroid(int u){
int root=cent(u,-1,dfs_sz(u,-1));
kil[root]=1;
czy.clear();
for(auto &x:Ver[cent]){
upd(x.first,1,x.second,1,-1);
upd(x.first,0,x.second,1,-1);
}
for(auto &x:czy){
naj[x]=1e9+2137;
}
for(auto &x:Ver[root]){
if(!kil[x])
centroid(x);
}
}
int best_path(int N,int K,int V[][2],int len[]){
n=N;
k=K;
for(int i=0;i<k-1;i++){
Ver[V[i][0]+1].push_back({V[i][1]+1,len[i]});
Ver[V[i][1]+1].push_back({V[i][0]+1,len[i]});
}
memset(naj,1e9+2137,sizeof naj);
naj[0]=0;
centroid(0);
return (ans==1e9+1 ? -1 : ans);
}
