# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
551217 | AriaH | 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 "grader.h"
/* Im the best and i work like that */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
const int N = 1e6 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
mt19937 rng(time(nullptr));
vector < pii > G[N];
vector < int > S[N];
multiset < int > st[N];
int n, k, tot, sub[N], hide[N];
ll dis[N], H[N];
void pre(int v, int P = -1)
{
sub[v] = 1;
for(auto [u, c] : G[v])
{
if(hide[u] || u == P) continue;
pre(u, v);
sub[v] += sub[u];
}
}
int find(int v, int P, int _n)
{
for(auto [u, c] : G[v])
{
if(hide[u] || u == P) continue;
if(sub[u] * 2 > _n) return find(u, v, _n);
}
return v;
}
void calc(int v, int P, int jad)
{
S[jad].push_back(v);
if(dis[v] < N)
{
st[dis[v]].insert(H[v]);
}
for(auto [u, c] : G[v])
{
if(hide[u] || u == P) continue;
H[u] = H[v] + 1;
dis[u] = dis[v] + c;
calc(u, v, jad);
}
}
void dec(int v)
{
pre(v);
int _n = sub[v];
int cen = find(v, -1, _n);
for(auto [u, c] : G[cen])
{
if(hide[u]) continue;
dis[u] = c;
H[u] = 1;
calc(u, cen, u);
}
if(SZ(st[k]))
{
tot = min(tot, *st[k].begin());
}
for(auto [u, c] : G[cen])
{
if(hide[u]) continue;
for(auto node : S[u])
{
if(dis[node] >= N) continue;
st[dis[node]].erase(st[dis[node]].find(H[node]));
}
for(auto node : S[u])
{
if(dis[node] >= k) continue;
if(SZ(st[k - dis[node]]))
{
tot = min((ll)tot, H[node] + *st[k - dis[node]].begin());
}
}
S[u].clear();
}
hide[cen] = 1;
for(auto [u, c] : G[cen])
{
if(hide[u]) continue;
dec(u);
}
}
int best_path(int _n, int K, int H[][2], int L[])
{
tot = 1e9;
n = _n;
k = K;
for(int i = 0; i < n - 1; i ++)
{
int a = H[i][0], b = H[i][1];
G[a].push_back(Mp(b, L[i]));
G[b].push_back(Mp(a, L[i]));
}
dec(1);
if(tot == 1e9)
{
return -1;
}
return tot;
}