# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
983119 | vjudge1 | Cyberland (APIO23_cyberland) | C++17 | 0 ms | 0 KiB |
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>
using namespace std;
#include <vector>
#define int long long
const int NMAX = 1e5 + 5;
vector<pair<int,int> > g[NMAX];
vector<int> dis(NMAX);
vector<vector<int> > up(20, vector<int>(NMAX));
vector<int> ary(NMAX), tin(NMAX), tout(NMAX);
int ans, goal, timer;
void precalc(int v, int p){
tin[v] = ++timer;
up[0][v] = p;
for(int i = 1; i < 20; i++){
up[i][v] = up[i - 1][up[i - 1][v]];
}
for(auto [to, cost] : g[v]){
if(to != p){
dis[to] = dis[v] + cost;
precalc(to, v);
}
}
tout[v] = ++timer;
}
bool upper (int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca (int a, int b) {
if (upper (a, b)) return a;
if (upper (b, a)) return b;
for (int i=19; i>=0; --i)
if (! upper (up[a][i], b))
a = up[a][i];
return up[a][0];
}
int dist(int a, int b){
return dis[a] + dis[b] - dis[lca(a, b)] * 2;
}
void dfs(int v, int p){
if(v == goal) return;
if(ary[v] == 0){
ans = min(ans, dist(v, goal));
}
for(auto [to, cost] : g[v]){
if(to != p) dfs(to, v);
}
}
double solve(int n, int m, int k, int h, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr) {
for(int i = 1; i <= n; i++){
g[i].clear();
dis[i] = 0;
for(int j = 0; j < 20; j++) up[j][i] = 0;
tin[i] = 0;
tout[i] = 0;
timer = 0;
}
goal = h;
for(int i = 0; i < m; i++){
g[x[i]].push_back({y[i], c[i]});
g[y[i]].push_back({x[i], c[i]});
}
for(int i = 0; i < n; i++){
ary[i + 1] = arr[i];
}
precalc(1, 1);
ans = dist(1, goal);
dfs(1, -1);
return ans;
}