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;
const int N = 1e5 + 17;
int n, s, q, e, lg;
int ti, in[N], out[N], p[17][N];
long long d[N], f[N], m[17][N], shop[N];
vector <pair <int, int>> adj[N];
pair <int, int> ed[N];
void dfs (int u, int pr)
{
in[u] = ++ti, f[u] = shop[u];
for (auto [v, w]: adj[u])
{
if (v == pr)
{
continue;
}
d[v] = d[u] + w;
dfs (v, u);
f[u] = min (f[u], f[v] + w);
}
out[u] = ti;
}
void dfz (int u, int pr)
{
p[0][u] = pr, m[0][u] = f[u] - d[u];
for (int i = 1; i <= lg; ++i)
{
p[i][u] = p[i - 1][p[i - 1][u]];
m[i][u] = min (m[i - 1][u], m[i - 1][p[i - 1][u]]);
}
for (auto [v, w]: adj[u])
{
if (v == pr)
{
continue;
}
dfz (v, u);
}
}
bool check (int u, int v)
{
return in[u] <= in[v] && in[v] <= out[u];
}
long long calc (int u, int v)
{
if (u == v)
{
return m[0][u];
}
long long res = 1e18;
for (int i = lg; i >= 0; --i)
{
if (p[i][u] && check (v, p[i][u]))
{
res = min (res, m[i][u]);
u = p[i][u];
}
}
return min (res, m[0][u]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> s >> q >> e;
lg = __lg(n);
for (int i = 1, u, v, w; i < n; ++i)
{
cin >> u >> v >> w;
adj[u].push_back ({v, w});
adj[v].push_back ({u, w});
shop[i] = 1e18, ed[i] = {u, v};
}
shop[n] = 1e18;
for (int i = 1, x; i <= s; ++i)
{
cin >> x;
shop[x] = 0;
}
dfs (e, 0);
dfz (e, 0);
while (q--)
{
int id, x;
cin >> id >> x;
auto [u, v] = ed[id];
int child = (check (u, v) ? v : u);
if (!check (child, x))
{
cout << "escaped\n";
continue;
}
long long ans = calc (x, child) + d[x];
if (ans > 1e14)
{
cout << "oo\n";
continue;
}
cout << ans << "\n";
}
}
# | 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... |