This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
* Written by
* ______ _ _ ______ ____ ____ ____ ____
* |_ _|| |_| ||_ _||_ || __ || __ |/ _/
* | | | _ | | | / / || |||| ||| |__
* | | | | | | | | | |_ ||__||||__||\__ \
* |__| |_| |_| |__| |___||____||____|/____/
*/
//#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define nmax 500050
#define ll long long
#define ii pair<int, int>
const ll inf = (ll)1e15;
int n, sz[nmax], p[nmax], r[nmax], parent[nmax][25], depth[nmax];
ll ans[nmax], sum[nmax];
vector<ii> adj[nmax];
int dfs(int u, int p = -1)
{
sz[u] = 1;
for(auto [C, v]: adj[u]) {
if(v != p && !r[v]) sz[u] += dfs(v, u);
}
return sz[u];
}
int find_centroid(int m, int u, int p = -1)
{
for(auto [C, v]: adj[u]) {
if(v != p && !r[v] && 2 * sz[v] > m)
return find_centroid(m, v, u);
}
return u;
}
int centroid(int u = 0)
{
u = find_centroid(dfs(u), u);
r[u] = 1;
p[u] = -1;
for(auto [C, v]: adj[u]) {
if(!r[v]) p[centroid(v)] = u;
}
return u;
}
void dfs2(int u, int p = -1)
{
for(int j = 1; j <= 19; ++j) {
parent[u][j] = parent[u][j - 1] != -1 ? parent[parent[u][j - 1]][j - 1] : -1;
}
for(auto [C, v]: adj[u]) {
if(v == p) continue;
parent[v][0] = u;
depth[v] = depth[u] + 1;
sum[v] = sum[u] + C;
dfs2(v, u);
}
}
int lca(int x, int y)
{
if(depth[x] > depth[y])
swap(x, y);
for(int j = 19; j >= 0; --j) {
if(parent[y][j] != -1 && depth[x] <= depth[parent[y][j]])
y = parent[y][j];
}
if(x == y)
return x;
for(int j = 19; j >= 0; --j) {
if(parent[x][j] != parent[y][j])
x = parent[x][j], y = parent[y][j];
}
return parent[x][0];
}
inline ll dist(int x, int y)
{
return sum[x] + sum[y] - 2 * sum[lca(x, y)];
}
void Init(int N, int A[], int B[], int D[])
{
n = N;
for(int i = 0; i < n - 1; ++i) {
adj[A[i]].push_back(ii(D[i], B[i]));
adj[B[i]].push_back(ii(D[i], A[i]));
}
centroid();
fill(ans, ans + n, inf);
sum[0] = depth[0] = 0;
parent[0][0] = -1;
dfs2(0);
}
void update(int y, bool del)
{
for(int x = y; x != -1; x = p[x]) {
if(del) ans[x] = inf;
else ans[x] = min(ans[x], dist(x, y));
}
}
ll get(int y)
{
ll res = inf;
for(int x = y; x != -1; x = p[x])
res = min(res, ans[x] + dist(x, y));
return res;
}
ll Query(int S, int X[], int T, int Y[])
{
ll res = inf;
if(S <= 10 && T <= 10) {
for(int i = 0; i < S; ++i) {
for(int j = 0; j < T; ++j)
res = min(res, dist(X[i], Y[j]));
}
return res;
}
for(int i = 0; i < S; ++i) {
update(X[i], 0);
}
for(int i = 0; i < T; ++i) {
res = min(res, get(Y[i]));
}
for(int i = 0; i < S; ++i) {
update(X[i], 1);
}
return res;
}
Compilation message (stderr)
factories.cpp: In function 'int dfs(int, int)':
factories.cpp:26:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | for(auto [C, v]: adj[u]) {
| ^
factories.cpp: In function 'int find_centroid(int, int, int)':
factories.cpp:34:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto [C, v]: adj[u]) {
| ^
factories.cpp: In function 'int centroid(int)':
factories.cpp:46:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for(auto [C, v]: adj[u]) {
| ^
factories.cpp: In function 'void dfs2(int, int)':
factories.cpp:57:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | for(auto [C, v]: adj[u]) {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |