/* Author: goats_9 */
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const int LOG = 18;
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), in(n + 1), out(n + 1);
vector<vector<ll>> par(n+1, vector<ll> (LOG)), parlen(n+1, vector<ll> (LOG)), parcost(n+1, vector<ll> (LOG, 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;
}
int cnt = 0;
function<void(int, int)> dfs = [&] (int u, int p) {
in[u] = cnt++;
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);
}
out[u] = cnt++;
};
dfs(e, 0);
function<void(int, int, ll)> dfs2 = [&] (int u, int p, ll w) {
par[u][0] = p;
parlen[u][0] = w;
for (int i = 1; i < LOG; i++) {
par[u][i] = par[par[u][i - 1]][i - 1];
parlen[u][i] = parlen[u][i - 1] + parlen[par[u][i - 1]][i - 1];
}
parcost[u][0] = min(store[u], w + store[p]);
for (int i = 1; i < LOG; i++) {
parcost[u][i] = min(parcost[u][i - 1], parlen[u][i - 1] + parcost[par[u][i - 1]][i - 1]);
}
for (auto [v, w1] : adj[u]) {
if (v == p) continue;
dfs2(v, u, w1);
}
};
dfs2(e, 0, 0);
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 = LOG - 1; 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
161 ms |
67924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |