# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
41084 | comtalyst | Race (IOI11_race) | C++14 | 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.
/*
* Task: ioi11_race
* Lang: C/C++11
* Site: oj.uz
* Last Update: 12/2/2018
*/
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
using namespace std;
/* Note
----------------------------
Learned :
Bugs found & solved :
Optimizations :
----------------------------
*/
#define x first
#define y second
#define umap unordered_map
#define pqueue priority_queue
#define mset multiset
#define mp make_pair
#define mt make_tuple
#define long long long
#define MOD 1000000007
#define MAX (long)(1e16+5)
#define MIN (long)(-1e16-5)
#define FILEIN_ freopen("__in.txt","r",stdin)
#define FILEOUT_ freopen("__out.txt","w",stdout)
#define FILEIN(text) freopen(text,"r",stdin)
#define FILEOUT(text) freopen(text,"w",stdout)
//long tmp[200005][2],tmp2[200005];
vector<pair<long,long>> adl[200005];
umap<long,long> s[200005];
long ls[200005],sid[200005],lv[200005],sc,n,m,mn=MAX;
void solve(long x,long f){
long i,y,id,v,need,w;
pair<long,long> mx={0,0};
lv[x] = lv[f]+1;
for(i = 0; i < adl[x].size(); i++){
y = adl[x][i].x;
if(y == f) continue;
solve(y,x);
id = sid[y];
mx = max(mx,{s[id].size(),y});
}
// printf(">>>>>>>>> %lld from %lld\n",x,f);
if(mx.x){
sid[x] = sid[mx.y];
}
else{
sid[x] = ++sc;
mx.y = 0;
}
for(i = 0; i < adl[x].size(); i++){
y = adl[x][i].x;
if(y == f || y == mx.y) continue;
id = sid[y];
while(!s[id].empty()){
v = (*s[id].begin()).x + ls[id];
need = m-v-ls[sid[x]];
// printf(":: %lld\n",v);
if(s[sid[x]][need]){
// printf("<<<<< %lld\n",s[sid[x]][need]+(*s[id].begin()).y-lv[x]*2);
mn = min(mn,s[sid[x]][need]+(*s[id].begin()).y-lv[x]*2);
}
else{
s[sid[x]].erase(need);
}
if(s[sid[x]][v-ls[sid[x]]]){
s[sid[x]][v-ls[sid[x]]] = min(s[sid[x]][v-ls[sid[x]]],(*s[id].begin()).y);
}
else{
s[sid[x]][v-ls[sid[x]]] = (*s[id].begin()).y;
}
s[id].erase(s[id].begin());
}
}
for(i = 0; i < adl[x].size(); i++){
y = adl[x][i].x;
w = adl[x][i].y;
if(y == f){
break;
}
}
need = m-w-ls[sid[x]];
if(s[sid[x]][need]){
mn = min(mn,s[sid[x]][need]-lv[f]);
}
else{
s[sid[x]].erase(need);
}
ls[sid[x]] += w;
s[sid[x]][w - ls[sid[x]]] = lv[x];
}
long best_path(long _n,long _m,long _edl[][2],long _edw[]){ // f*ck you 0-based idx
long i,j;
n = _n;
m = _m;
for(i = 0; i < n-1; i++){
adl[_edl[i][0]+1].emplace_back(_edl[i][1]+1,_edw[i]);
adl[_edl[i][1]+1].emplace_back(_edl[i][0]+1,_edw[i]);
}
solve(1,0);
if(mn != MAX){
return mn;
}
else{
return -1;
}
}
/*main(){
long t,i,j,k,_n,_m;
scanf("%lld %lld",&_n,&_m);
for(i = 0; i < _n-1; i++){
scanf("%lld %lld",&tmp[i][0],&tmp[i][1]);
}
for(i = 0; i < _n-1; i++){
scanf("%lld",&tmp2[i]);
}
printf(">> %lld\n",best_path(_n,_m,tmp,tmp2));
return 0;
}*/