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 <bits/stdc++.h>
#include "factories.h"
using namespace std;
const int maxN = 2e5 + 10, LOG = 22;
vector<pair<int, long long>> adj[maxN];
int sz[maxN], up[maxN][LOG], depth[maxN], compSize, p[maxN];
long long dist[maxN], sub[maxN];
bool used[maxN];
void dfs_lca(int node, int parent) {
for(auto i: adj[node]) {
if(i.first == parent) continue;
up[i.first][0] = node;
for(int j = 1; j < LOG; j++) {
up[i.first][j] = up[up[i.first][j - 1]][j - 1];
}
dist[i.first] = dist[node] + i.second;
depth[i.first] = depth[node] + 1;
dfs_lca(i.first, node);
}
}
int lca(int a, int b) {
if(a == b) {
return a;
}
if(depth[a] < depth[b]) {
swap(a, b);
}
int delta = depth[a] - depth[b];
for(int i = 0; i < LOG; i++) {
if(delta & (1 << i)) {
a = up[a][i];
}
}
if(a == b) {
return a;
}
for(int i = LOG - 1; i >= 0; i--) {
if(up[a][i] != up[b][i]) {
a = up[a][i], b = up[b][i];
}
}
return up[a][0];
}
long long distance(int a, int b) {
return dist[a] + dist[b] - 2 * dist[lca(a, b)];
}
int dfs_sz(int node, int parent) {
sz[node] = 1, compSize++;
for(auto i: adj[node]) {
if(i.first == parent || used[i.first]) continue;
sz[node] += dfs_sz(i.first, node);
}
return sz[node];
}
int get_centroid(int node, int parent) {
for(auto i: adj[node]) {
if(i.first == parent || used[i.first]) continue;
if(2 * sz[i.first] > compSize) {
return get_centroid(i.first, node);
}
}
return node;
}
int find_centroid(int node, int parent) {
compSize = 0;
dfs_sz(node, 0);
int centroid = get_centroid(node, 0);
return centroid;
}
void centroid_decomposition() {
queue<pair<int, int>> q;
q.push({1, 0});
dfs_lca(1, 0);
for(int i = 0; i < maxN; i++) {
sub[i] = 1e15;
}
while(!q.empty()) {
auto u = q.front();
q.pop();
int centroid = find_centroid(u.first, 0);
if(u.second) {
p[centroid] = u.second;
}
used[centroid] = true;
for(auto i: adj[centroid]) {
if(used[i.first]) continue;
q.push({i.first, centroid});
}
}
}
void Init(int N, int A[], int B[], int D[]) {
for(int i = 0; i < N; i++) {
int a = A[i], b = B[i], c = D[i];
a++, b++;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
centroid_decomposition();
}
set<int> need_to_erase;
void upd(int node) {
int curr = node;
while(curr) {
sub[curr] = min(sub[curr], distance(node, curr));
need_to_erase.insert(curr);
curr = p[curr];
}
}
long long qry(int node) {
long long answ = sub[node], curr = node;
while(curr) {
answ = min(answ, sub[node] + distance(node, curr));
curr = p[curr];
}
return answ;
}
long long Query(int S, int X[], int T, int Y[]) {
long long answ = 1e15;
if(S < T) {
for(int i = 0; i < S; i++) {
upd(X[i] + 1);
}
for(int i = 0; i < T; i++) {
answ = min(answ, qry(Y[i] + 1));
}
} else {
for(int i = 0; i < T; i++) {
upd(Y[i] + 1);
}
for(int i = 0; i < S; i++) {
answ = min(answ, qry(X[i] + 1));
}
}
for(auto i: need_to_erase) {
sub[i] = 1e15;
} need_to_erase.clear();
return answ;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |