Submission #867615

#TimeUsernameProblemLanguageResultExecution timeMemory
86761512345678Valley (BOI19_valley)C++17
100 / 100
160 ms55892 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int nx=1e5+5, kx=17; ll n, s, q, e, u, v, w, mnd[nx], mn[nx][kx], pa[nx][kx], ans[nx], lvl[nx], dp[nx], qs[nx], x, y, cnt; vector<tuple<ll, ll, ll>> d[nx]; vector<pair<ll, ll>> t[nx]; bool pw[nx]; void dfs(int u, int p) { //cout<<"here"<<' '<<u<<' '<<mnd[u]<<'\n'; pa[u][0]=p; lvl[u]=lvl[p]+1; for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1]; for (auto [v, w, i]:d[u]) { if (v==p) continue; qs[v]=qs[u]+w; dfs(v, u); mnd[u]=min(mnd[u], mnd[v]+w); } if (pw[u]) mnd[u]=0; } void dfs2(int u, int p) { for (int i=1; i<kx; i++) mn[u][i]=min(mn[u][i-1], mn[pa[u][i-1]][i-1]+qs[u]-qs[pa[u][i-1]]); //for (int i=0; i<4; i++) cout<<u<<' '<<i<<' '<<mn[u][i]<<'\n'; for (auto [x, y]:t[u]) { if (dp[x]) { ll dn=dp[x], res=mnd[u], tmp=u, vl=0; for (int i=kx-1; i>=0; i--) if (lvl[pa[tmp][i]]>=lvl[dn]) res=min(res, mn[tmp][i]+vl), vl+=qs[tmp]-qs[pa[tmp][i]], tmp=pa[tmp][i]; ans[y]=res; } else ans[y]=-1; } for (auto [v, w, i]:d[u]) { if (v==p) continue; dp[i]=v; mn[v][0]=min(mnd[u]+w, (ll)1e18); dfs2(v, u); dp[i]=0; } } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n>>s>>q>>e; for (int i=1; i<=n-1; i++) cin>>u>>v>>w, d[u].push_back({v, w, i}), d[v].push_back({u, w, i}); for (int i=1; i<=n; i++) mnd[i]=1e18; for (int i=0; i<s; i++) cin>>x, pw[x]=1; for (int i=0; i<q; i++) cin>>x>>y, t[y].push_back({x, i}); dfs(e, e); mn[e][0]=1e18; dfs2(e, e); for (int i=0; i<q; i++) { if (ans[i]==-1) cout<<"escaped\n"; else if (ans[i]>=1e18) cout<<"oo\n"; else cout<<ans[i]<<'\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...