#include <iostream>
#include <map>
#include <vector>
#define pb push_back
#define fi first
#define se second
typedef long long ll;
using namespace std;
const int N = 1e5 + 5;
const ll INF = 1e18;
int n, s, q, e;
pair<int, int> edge[N];
vector<int> g[N];
map<int, bool> shop;
map<pair<int, int>, ll> cost;
bool is_esc[N];
int in[N], out[N], clk, par[N];
ll dist[N];
void dfs(int v, int p = -1) {
in[v] = ++clk;
par[v] = p;
for (int to : g[v]) {
if (to != p) {
dist[to] = dist[v] + max(cost[{v, to}], cost[{to, v}]);
dfs(to, v);
}
}
out[v] = ++clk;
}
bool subtree_of(int a, int b) {
if (in[a] <= in[b] && out[b] <= out[a]) return 1;
return 0;
}
ll magic[N];
void buildMagic(int v) {
for (int to : g[v]) {
if (to == par[v]) continue;
buildMagic(to);
}
if (shop[v] == 1) {
magic[v] = dist[v];
}
else
magic[v] = INF;
for (int to : g[v]) {
if (to == par[v]) continue;
magic[v] = min(magic[v], magic[to]);
}
}
void solve() {
cin >> n >> s >> q >> e;
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
cost[{a, b}] = c;
g[a].pb(b);
g[b].pb(a);
edge[i] = {a, b};
}
while (s--) {
int c;
cin >> c;
shop[c] = 1;
}
dfs(e);
buildMagic(e);
for (int i = 1; i <= n; i++) {
magic[i] -= 2 * 1ll * dist[i];
}
while (q--) {
int i, r;
cin >> i >> r;
int a, b;
a = edge[i].fi;
b = edge[i].se;
if (par[a] == b) swap(a, b); // a -> b (in tree)
//cout << "a is " << a << ", b is " << b << '\n';
if (subtree_of(e, r) && !subtree_of(b, r)) {
cout << "escaped\n";
continue;
}
int R = r;
ll ans = INF;
while (R != a) {
ans = min(ans, magic[R]);
R = par[R];
}
if (ans < 1e17) cout << dist[r] + ans << '\n';
else cout << "00\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
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... |