#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)
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> stor(s);
for(int &i:stor)cin >> i;
vector<int> tin(n+1), tout(n+1), dep(n+1), dis(n+1), nm(n+1, 0);
vector<vector<int>> lc(22, vector<int> (n+1));
int tim = 1;
auto dfs = [&](auto dfs, int ps, int pr) -> void{
tin[ps] = tim++;
lc[0][ps] = pr;
for(int i = 1; i < 22; i++){
lc[i][ps] = lc[i-1][lc[i-1][ps]];
}
for(auto [to, c]: g[ps]){
if(to == pr)continue;
dis[to] = dis[ps] + c;
dep[to] = dep[ps] + 1;
dfs(dfs, to, ps);
nm[ps] += nm[to];
}
tout[ps] = tim++;
};
vector<int> isg(n+1, 0);
for(int i:stor){
nm[i]++;
isg[i] = 1;
}
dfs(dfs,1,1);
while(q--){
int id, ps;
cin >> id >> ps;
auto [a, b] = edg[id];
if(dep[a] < dep[b])swap(a, b);
if(tin[a] <= tin[ps] && tout[ps] <= tout[a]){
if(tin[a] <= tin[e] && tout[e] <= tout[a]){
cout << "escaped\n";
continue;
}
if(nm[a] == 0){
cout << "oo\n";
continue;
}
}else{
if(!(tin[a] <= tin[e] && tout[e] <= tout[a])){
cout << "escaped\n";
continue;
}
if(s - nm[a] == 0){
cout << "oo\n";
continue;
}
}
if(isg[ps]){
cout << 0 << '\n';
continue;
}
cout << dis[ps] << '\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... |