#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ff first
#define ss second
#define pb push_back
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
template<class T, class U> inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return true; } return false; }
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define rnd(l, r) uniform_int_distribution <int> (l, r)(rng)
const int inf = 1e18;
void solve(){
int n, s, q, e;
cin >> n >> s >> q >> e;
vector<vector<array<int,2>>> g(n+1);
vector<array<int,2>> edg(n);
for(int i = 0; i < n-1; i++){
int u, v, c;
cin >> u >> v >> c;
edg[i+1] = {u, v};
g[u].pb({v, c});
g[v].pb({u, c});
}
vector<int> dp(n+1, inf), dis(n+1, 0);
while(s--){
int x;cin >> x;
dp[x] = 0;
}
auto prec = [&](auto prec, int ps, int pr) -> void{
for(auto[to, c]:g[ps]){
if(to == pr)continue;
dis[to] = dis[ps] + c;
prec(prec, to, ps);
dp[ps] = min(dp[ps], dp[to] + c);
};
};
prec(prec, e,e);
vector<int> tin(n+1), tout(n+1);
vector<vector<int>> lc(22, vector<int> (n+1)), mn(22, vector<int> (n+1, inf));
int tim = 1;
auto dfs = [&](auto dfs, int ps, int pr) -> void{
tin[ps] = tim++;
lc[0][ps] = pr;
mn[0][ps] = dp[ps] - dis[ps];
for(int i = 1; i < 22; i++){
lc[i][ps] = lc[i-1][lc[i-1][ps]];
mn[i][ps] = min(mn[i-1][ps], mn[i-1][lc[i-1][ps]]);
}
for(auto [to, c]: g[ps]){
if(to == pr)continue;
dfs(dfs, to, ps);
}
tout[ps] = tim++;
};
dfs(dfs,e,e);
while(q--){
int id, ps;
cin >> id >> ps;
auto [a, b] = edg[id];
if(dis[a] < dis[b])swap(a, b);
if(tin[a] <= tin[ps] && tout[ps] <= tout[a]){
int ans = dp[ps], res = ps;
for(int i = 20; i >= 0; i--){
if(dis[lc[i][res]] >= dis[a]){
res = lc[i][res];
chmin(ans, mn[i][res] + dis[ps]);
}
}
if(ans == inf)cout << "oo\n";
else cout << ans << '\n';
}else cout << "escaped\n";
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int tt = 1;
//cin >> tt;
while(tt--){
solve();
};
}
# | 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... |