This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author: goats_9 */
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e15;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, s, q, e;
cin >> n >> s >> q >> e;
e--;
vector<array<ll, 3>> edges(n);
vector<ll> store(n, INF), depth(n);
vector<vector<ll>> par(n, vector<ll> (20, -1)), parlen(n, vector<ll> (20, INF)), parcost(n, vector<ll> (20, INF));
vector<vector<array<int, 2>>> adj(n, vector<array<int, 2>>());
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
edges[i] = {u, v, w};
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
while (s--) {
int x;
cin >> x;
x--;
store[x] = 0;
}
function<void(int, int)> dfs = [&] (int u, int p) {
for (auto [v, w] : adj[u]) {
if (v == p) continue;
depth[v] = depth[u] + 1;
dfs(v, u);
store[u] = min(store[u], store[v] + w);
}
for (auto [v, w] : adj[u]) {
if (v == p) continue;
par[v][0] = u;
parlen[v][0] = w;
parcost[v][0] = min(INF, w + store[u]);
}
};
dfs(e, -1);
for (int j = 1; j < 20; j++) {
for (int i = 0; i < n; i++) {
int u = par[i][j - 1];
if (u == -1) continue;
par[i][j] = par[u][j - 1];
parlen[i][j] = parlen[i][j - 1] + parlen[u][j - 1];
parcost[i][j] = min(parcost[i][j - 1], parlen[i][j - 1] + parcost[u][j - 1]);
}
}
while (q--) {
int i, r;
cin >> i >> r;
r--;
auto [u, v, w] = edges[i];
if (depth[u] > depth[v]) swap(u, v);
int d = depth[r] - depth[v];
if (d < 0) {
cout << "escaped\n";
continue;
}
if (d == 0) {
if (r != v) cout << "escaped\n";
else {
if (store[r] >= INF) cout << "oo\n";
else cout << store[r] << '\n';
}
continue;
}
ll ans = store[r];
ll psm = 0;
int cu = r;
for (int j = 0; j < 20; j++) {
if (d&(1<<j)) {
ans = min(ans, psm + parcost[cu][j]);
psm += parlen[cu][j];
cu = par[cu][j];
}
}
if (ans >= INF) 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... |