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;
#define ll long long
#define N 200005
int in[N], out[N], timer, P[20][N], dep[N];
vector<tuple<int, int, int>> edge;
vector<pair<int, int>> g[N];
ll dp[N], dp2[N], O[20][N];
void dfs(int now, int par)
{
P[0][now] = par; dep[now] = dep[par]+1;
in[now] = timer++;
for (auto [w, v] : g[now]) if (v != par) dp2[v] = dp2[now] + w, dfs(v, now), dp[now] = min(dp[now], w + dp[v]);
out[now] = timer++;
O[0][now] = dp[now] - dp2[now];
// printf("O %d: %lld dp: %lld dp2: %lld\n", now, O[0][now], dp[now], dp2[now]);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,s,q,e; cin >> n >> s >> q >> e;
memset(dp, 63, sizeof dp), memset(O, 63, sizeof O);
edge.resize(n-1);
for (auto &[u, v, w] : edge) cin >> u >> v >> w, g[u].emplace_back(w, v), g[v].emplace_back(w, u);
int c;
while(s--) cin >> c, dp[c] = 0;
dfs(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]);
// for (int j = 0; j < 20; ++j){
// for (int u = 1; u <= n; ++u) cout << O[j][u] << " ";
// cout << '\n';
// }
// for(int i=1;i<=n;i++) cout << in[i] << " " << out[i] << '\n';
for (int i, u; q--;)
{
cin >> i >> u;
int v = get<0>(edge[i-1]) == u ? get<1>(edge[i-1]) : get<0>(edge[i-1]), v2 = get<0>(edge[i-1]) == v ? get<1>(edge[i-1]) : get<0>(edge[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";
}
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... |