제출 #650469

#제출 시각아이디문제언어결과실행 시간메모리
650469leroycutValley (BOI19_valley)C++17
100 / 100
210 ms43548 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int N = 1e5 + 3, S = 20; const ll INF = 1e18; int tin[N], tout[N], up[N][S]; bool sh[N]; ll mag[N], len[N], up_mag[N][S]; int t = 1; struct edge{ int v, u; }; edge ed[N]; vector<vector<pair<int, ll>>> g; void dfs0(int v, int p, ll l){ if(p != -1){ up[v][0] = p; } tin[v] = t++; len[v] = l; for(auto i : g[v]){ if(i.first == p) continue; dfs0(i.first, v, l + i.second); } tout[v] = t++; } void cal_mag(int v, int p){ ll mi = (sh[v] ? len[v] : INF); if(mi != INF) mi -= 2 * len[v]; for(auto i : g[v]){ if(i.first == p) continue; cal_mag(i.first, v); if(mag[i.first] != INF) mi = min(mi, mag[i.first] + 2 * i.second); } mag[v] = mi; up_mag[v][0] = mi; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, s, q, e; cin >> n >> s >> q >> e; g.resize(n + 1); for(int i = 1; i <= n - 1; ++i){ int x, y; ll w; cin >> x >> y >> w; ed[i] = {x, y}; g[x].push_back({y, w}); g[y].push_back({x, w}); } for(int i = 0; i < s; ++i){ int a; cin >> a; sh[a] = true; } dfs0(e, -1, 0); cal_mag(e, -1); for(int j = 1; j < S; ++j){ for(int i = 1; i <= n; ++i){ up[i][j] = up[up[i][j - 1]][j - 1]; up_mag[i][j] = min(up_mag[i][j - 1], up_mag[up[i][j - 1]][j - 1]); } } // for(int i = 1; i <= n; ++i){ // cout << len[i] << " " << mag[i] << " " << i << "\n"; // } for(int i = 1; i <= q; ++i){ int ro, v; cin >> ro >> v; int p, ch; p = ed[ro].u; ch = ed[ro].v; ll l = len[v]; if(tin[ed[ro].u] > tin[ed[ro].v]) swap(p, ch); // cout << ch << " " << v << "|\n"; if(tin[ch] <= tin[v] && tout[v] <= tout[ch]){ ll ans = mag[v]; // cout << v << " " << up[v][0] << "!\n"; for(int k = S - 1; k >= 0; --k){ int cur = up[v][k]; if(tin[ch] <= tin[cur] && tout[cur] <= tout[ch]){ // cout << k << " " << v << " " << mag[v] << "*\n"; ans = min(ans, up_mag[v][k]); v = cur; } } ans = min(ans, mag[ch]); // cout << v << " " << mag[v] << " " << up_mag[4][2] << "\n"; if(ans == INF){ cout << "oo\n"; }else{ cout << ans + l << "\n"; } }else{ cout << "escaped\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...