Submission #916705

#TimeUsernameProblemLanguageResultExecution timeMemory
916705a_l_i_r_e_z_aValley (BOI19_valley)C++17
100 / 100
167 ms48024 KiB
// In the name of God // Hope is last to die #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define pb push_back #define int long long #define S second #define F first #define mp make_pair #define smax(x, y) (x) = max((x), (y)) #define smin(x, y) (x) = min((x), (y)) #define all(x) (x).begin(), (x).end() #define len(x) ((int)(x).size()) const int maxn = 1e5 + 5, lg = 20; const int inf = 1e18 + 7; int n, s, q, r, timer, h[maxn], val[maxn], dp[maxn][lg], par[maxn][lg]; int ras[maxn], in[maxn], out[maxn]; vector<pair<int, pii>> adj[maxn]; bool mark[maxn]; void dfs(int v, int p){ in[v] = timer++; par[v][0] = p; val[v] = inf; if(mark[v]) val[v] = h[v]; for(auto [u, e]: adj[v]){ if(u != p){ h[u] = h[v] + e.F; ras[e.S] = u; dfs(u, v); smin(val[v], val[u]); } } dp[v][0] = val[v] - 2 * h[v]; out[v] = timer; } bool is_par(int u, int v){ return in[u] <= in[v] && out[u] >= out[v]; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s >> q >> r; for(int i = 1; i < n; i++){ int u, v, w; cin >> u >> v >> w; adj[u].pb(mp(v, mp(w, i))); adj[v].pb(mp(u, mp(w, i))); } for(int i = 0; i < s; i++){ int u; cin >> u; mark[u] = 1; } dfs(r, r); for(int i = 0; i < lg; i++) dp[0][i] = inf; for(int b = 1; b < lg; b++){ for(int i = 1; i <= n; i++){ par[i][b] = par[par[i][b - 1]][b - 1]; dp[i][b] = dp[i][b - 1]; smin(dp[i][b], dp[par[i][b - 1]][b - 1]); } } while(q--){ int ind, v; cin >> ind >> v; int u = ras[ind]; if(!is_par(u, v)) cout << "escaped\n"; else{ int x = v; int ans = dp[u][0]; for(int b = lg - 1; b >= 0; b--){ if(!is_par(par[v][b], u)){ smin(ans, dp[v][b]); v = par[v][b]; } } smin(ans, dp[v][0]); ans += h[x]; if(ans <= 1e15) cout << ans << '\n'; else cout << "oo\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...