#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)
#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define eb emplace_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define SZ(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem(a,b) memset(a, b, sizeof(a))
const long long INF = 1e16;
struct edge {
int node, weight;
edge(int _node, int _weight) : node(_node), weight(_weight) {}
};
struct centroid_decomposition {
int N;
vector<vector<edge>> adj;
vector<int> depth;
vector<int> subtree_size;
// parent of a node in centroid tree.
vector<int> centroid_parent;
vector<int> node_list;
// gives the distance of each node to its descendants in centroid tree.
vector<vector<long long>> dis; // from node to its ancestors.
bool found_centroid;
void init(int _N) {
N = _N;
adj.assign(N, {});
depth.resize(N);
subtree_size.resize(N);
centroid_parent.assign(N, -1);
dis.resize(N,{});
}
void add_edge(int u, int v, int w) {
assert(u != v);
adj[u].emplace_back(edge(v,w));
adj[v].emplace_back(edge(u,w));
}
// Erasing edges is O(number of nodes remaining) which is fine within our decomposition.
void erase_edge(int from, int to) {
for(edge &e : adj[from]) {
if(e.node == to) {
swap(e, adj[from].back());
adj[from].pop_back();
return;
}
}
assert(false);
}
int dfs(int node, long long weight = 0, int parent = -1, int root = -1) {
if(parent < 0) {
root = node;
node_list.clear();
}
if(found_centroid){
dis[node].pb(weight);
}
subtree_size[node] = 1;
node_list.push_back(node);
for(edge &e : adj[node]) {
if(e.node != parent) {
subtree_size[node] += dfs(e.node, e.weight + weight, node, parent < 0 ? node : root);
}
}
return subtree_size[node];
}
int centroid(int root) {
int n = dfs(root);
bool found;
// Repeatedly move to the subtree that is at least half of the tree, if such a subtree exists.
do {
found = false;
for(edge &e : adj[root]){
if(subtree_size[e.node] < subtree_size[root] && 2 * subtree_size[e.node] >= n) {
root = e.node;
found = true;
break;
}
}
} while(found);
return root;
}
// centroid parent of root of centroid tree is -1
void solve(int root) {
found_centroid = false;
root = centroid(root);
found_centroid = true;
dfs(root);
for(int node : node_list){
if(node != root){
centroid_parent[node] = root;
}
}
for(edge &e : adj[root]){
erase_edge(e.node, root);
}
// Recurse after solving root, so that edge erasures don't cause incorrect results.
for(edge &e : adj[root]){
solve(e.node);
}
}
}cd;
vt<long long> ans;
void turn_on(int _i){
for(int i = _i, cnt = 0; ~i; i = cd.centroid_parent[i], ++cnt){
ckmin(ans[i],cd.dis[_i][cnt]);
}
}
void turn_off(int _i){
for(int i = _i; ~i; i = cd.centroid_parent[i]){
ans[i] = INF;
}
}
void Init(int N, int A[], int B[], int D[]) {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cd.init(N);
ans.assign(N,INF);
f(e,0,N-1){
cd.add_edge(A[e],B[e],D[e]);
}
cd.solve(0);
f(i,0,N) reverse(all(cd.dis[i]));
return;
}
long long Query(int S, int X[], int T, int Y[]) {
f(i,0,S) turn_on(X[i]);
long long res = INF;
f(i,0,T){
for(int j = Y[i], cnt = 0; ~j; j = cd.centroid_parent[j], ++cnt){
ckmin(res, cd.dis[Y[i]][cnt] + ans[j]);
}
}
f(i,0,S) turn_off(X[i]);
return res;
}
Compilation message
factories.cpp: In function 'void turn_on(int)':
factories.cpp:176:3: error: 'ckmin' was not declared in this scope
176 | ckmin(ans[i],cd.dis[_i][cnt]);
| ^~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:221:4: error: 'ckmin' was not declared in this scope
221 | ckmin(res, cd.dis[Y[i]][cnt] + ans[j]);
| ^~~~~