# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
891286 | Erfan1386Y | 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 "race.h"
#include <bits/stdc++.h>
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);
#define what(x) cerr << #x << " is " << x << '\n';
#define kill(x) {cout << x << '\n'; return 0;}
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define F first
#define S second
const ll inf = 1e16, mod = 1e9 + 7, delta = 1e9 + 9, SQ = 450, P = 6065621;
using namespace std;
#define int ll
const ll N = 2e5 + 10, LG = 20;
ll n, k, ans = inf;
vector<pii> adj[N];
set<pii> s[N];
void add (int v, int u, int w) {
for (auto x: s[u]) {
auto y = *s[v].lower_bound(pii(k - x.F - w, 0));
if (y.F != k - x.F - w) continue;
ans = min(ans, x.S + y.S);
}
for (auto x: s[u]) {
s[v].insert({x.F + w, x.S + 1});
}
s[u].clear();
}
void dfs (int v, int p = -1) {
for (auto u: adj[v]) {
if (u.F - p) {
dfs(u.F, v);
if (s[v].size() < s[u.F].size())
s[v].swap(s[u.F]);
add(v, u.F, u.S);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
fast_io;
n = N, k = K;
for (int i = 0; i <= n; i++) {
s[i].insert({inf, 0});
s[i].insert({0, 0});
}
for (int i = 0; i < n - 1; i++) {
int u, v, w;
u = H[i][0], v = H[i][1];
w = L[i];
adj[u].pb({v, w});
adj[v].pb({u, w});
}
dfs(0);
// for (auto u: s[0])
// cout << u.F << ' ' << u.S << '\n';
if (ans >= inf)
return -1;
return ans + 1;
}