## Submission #997155

# Submission time Handle Problem Language Result Execution time Memory
997155 2024-06-11T18:19:04 Z goats_9 Valley (BOI19_valley) C++17
100 / 100
265 ms 77392 KB
```/* Author: goats_9 */

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll INF = 1e16;
const int LOG = 18;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, s, q, e;
cin >> n >> s >> q >> e;
vector<array<ll, 3>> edges(n);
vector<ll> store(n+1, INF), depth(n+1), in(n + 1), out(n + 1);
vector<vector<ll>> par(n+1, vector<ll> (LOG)), parlen(n+1, vector<ll> (LOG)), parcost(n+1, vector<ll> (LOG, INF));
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
edges[i] = {u, v, w};
}
while (s--) {
int x;
cin >> x;
store[x] = 0;
}
int cnt = 0;
function<void(int, int)> dfs = [&] (int u, int p) {
in[u] = cnt++;
for (auto &[v, w] : adj[u]) {
if (v == p) continue;
depth[v] = depth[u] + 1;
dfs(v, u);
store[u] = min(store[u], store[v] + w);
}
out[u] = cnt++;
};
dfs(e, 0);
function<void(int, int, ll)> dfs2 = [&] (int u, int p, ll w) {
par[u][0] = p;
parlen[u][0] = w;
for (int i = 1; i < LOG; i++) {
par[u][i] = par[par[u][i - 1]][i - 1];
parlen[u][i] = parlen[u][i - 1] + parlen[par[u][i - 1]][i - 1];
}
parcost[u][0] = min(store[u], w + store[p]);
for (int i = 1; i < LOG; i++) {
parcost[u][i] = min(parcost[u][i - 1], parlen[u][i - 1] + parcost[par[u][i - 1]][i - 1]);
}
for (auto [v, w1] : adj[u]) {
if (v == p) continue;
dfs2(v, u, w1);
}
};
dfs2(e, 0, 0);
while (q--) {
int i, r;
cin >> i >> r;
auto [u, v, w] = edges[i];
if (depth[u] > depth[v]) swap(u, v);
bool fl = (in[v] <= in[r]) && (out[r] <= out[v]);
if (!fl) {
cout << "escaped\n";
continue;
}
ll ans = store[r];
ll psm = 0;
for (int j = LOG - 1; j >= 0; j--) {
if (depth[par[r][j]] > depth[v]) {
ans = min(ans, psm + parcost[r][j]);
psm += parlen[r][j];
r = par[r][j];
}
}
if (r != v) ans = min(ans, psm + parcost[r][0]);
if (ans == INF) cout << "oo\n";
else cout << ans << '\n';
}
return 0;
}
```

#### Subtask #1 9.0 / 9.0

# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct

#### Subtask #2 27.0 / 27.0

# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 2 ms 1172 KB Output is correct
11 Correct 1 ms 1112 KB Output is correct
12 Correct 1 ms 1112 KB Output is correct
13 Correct 1 ms 1116 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct

#### Subtask #3 23.0 / 23.0

# Verdict Execution time Memory Grader output
1 Correct 188 ms 68244 KB Output is correct
2 Correct 181 ms 71596 KB Output is correct
3 Correct 188 ms 71760 KB Output is correct
4 Correct 225 ms 74088 KB Output is correct
5 Correct 197 ms 74320 KB Output is correct
6 Correct 265 ms 76904 KB Output is correct

#### Subtask #4 41.0 / 41.0

# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 2 ms 1172 KB Output is correct
11 Correct 1 ms 1112 KB Output is correct
12 Correct 1 ms 1112 KB Output is correct
13 Correct 1 ms 1116 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 188 ms 68244 KB Output is correct
16 Correct 181 ms 71596 KB Output is correct
17 Correct 188 ms 71760 KB Output is correct
18 Correct 225 ms 74088 KB Output is correct
19 Correct 197 ms 74320 KB Output is correct
20 Correct 265 ms 76904 KB Output is correct
21 Correct 205 ms 71248 KB Output is correct
22 Correct 163 ms 71048 KB Output is correct
23 Correct 177 ms 71248 KB Output is correct
24 Correct 191 ms 73808 KB Output is correct
25 Correct 247 ms 77392 KB Output is correct
26 Correct 163 ms 71248 KB Output is correct
27 Correct 154 ms 71252 KB Output is correct
28 Correct 174 ms 71248 KB Output is correct
29 Correct 231 ms 73040 KB Output is correct
30 Correct 236 ms 74928 KB Output is correct
31 Correct 149 ms 71248 KB Output is correct
32 Correct 171 ms 71252 KB Output is correct
33 Correct 173 ms 71508 KB Output is correct
34 Correct 215 ms 73808 KB Output is correct
35 Correct 242 ms 77136 KB Output is correct
36 Correct 203 ms 73644 KB Output is correct