#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}
using namespace std;
const int MAXN = 1e5 + 5, infINT = 1e9 + 389;
const long long inf = 1e18 + 3818;
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, chain[MAXN], h[MAXN], sta[MAXN], fin[MAXN], sz[MAXN], par[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;
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 dfsInit(int u){
sz[u] = 1;
for(int id: adj[u]){
int v = e[id].other(u);
if (v == par[u]) continue;
par[v] = u; h[v] = h[u] + 1;
dist[v] = dist[u] + e[id].w;
dfsInit(v);
sz[u] += sz[v];
}
sort(all(adj[u]), [&](const int &a, const int &b){
return (a == par[u] ? INT_MIN: sz[a]) > (b == par[u] ? INT_MIN: sz[b]);
});
}
void dfsMark(int u, bool isHeavy = 0){
sta[u] = ++sta[0];
chain[u] = (isHeavy ? chain[par[u]]: u);
if (shop[u]) best[u] = 0;
for(int id: adj[u]){
int v = e[id].other(u);
if (v == par[u]) continue;
dfsMark(v, v == e[adj[u][0]].other(u));
minimize(best[u], best[v] + e[id].w);
}
fin[u] = sta[0];
}
bool check(int R, int u){
return sta[u] <= sta[R] && sta[R] <= fin[u];
}
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;
}
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(){
for(int i = 1; i <= numNode; i++) best[i] = inf;
dfsInit(root);
dfsMark(root);
myIt = segmentTree(numNode);
for(int i = 1; i <= numNode; i++){
if (dist[i] < inf && best[i] < inf) myIt.update(sta[i], best[i] - dist[i]);
}
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 x = getHLD(R, u);
long long res = dist[R] + x;
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:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | 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... |