이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* Author: goats_9 */
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, s, q, e;
cin >> n >> s >> q >> e;
vector<array<ll, 3>> edges(n);
vector<ll> store(n+1, INF), depth(n+1);
vector<vector<ll>> par(n+1, vector<ll> (20)), parlen(n+1, vector<ll> (20)), parcost(n+1, vector<ll> (20, INF));
vector<vector<array<ll, 2>>> adj(n+1, vector<array<ll, 2>>());
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
edges[i] = {u, v, w};
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
while (s--) {
int x;
cin >> 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(store[v], w + store[u]);
}
};
dfs(e, 0);
for (int j = 1; j < 20; j++) {
for (int i = 1; i <= n; i++) {
int u = par[i][j - 1];
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;
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 = 19; j >= 0; 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... |