#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
using namespace std;
template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
const int MAXN = 1e5 + 5;
const long long inf = 1e18 + 3;
struct edge{
int u, v, w;
edge(int _u = 0, int _v = 0, int _w = 0): u(_u), v(_v), w(_w) {};
int other(int x){
return u ^ v ^ x;
}
};
int numNode, numShop, numQuery, root, h[MAXN], chain[MAXN], par[MAXN], sz[MAXN], sta[MAXN], fin[MAXN];
long long dist[MAXN], best[MAXN];
bool shop[MAXN];
edge e[MAXN];
vector<int> adj[MAXN];
struct segmentTree{
int n;
vector<long long> mi;
segmentTree(int _n = 0): n(_n){
mi.assign(n << 1 | 1, inf);
}
void update(int pos, long long x){
pos--;
pos += n;
minimize(mi[pos], x);
while(pos > 1){
pos >>= 1;
minimize(mi[pos], mi[pos << 1]);
minimize(mi[pos], mi[pos << 1 | 1]);
}
}
long long get(int L, int R){
L--;
L += n; R += n;
long long res = inf;
while(L < R){
if (L & 1) minimize(res, mi[L++]);
if (R & 1) minimize(res, mi[--R]);
L >>= 1; R >>= 1;
}
return res;
}
} myIt;
void dfsLeaf(int u){
sz[u] = 1;
for(int id: adj[u]) if (e[id].other(u) != par[u]){
int v = e[id].other(u);
h[v] = h[u] + 1; dist[v] = dist[u] + e[id].w;
par[v] = u;
dfsLeaf(v);
sz[u] += sz[v];
}
sort(all(adj[u]), [&](const int &idA, const int &idB){
int a = e[idA].other(u);
int b = e[idB].other(u);
return (a == par[u] ? INT_MIN: sz[a]) > (b == par[u] ? INT_MIN: sz[b]);
});
}
void dfsRoot(int u, bool isHeavy = 0){
chain[u] = (isHeavy ? chain[par[u]]: u);
sta[u] = ++sta[0];
if (shop[u]) best[u] = 0;
for(int id: adj[u]) if (e[id].other(u) != par[u]) {
int v = e[id].other(u);
dfsRoot(v, id == adj[u][0]);
minimize(best[u], best[v] + e[id].w);
}
fin[u] = sta[0];
}
long long getHLD(int u, int p){
long long res = inf;
while(chain[u] != chain[p]){
minimize(res, myIt.get(sta[chain[u]], sta[u]));
u = par[chain[u]];
}
minimize(res, myIt.get(sta[p], sta[u]));
return res;
}
bool check(int R, int u){
return sta[u] <= sta[R] && sta[R] <= fin[u];
}
void input(){
cin >> numNode >> numShop >> numQuery >> root;
int u, v, w;
for(int i = 1; i < numNode; i++){
cin >> u >> v >> w;
adj[u].push_back(i);
adj[v].push_back(i);
e[i] = edge(u, v, w);
}
for(int i = 1; i <= numShop; i++){
int x; cin >> x;
shop[x] = 1;
}
}
void solve(){
memset(best, 0x3f, sizeof best);
dfsLeaf(root);
dfsRoot(root);
myIt = segmentTree(numNode);
for(int u = 1; u <= numNode; u++) myIt.update(sta[u], best[u] - dist[u]);
while(numQuery--){
int id, R; cin >> id >> R;
int u = e[id].u, v = e[id].v;
if (h[u] < h[v]) swap(u, v);
if (!check(R, u)) cout << "escaped\n";
else{
long long res = dist[R] + getHLD(R, u);
if (res < inf) cout << res << '\n';
else cout << "oo\n";
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("test.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
input();
solve();
}
Compilation message (stderr)
valley.cpp: In function 'int main()':
valley.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |