This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//bit chass 1
#include <bits/stdc++.h>
#define x first
#define y second
#define el "\n"
#define ll long long
#define pb push_back
#define pll pair <ll, ll>
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
#define lcm(x,y) x * y / __gcd(x, y)
#define ibase ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;
const int N = 1e5 + 5, inf = 1e9 + 7, M = 2e5, K = 300;
const ll MI = 2e18, mm = 1e15 + 5;
const double P = 3.14;
int n, s, q, e, p, tim = 1, tin[2 * N], tout[2 * N], up[N][20];
ll dist[N], dp[N], mn[N][20];
bool c[N];
vector <pair <int, int>> g[N];
pair <int, int> ed[N];
void dfs(int v, int pr = 0) {
tin[v] = tim++;
if (c[v])
dp[v] = 0;
up[v][0] = pr;
for (int i = 1; i < 20; i++)
up[v][i] = up[up[v][i - 1]][i - 1];
for (auto to : g[v]) {
if (to.x != pr) {
dist[to.x] = dist[v] + to.y;
dfs(to.x, v);
dp[v] = min(dp[to.x] + to.y, dp[v]);
}
}
tout[v] = tim++;
}
void dfs2(int v, int pr) {
mn[v][0] = dp[pr] - dist[pr];
for (int i = 1; i < 20; i++)
mn[v][i] = min(mn[up[v][i - 1]][i - 1], mn[v][i - 1]);
for (pii to : g[v])
if (to.x != pr)
dfs2(to.x, v);
}
bool upper(int x, int y) {
return (tin[x] <= tin[y] && tout[y] <= tout[x]);
}
void solve() {
cin >> n >> s >> q >> e;
for (int i = 1, x, y, w; i < n; i++) {
cin >> x >> y >> w;
g[x].pb({y, w});
g[y].pb({x, w});
ed[i] = {x, y};
dp[i] = dp[i + 1] = mm;
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < 20; j++)
mn[i][j] = mm;
for (int i = 1, x; i <= s; i++) {
cin >> x;
c[x] = 1;
}
dfs(e);
dfs2(e, 0);
tout[0] = tim;
while (q--) {
int i, r, v;
cin >> i >> r;
pii x = ed[i];
if (upper(x.x, x.y))
v = x.y;
else
v = x.x;
if (upper(v, r)) {
if (v == r) {
if (dp[v] == mm)
cout << "oo" << el;
else
cout << dp[v] << el;
continue;
}
ll ans = mm, rr = r;
for (int i = 19; i >= 0; i--) {
if (upper(v, up[r][i]))
ans = min(mn[r][i], ans), r = up[r][i];
}
if (ans + dist[rr] >= mm && dp[rr] == mm)
cout << "oo" << el;
else
cout << min(ans + dist[rr], dp[rr]) << el;
}
else {
cout << "escaped" << el;
}
}
}
int main() {
ibase;
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
// cout << "Case " << i << ": ";
solve();
cout << el;
}
}
# | 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... |