// Alphine walley
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define REP(i, k) for(int i = 0 ; i < k ; i ++)
#define pb push_back
#define pii pair<int, int>
#define ll long long
const int N = 1e5+10;
int n, s, q, e;
vector<pii> adj[N];
bool shop[N];
array<int, 3> E[N];
int dis[N], st[N], fn[N];
int timer;
int h[N];
void dfs(int v = 0, int p = -1) {
st[v] = timer++;
h[v] = (p == -1 ? 0 : h[p] + 1);
for(auto [neigh, w]: adj[v]) {
if (neigh != p) {
dfs(neigh, v);
}
}
fn[v] = timer;
}
bool zird(int v, int u) {
return st[u] <= st[v] && fn[v] <= fn[u];
}
int32_t main() {
cin >> n >> s >> q >> e; e--;
REP(i, n-1) {
int a, b, w; cin >> a >> b >> w; a--, b--;
E[i] = {a, b, w};
adj[a].pb({b, w}), adj[b].pb({a, w});
}
REP(i, s) {
int c; cin >> c; c--;
shop[c] = true;
}
dfs();
while(q--) {
int a, b; cin >> a >> b; a--; b--;
auto [x, y, _] = E[a];
if (h[x] > h[y]) swap(x, y);
if (zird(b, y) == zird(e, y)) {
cout << "escaped\n";
continue;
}
if (s == n) {
cout << "0\n";
continue;
}
queue<int> q;
q.push(b);
fill(dis, dis+n, 1e18);
dis[b] = 0;
bool escape = false;
int ans = 1e18;
while(!q.empty()) {
int u = q.front();
q.pop();
if (u == e) {
escape = true;
break;
}
if (shop[u]) ans = min(ans, dis[u]);
for(auto [neigh, w]: adj[u]) {
if ((u == E[a][0] && neigh == E[a][1]) || (u == E[a][1] && neigh == E[a][0])) continue;
if (dis[u] + w < dis[neigh]) {
dis[neigh] = dis[u] + w;
q.push(neigh);
}
}
}
if (ans == 1e18) cout << "oo\n";
else cout << ans << '\n';
}
return 0;
}
# | 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... |