# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
987702 | Tsagana | Race (IOI11_race) | C++14 | 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 all(x) x.begin(), x.end()
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define F first
#define S second
using namespace std;
int d, dsu[200069], cc[200069], lz[200069][2], z, inf=1e18;
vector <pair<int, int>> adj[200069];
multiset<pair<int, int>> ms[200069];
int fd(int x) {
if (dsu[x] == x) return x;
return dsu[x] = fd(dsu[x]);
}
void jo(int x,int y) {
int k, w; x = fd(x); y = fd(y);
if (cc[x] < cc[y]) swap(x, y);
for (auto it: ms[y]) {
k = it.F + lz[y][1];
w = it.S + lz[y][0];
auto it2 = ms[x].lb({d - k - lz[x][1], -inf});
if (it2 != ms[x].end() && it2.F + lz[x][1] == d - k)
z = min(z, w + it2.S + lz[x][0]);
}
for (auto it: ms[y]) {
k = it.F + lz[y][1];
w = it.S + lz[y][0];
ms[x].insert({k - lz[x][1], w - lz[x][0]});
}
dsu[y] = x; cc[x] += cc[y];
}
void dfs(int x) {
vtd[x] = cc[x] = 1; dsu[x] = x;
ms[x].insert({0, 0});
for (auto i: adj[x]) {
int l = i.F, w = i.S;
if (!vtd[l]) {
dfs(l);
lz[fd(l)][0]++;
lz[fd(l)][1] += w;
jo(x,l);
}
}
}
int best_path(int n,int od,int ed[][2],int wg[]) {
int k, l;
d = od;
for (int i = 0; i < n-1; i++) {
k = ed[i][0];
l = ed[i][1];
adj[k].pb({l, wg[i]});
adj[l].pb({k, wg[i]});
}
z = inf;
dfs(0);
if (z == inf) z = -1;
return z;
}