Submission #836285

#TimeUsernameProblemLanguageResultExecution timeMemory
836285lamduybao03Valley (BOI19_valley)C++14
100 / 100
440 ms69144 KiB
#include <bits/stdc++.h> #define int long long #define sqr(x) ((x) * (x)) using namespace std; typedef pair<int, int> pi; typedef pair<pi, int> ppi; const int mod = 1e9+7; const int inv_2 = (mod+1) / 2; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef LOCAL freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif int n, s, q, e; cin >> n >> s >> q >> e; map<pi, int> edge_to_id; vector<vector<pi>> adj(n+1); for(int i = 1; i < n; i++) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w}); edge_to_id[{u, v}] = i; edge_to_id[{v, u}] = i; } vector<int> par(n+1); vector<int> d(n+1); vector<int> h(n+1); vector<int> vis(n+1); vector<int> pos(n+1); vector<int> down(n+1, 1e18); for(int i = 0; i < s; i++) { int u; cin >> u; down[u] = 0; } function<void(int)> dfs = [&] (int u) { vis[u] = 1; for(auto p : adj[u]) { int v = p.first; int w = p.second; if(!vis[v]) { pos[edge_to_id[{u, v}]] = v; par[v] = u; d[v] = d[u] + w; h[v] = h[u] + 1; dfs(v); down[u] = min(down[u], down[v] + w); } } }; dfs(e); for(int i = 1; i <= n; i++) down[i] = down[i] - d[i]; vector<vector<int>> p(20, vector<int> (n+1)); vector<vector<int>> dis(20, vector<int> (n+1, 1e18)); for(int i = 1; i <= n; i++) { p[0][i] = par[i]; dis[0][i] = down[i]; } for(int i = 1; i < 20; i++) { for(int j = 1; j <= n; j++) { p[i][j] = p[i-1][p[i-1][j]]; dis[i][j] = min(dis[i-1][j], dis[i-1][p[i-1][j]]); } } function<int(int, int)> lca = [&] (int u, int v) { if(h[u] < h[v]) { swap(u, v); } int delta = h[u] - h[v]; for(int i = 0; i < 20; i++) { if(delta & (1 << i)) { u = p[i][u]; } } if(u == v) return u; for(int i = 19; i >= 0; i--) { if(p[i][u] != p[i][v]) { u = p[i][u]; v = p[i][v]; } } u = p[0][u]; v = p[0][v]; if(u != v) throw 0; return u; }; function<int(int, int)> get = [&] (int u, int k) { int s = 1e18; for(int i = 0; i < 20; i++) { if(k & (1 << i)) { s = min(s, dis[i][u]); u = p[i][u]; } } return s; }; while(q--) { int i, u; cin >> i >> u; int v = pos[i]; if(lca(u, v) != v) { cout << "escaped\n"; } else { int delta = h[u] - h[v] + 1; int s = get(u, delta) + d[u]; if(s > 1e17) cout << "oo\n"; else cout << s << "\n"; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...