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;
typedef long long ll;
const int maxn = 1e5 + 10;
const ll inf = 1e18;
vector<pair<int, int> > t[maxn];
int w[maxn];
int dw[maxn], h[maxn];
int Time, st[maxn], ft[maxn];
int par[maxn][18];
ll dis[maxn], dp[maxn], RMQ[maxn][18];
ll get(int v, int x){
ll ret = inf;
for (int i = 17; i >= 0; i--)
if (x & (1 << i)){
ret = min(ret, RMQ[v][i]);
v = par[v][i];
}
return ret;
}
void DFS(int v, int p = -1){
par[v][0] = p;
RMQ[v][0] = dp[v] - 2ll * dis[v];
for (int i = 1; i < 18 and par[v][i - 1] != -1; i++){
par[v][i] = par[par[v][i - 1]][i - 1];
RMQ[v][i] = min(RMQ[v][i - 1], RMQ[par[v][i - 1]][i - 1]);
}
for (auto u : t[v])
if (u.first != p)
DFS(u.first, v);
}
void dfs(int v, int p = -1){
st[v] = Time ++;
if (dp[v] == 0)
dp[v] = dis[v];
for (auto edge : t[v]){
int u = edge.first, e = edge.second;
if (u != p){
dw[e] = u;
dis[u] = dis[v] + w[e];
h[u] = h[v] + 1;
dfs(u, v);
dp[v] = min(dp[v], dp[u]);
}
}
ft[v] = Time;
}
int main(){
ios_base::sync_with_stdio(false);
int n, s, q, e;
cin >> n >> s >> q >> e;
for (int i = 1; i <= n - 1; i++){
int v, u;
cin >> v >> u >> w[i];
t[v].push_back({u, i});
t[u].push_back({v, i});
}
memset(dp, 63, sizeof dp);
for (int i = 1; i <= s; i++){
int c;
cin >> c;
dp[c] = 0;
}
memset(par, -1, sizeof par);
dfs(e);
DFS(e);
while (q --){
int e, v;
cin >> e >> v;
int u = dw[e];
if (st[u] <= st[v] and ft[v] <= ft[u]){
if (dp[u] > inf)
cout << "oo\n";
else
cout << dis[v] + get(v, h[v] - h[u] + 1) << '\n';
}
else
cout << "escaped\n";
}
}
# | 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... |