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 "factories.h"
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
int n, sz, C;
vector <vector <pair <int, ll>>> g(6e5);
vector <bool> del(6e5, false);
vector <int> par(6e5, -1);
vector <int> dep(6e5, 0);
vector <vector <ll>> dis(6e5, vector <ll>(20, 1e18));
vector <ll> mn(6e5, 1e18);
queue <int> q;
int get_C(int x) {
if (del[x]) return 0;
del[x] = true;
int mx = 0, sum = 1;
for (auto& p : g[x]) {
int y = p.first, cur;
cur = get_C(y);
mx = max(mx, cur);
sum += cur;
}
mx = max(mx, sz - sum);
if (mx <= sz / 2) C = x;
del[x] = false;
return sum;
}
void dfs(int x, ll d, vector <pair <int, ll>>& v) {
if (del[x]) return;
del[x] = true;
v.push_back({ x,d });
for (auto p : g[x]) dfs(p.first, d + p.second, v);
del[x] = false;
}
void cd(int x, int p, int d) {
if (del[x]) return;
sz = get_C(x);
get_C(x);
int c = C;
vector <pair <int, ll>> v;
dfs(c, 0, v);
del[c] = true;
par[c] = p;
dep[c] = d;
for (auto& p : g[c]) cd(p.first, c, d + 1);
for (auto& p : v) dis[p.first][dep[c]] = p.second;
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
for (int i = 0; i < n - 1; i++) {
g[A[i]].push_back({ B[i],D[i] });
g[B[i]].push_back({ A[i],D[i] });
}
cd(0, -1, 0);
}
void add(int x) {
for (int i = x; i > -1; i = par[i]) {
mn[i] = min(mn[i], dis[x][dep[i]]);
q.push(i);
}
}
ll ask(int x) {
ll ret = 1e18;
for (int i = x; i > -1; i = par[i]) ret = min(ret, dis[x][dep[i]] + mn[i]);
return ret;
}
ll Query(int S, int X[], int T, int Y[]) {
ll ans = 1e18;
for (int i = 0; i < T; i++) add(Y[i]);
for (int i = 0; i < S; i++) ans = min(ans, ask(X[i]));
while (q.size()) { mn[q.front()] = 1e18; q.pop(); }
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |