| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1323793 | Trisanu_Das | Race (IOI11_race) | C++17 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define ff first
#define ss second
int K, ans = INT_MAX; vector<pair<int, int>> adj[200005]; set<pair<int, int>> S[200005];
void dfs(int u, int par, int dist, int depth) {
S[u].insert({dist, depth});
for(auto v : adj[u]){
if(v.ff != par){
dfs(v.ff, u, dist + v.ss, depth + 1);
if(S[u].size() < S[v.ff].size()) swap(S[u], S[v.ff]);
for(auto d : S[v.ff]){
int o = K + 2 * dist - d.f;
auto x = S[u].lower_bound({o, -1});
if(x != S[u].end() && (*x).ff == other)
ans = min(ans, d.ss + (*x).ss - 2 * depth);
}
for(auto d : S[v.ff]) S[u].insert(d);
}
}
}
int best_path(int n, int k, int h[][2], int l[]) {
k = K;
for (int i = 0; i < n - 1; i++){
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}K = k; dfs(0, 0, 0, 0); return (ans == INT_MAX) ? -1 : ans;
}
