Submission #997155

#TimeUsernameProblemLanguageResultExecution timeMemory
997155goats_9Valley (BOI19_valley)C++17
100 / 100
265 ms77392 KiB
/* 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)); vector<vector<array<ll, 2>>> adj(n+1, vector<array<ll, 2>>()); for (int i = 1; i < n; i++) { int u, v, w; cin >> u >> v >> w; edges[i] = {u, v, w}; adj[u].push_back({v, w}); adj[v].push_back({u, 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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...