# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
637820 | erto | 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>
typedef long long int ll;
#define INF (int)(1e9 + 7)
#define INF2 998244353
#define N (ll)(2e5 + 5)
using namespace std;
#define int ll
#define lsb(x) (x & (-x))
int n, k, g;
int ans = INF;
vector<pair<int, int>> v[N];
set<pair<int, int>> s[N];
int d[N];
int dis[N];
int h[N][2], l[N];
void dfs(int x, int p){
for(auto u : v[x]){
if(u.first != p){
d[u.first] = d[x] + 1;
dis[u.first] = dis[x] + u.second;
dfs(u.first, x);
}
}
s[x].insert({dis[x], d[x]});
for(auto u : v[x]){
if(u.first != p){
if(s[x].size() < s[u.first].size()){
swap(s[x], s[u.first]);
}
for(auto j : s[u.first]){
if(x == 0){
cout << j.first << '\n';
}
g = k - (j.first - dis[x]) + dis[x];
auto it = s[x].lower_bound(make_pair(g, 0ll));
if(it != s[x].end() && it->first == g){
ans = min(ans, j.second + it->second - 2 * d[x]);
}
s[x].insert(j);
}
}
}
s[x].insert({0, d[x]});
}
int best_path(int nn,int K,int H[][2], int L[]){
n = nn;
for(int i=0; i<n-1; i++){
v[h[i][0]].push_back({h[i][1], L[i]});
v[h[i][1]].push_back({h[i][0], L[i]});
}
k=K;
dfs(0, -1);
if(ans == INF)return - 1;
return ans;
}