#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
const int MXN = 100 * 1000 + 3;
const int LGN = 19;
const int inf = 1e17;
vector<array<int, 2>> g[MXN];
int up[MXN][LGN], dp[MXN][LGN], dt[MXN], d[MXN], st[MXN], best[MXN];
int tin[MXN], tout[MXN], uu[MXN], vv[MXN];
int n, m, q, e, tx;
void dfs(int v, int p) {
tin[v] = ++tx;
best[v] = st[v] ? dt[v] : inf;
for(auto &[u, w] : g[v]) if(u != p) {
d[u] = d[v] + 1;
dt[u] = dt[v] + w;
dfs(u, v);
best[v] = min(best[v], best[u]);
}
tout[v] = tx;
}
void init(int v, int p) {
for(int i = 1; i < LGN; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
dp[v][i] = min(dp[v][i - 1], dp[up[v][i - 1]][i - 1]);
}
for(auto &[u, w] : g[v]) if(u != p) {
up[u][0] = v;
dp[u][0] = min(best[v], best[u]);
init(u, v);
}
}
bool anc(int u, int v) {
return tin[v] <= tin[u] && tout[u] <= tout[v];
}
signed main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
cin >> n >> m >> q >> e;
for(int u, v, w, i = 1; i < n; i++) {
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
uu[i] = u, vv[i] = v;
}
for(int v, i = 0; i < m; i++) {
cin >> v;
st[v] = true;
}
dfs(e, -1);
// cout << "bests: \n";
// for(int i = 1; i <= n; i++) {
// cout << best[i] << '\n';
// }
for(int i = 1; i <= n; i++)
best[i] -= 2 * dt[i];
// cout << "in/out: \n";
// for(int i = 1; i <= n; i++) {
// cout << tin[i] << ' ' << tout[i] << '\n';
// }
// cout << "depths: \n";
// for(int i = 1; i <= n; i++) {
// cout << d[i] << ' ' << dt[i] << '\n';
// }
// cout << "bests: \n";
// for(int i = 1; i <= n; i++) {
// cout << best[i] + dt[i] << '\n';
// }
dp[e][0] = best[e];
init(e, 0);
while(q--) {
int a, r;
cin >> a >> r;
int u = uu[a], v = vv[a];
if(d[u] < d[v])
swap(u, v);
if(anc(r, u)) {
int h = d[r] - d[u];
int ans = inf, o = r;
//cout << h << '\n';
for(int i = LGN - 1; i >= 0; i--) if(h >> i & 1) {
//cout << "in: " << i << ' ' << dp[r][i] << endl;
ans = min(ans, dp[r][i]);
r = up[r][i];
}
if(ans >= 1e16)
cout << "oo" << '\n';
else
cout << ans + dt[o] << '\n';
} else {
cout << "escaped" << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
169 ms |
48360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |