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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using ii = pair<int, int>;
#define fi first
#define se second
#define pb push_back
#define numBit(x) (__builtin_popcountll(1ll * (x)))
#define getBit(x, i) ((x) >> (i) & 1)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
template<class X, class Y>
bool minimize(X &x, const Y &y) {
X eps = 1e-9;
if (x > y + eps) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
X eps = 1e-9;
if (x + eps < y) {
x = y;
return true;
} else return false;
}
const int N = 5e5 + 7;
long long oo = 1e18 + 7;
//sample program
int n, sz[N], par[N];
ll ans[N];
gp_hash_table<int, ll> d[N];
bool done[N];
vector<ii> adj[N];
void get_sz(int u, int par) {
sz[u] = 1;
for (auto [v, w]: adj[u]) if (v != par) {
get_sz(v, u); sz[u] += sz[v];
}
}
int centroid(int u, int par, int n) {
for (auto [v, w]: adj[u]) if (v != par && !done[v]) {
if (sz[v] * 2 > n) return centroid(v, u, n);
}
return u;
}
void dfs(int u, int par, int rt, ll dist) {
d[rt][u] = dist;
for (auto [v, w]: adj[u]) if (v != par)
dfs(v, u, rt, dist + w);
}
void solve(int u, int p) {
get_sz(u, p); u = centroid(u, p, sz[u]);
done[u] = 1; dfs(u, p, u, 0);
for (auto [v, w]: adj[u]) if (v != p && !done[v])
par[v] = u, solve(v, u);
}
void Init(int N, int A[], int B[], int D[]){
n = N; fill(ans, ans + n + 1, oo);
for (int i = 0; i < n - 1; i++) {
adj[A[i]].pb(ii(B[i], D[i])); adj[B[i]].pb(ii(A[i], D[i]));
}
solve(1, -1);
}
void upd(int u, bool t) {
for (int v = u; v; v = par[v])
if (t) minimize(ans[v], d[v][u]); else ans[v] = oo;
}
long long get(int u) {
long long res = oo;
for (int v = u; v; v = par[v])
minimize(res, ans[v] + d[v][u]);
return res;
}
long long Query(int S, int X[], int T, int Y[]) {
long long res = 1e18;
for (int i = 0; i < S; i++) upd(X[i], 1);
for (int i = 0; i < T; i++) minimize(res, get(Y[i]));
for (int i = 0; i < S; i++) upd(X[i], 0);
return res;
}
//int main() {
// int A[] = {0, 1, 2, 2, 4, 1};
// int B[] = {1, 2, 3, 4, 5, 6};
// int D[] = {4, 4, 5, 6, 5, 3};
// Init(7, A, B, D);
// int X[] = {2};
// int Y[] = {5};
// cout << Query(1, X, 1, Y);
//}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |