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 "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int LOG = 18;
const int N = 1e5 + 5;
const long long INF = 1e15;
int dep[N];
bool shop[N];
int pr[LOG][N];
vector<int> g[N];
long long dis[N];
array<int, 3> e[N];
long long dp[LOG][N];
int cnt, in[N], out[N];
void dfs(int v, int p) {
in[v] = cnt++;
dp[0][v] = (shop[v] ? dis[v] : INF);
for (int i : g[v]) {
int u = v ^ e[i][0] ^ e[i][1];
if (u == p) continue;
dep[u] = dep[v] + 1;
dis[u] = dis[v] + e[i][2];
pr[0][u] = v;
for (int j = 1; j < LOG; ++j) {
pr[j][u] = pr[j - 1][pr[j - 1][u]];
}
dfs(u, v);
dp[0][v] = min(dp[0][v], dp[0][u]);
}
out[v] = cnt - 1;
}
void init_dp(int v, int p) {
for (int i : g[v]) {
int u = v ^ e[i][0] ^ e[i][1];
if (u == p) continue;
for (int j = 1; j < LOG; ++j) {
dp[j][u] = min(dp[j - 1][u], dp[j - 1][pr[j - 1][u]]);
}
init_dp(u, v);
}
}
bool isParent(int v, int u) {
return in[v] <= in[u] && out[v] >= out[u];
}
long long get(int u, int v) {
long long ret = INF;
long long cc = dis[u];
//deb(u) deb(v) deb(dp[0][v]) cout << '\n';
for (int j = 0; j < LOG; ++j) {
if ((dep[u] - dep[v]) >> j & 1) {
ret = min(ret, dp[j][u] + cc);
u = pr[j][u];
}
}
assert(u == v);
ret = min(ret, dp[0][u] + cc);
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, s, q, ep;
cin >> n >> s >> q >> ep;
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
e[i] = {u, v, w};
g[u].push_back(i);
g[v].push_back(i);
}
for (int i = 0; i < s; ++i) {
int v;
cin >> v;
shop[v] = true;
}
dfs(ep, ep);
for (int i = 1; i <= n; ++i) {
if (dp[0][i] != INF) {
dp[0][i] -= 2 * dis[i];
}
}
init_dp(ep, ep);
while (q--) {
int i, v;
cin >> i >> v;
--i;
int qu = e[i][0];
int qv = e[i][1];
if (dis[qv] > dis[qu]) {
swap(qv, qu);
}
if (isParent(qu, v)) {
long long ans = get(v, qu);
if (ans < INF) {
cout << ans << '\n';
} else {
cout << "oo" << '\n';
}
} else {
cout << "escaped" << '\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... |