# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
941954 | 4QT0R | 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.
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> graph[200004];
bool usun[200004];
int siz[200004];
int ans=200004;
unordered_map<int,int> mp;
int dlugosc;
void prep(int v, int p){
siz[v]=1;
for (auto u : graph[v]){
if (u.first==p || usun[u.first])continue;
prep(u.first,v);
siz[v]+=siz[u.first];
}
}
int find_centr(int v, int p, int n){
int mx_pod=0,mx_son;
for (auto u : graph[v]){
if (u.first==p || usun[u.first])continue;
if (mx_pod<siz[u.first]){
mx_son=u.first;
mx_pod=siz[u.first];
};
}
if (mx_pod*2>n)return find_centr(mx_son,v,n);
else return v;
}
void dfs1(int v, int p, int odl, int dist){
if (mp.find(dlugosc-odl)!=mp.end())ans=min(ans,mp[dlugosc-odl]+dist);
for (auto u : graph[v]){
if (u.first==p || usun[u.first])continue;
dfs1(u.first,v,odl+u.second,dist+1);
}
}
void dfs2(int v, int p, int odl, int dist){
for (auto u : graph[v]){
if (u.first==p || usun[u.first])continue;
dfs2(u.first,v,odl+u.second,dist+1);
}
if (mp.find(odl)==mp.end())mp[odl]=dist;
else mp[odl]=min(mp[odl],dist);
}
void dekomp(int v){
prep(v,-1);
int centr=find_centr(v,-1,siz[v]);
usun[centr]=true;
mp.clear();
for (auto u : graph[centr]){
if (usun[u.first])continue;
dfs1(u.first,centr,u.second,1);
dfs2(u.first,centr,u.second,1);
}
if (mp.find(dlugosc)!=mp.end())ans=min(ans,mp[dlugosc]);
for (auto u : graph[centr]){
if (usun[u.first])continue;
dekomp(u.first);
}
}
int best_path(int n, int k, vector<pair<int,int>> h, vector<int> l){
dlugosc=k;
for (int i = 0; i<n-1; i++){
graph[h[i].first].push_back({h[i].second,l[i]});
graph[h[i].second].push_back({h[i].first,l[i]});
}
dekomp(0);
if (ans>200000)return -1;
return ans;
}