This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll = long long;
using ld = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)
#define N 200005
#define ALL(x) x.begin(), x.end()
int n, s, e, q, in[N], out[N], timer, P[20][N], dep[N];
vector<tuple<int, int, int>> el;
vector<pair<int, int>> g[N];
ll dp[N], dp2[N], O[20][N];
void dfs0(int u, int p)
{
P[0][u] = p; dep[u] = dep[p]+1;
in[u] = timer++;
for (auto [w, v] : g[u]) if (v != p) dp2[v] = dp2[u] + w, dfs0(v, u), dp[u] = min(dp[u], w + dp[v]);
out[u] = timer++;
O[0][u] = dp[u] - dp2[u];
}
void init()
{
ShinLena;
cin >> n >> s >> q >> e;
memset(dp, 63, sizeof dp), memset(O, 63, sizeof O);
el.resize(n-1);
for (auto &[u, v, w] : el) cin >> u >> v >> w, g[u].emplace_back(w, v), g[v].emplace_back(w, u);
for (int i = 0, c; i < s; ++i) cin >> c, dp[c] = 0;
dfs0(e, e);
for (int j = 1; j < 20; ++j)
for (int u = 1; u <= n; ++u) P[j][u] = P[j-1][P[j-1][u]], O[j][u] = min(O[j-1][P[j-1][u]], O[j-1][u]);
}
void answer()
{
for (int i, u; q--;)
{
cin >> i >> u;
int v = get<0>(el[i-1]) == u ? get<1>(el[i-1]) : get<0>(el[i-1]), v2 = get<0>(el[i-1]) == v ? get<1>(el[i-1]) : get<0>(el[i-1]);
if (P[0][v2] == v) swap(v, v2);
if (in[v] <= in[u] && out[u] <= out[v])
{
ll z = min(dp[u], dp[v] - dp2[v] + dp2[u]), u0 = u, dt = dep[u]-dep[v];
for (int j = 20; j--;) if (dt & (1 << j)) z = min(z, dp2[u0] + O[j][u]), u = P[j][u];
(z < 1e15 ? (cout << z << '\n') : cout << "oo\n");
}
else cout << "escaped\n";
}
}
signed main()
{
init();
answer();
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... |