# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1067136 | EkinOnal | Race (IOI11_race) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3,unroll-loops,Ofast")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define MAX 200007
#define pb push_back
#define mp make_pair
#define int long long
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>>
const int mod = 1e9+7;
const int INF = 1e18;
// myMap.begin()->first : key
// myMap.begin()->second : value
//int epow(int a,int b){int ans=1;while(b){if(b&1) ans*=a;a*=a;;b>>=1;}return ans;}
//int gcd(int a,int b) {if(a<b)swap(a,b);while(b){int tmp=b;b=a%b;a=tmp;}return a;}
//int mul(int a,int b){return ((a%mod)*(b%mod))%mod;}
//int sum(int a,int b){return ((a%mod)+(b%mod))%mod;}
//typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
vi dead(MAX),sz(MAX),mpp(1e6+5);
vpii adj[MAX];
int n,k,ans=INF;
void dfs(int node,int par){
sz[node]=1;
for(auto u : adj[node]){
if(dead[u.f] || u.f==par) continue;
dfs(u.f,node);
sz[node]+=sz[u.f];
}
}
int findc(int node,int par,int val){
for(auto u : adj[node]){
if(u.f==par || dead[u.f]) continue;
if(sz[u.f]>=val/2) return findc(u.f,node,sz[u.f]); // val olmalidir!!
}
return node;
}
void check(int node,int par,int len,int sayi){
if(len>k) return;
if(mpp[k-len!=INF]) ans=min(ans,mpp[k-len]+sayi);
for(auto u : adj[node]){
if(u.f=u.f=par || dead[u.f]) continue;
check(u.f,node,len+u.s,sayi+1);
}
}
void build(int node,int par,int len,int sayi){
if(len>k) return;
mpp[len]=min(mpp[len],sayi);
for(auto u : adj[node]){
if(u.f==par || dead[u.f]) continue;
build(u.f,node,len+u.s,sayi+1);
}
}
void solve(int x){
dfs(x,x);
x = findc(x,x,sz[x]);
dead[x]=true;
for(auto u : adj[x]){
if(dead[u.f]) continue;
check(u.s,x,u.s,1);
build(u.s,x,u.s,1);
}
// cout<<ans<<endl;
return;
}
int best_path(int N, int K, int H[][2], int L[]){
k=K; n=N;
// memset(mpp,-1,sizeof(mpp));
fill(mpp.begin(),mpp.end(),INF);
for(int i=0;i<N-1;i++){
adj[H[i][0]].pb({H[i][1],L[i]});
adj[H[i][1]].pb({H[i][0],L[i]});
}
ans=N+2;
solve(0);
if(ans==N+2)ans=-1;
return ans;
}