# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
466567 | dattranxxx | Race (IOI11_race) | C++11 | 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"
/*
* Author : shora
*/
#include <bits/stdc++.h>
#define print(_v) for (auto &_ : _v) {cerr << _ << ' ';} cerr << endl;
using namespace std;
using ll = long long;
const int oo = 1e9;
const int N = 5e5, K = 1e6;
struct C {
int v, w;
C(int v, int w): v(v), w(w) {}
};
vector<C> G[N];
int vis[N], size[N], f[K + 1], dep[N];
int k;
int res = oo;
void find_size(int u, int e = -1) {
size[u] = 1;
for (C& c : G[u]) if (c.v != e && !vis[c.v]) {
find_size(c.v, u);
size[u] += size[c.v];
}
}
int centroid(int s, int u, int e = -1) {
for (C& c : G[u]) if (c.v != e && !vis[c.v])
if (size[c.v] > s / 2)
return centroid(s, c.v, u);
return u;
}
void dfs(int u, int e = -1, ll dis = 0) {
if (k < dis)
return;
if (e != -1)
dep[u] = dep[e] + 1;
if (f[k - dis] != oo)
res = min(res, f[k - dis] + dep[u]);
for (C& c : G[u]) if (c.v != e && !vis[c.v]) {
dfs(c.v, u, dis + c.w);
}
f[dis] = min(f[dis], dep[u]);
}
void clear(int u, int e = -1, ll dis = 0) {
if (k < dis)
return;
dep[u] = 0;
for (C& c : G[u]) if (c.v != e && !vis[c.v]) {
clear(c.v, u, dis + c.w);
}
f[dis] = oo;
}
void solve(int u) {
find_size(u);
u = centroid(size[u], u);
vis[u] = 1;
dfs(u); clear(u);
for (C& c : G[u]) if (!vis[c.v])
solve(c.v);
}