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;
#define int long long
#define oo 1e18
#define N 100005
#define log 20
#define fi first
#define se second
typedef pair<int, int> ii;
int n, q, s, e, dep[N], ds[N], par[log + 5][N], getdp[log + 5][N], dp[N], tin[N], tout[N], tim;
vector<ii> adj[N];
ii eg[N];
void init(int u, int p){
tin[u] = ++tim;
for (auto x : adj[u]){
if (x.fi == p) continue;
dep[x.fi] = dep[u] + 1;
ds[x.fi] = ds[u] + x.se;
par[0][x.fi] = u;
init(x.fi, u);
dp[u] = min(dp[u], dp[x.fi] + x.se);
}
tout[u] = tim;
}
void init2(int u, int p){
for (auto x : adj[u]){
if (x.fi == p) continue;
getdp[0][x.fi] = dp[u] - ds[u];
init2(x.fi, u);
}
}
bool in(int u, int v){
return (tin[v] >= tin[u] && tout[v] <= tout[u]);
}
int get(int u, int len){
int rt = oo;
for (int k = log; k >= 0; k--){
if ((1ll << k) <= len){
len -= (1ll << k);
rt = min(rt, getdp[k][u]);
u = par[k][u];
}
}
return rt;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> s >> q >> e;
dp[n] = oo;
for (int i = 1; i <= n - 1; i++){
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
dp[i] = oo;
eg[i] = {u, v};
}
for (int i = 1; i <= s; i++){
int u;
cin >> u;
dp[u] = 0;
}
dep[e] = 1;
init(e, 0);
init2(e, 0);
for (int k = 1; k <= log; k++){
for (int i = 1; i <= n; i++){
par[k][i] = par[k - 1][par[k - 1][i]];
getdp[k][i] = min(getdp[k - 1][i], getdp[k - 1][par[k - 1][i]]);
}
}
while (q--){
int idx, x;
cin >> idx >> x;
int u = eg[idx].fi, v = eg[idx].se;
if (par[0][v] != u) swap(u, v);
if (!in(v, x)){
cout << "escaped\n";
continue;
}
if (dp[v] == oo){
cout << "oo\n";
continue;
}
cout << min(dp[x], ds[x] + get(x, dep[x] - dep[v])) << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |