Submission #761552

#TimeUsernameProblemLanguageResultExecution timeMemory
761552MetalPowerValley (BOI19_valley)C++14
0 / 100
129 ms33756 KiB
#include <bits/stdc++.h> using namespace std; #define pii pair<int, int> #define fi first #define se second const int MX = 3e5 + 10; const int LG = 19; const int INF = 1e9 + 7; int N, S, Q, E, dep[MX], in[MX], out[MX], tim = 0; int isShop[MX], dist[MX], sp[MX][LG], wt[MX][LG]; vector<pii> adj[MX]; void chmin(int& a, int b){ if(b < a) a = b; } void dfs(int u, int v){ in[u] = ++tim; if(isShop[u]) dist[u] = 0; for(pii nxt : adj[u]){ if(nxt.fi == v) continue; dep[nxt.fi] = dep[u] + nxt.se; dfs(nxt.fi, u); chmin(dist[u], dist[nxt.fi] + nxt.se); } out[u] = tim; } void dfs2(int u, int v){ if(dist[u] != INF) dist[u] -= dep[u]; sp[u][0] = v; wt[u][0] = dist[u]; for(int j = 1; j < LG; j++){ sp[u][j] = sp[sp[u][j - 1]][j - 1]; wt[u][j] = min(wt[u][j - 1], wt[sp[u][j - 1]][j - 1]); } for(pii nxt : adj[u]){ if(nxt.fi == v) continue; dfs2(nxt.fi, u); } } void init(){ for(int i = 1; i <= N; i++) dist[i] = INF; for(int j = 0; j < LG; j++) sp[0][j] = 0, wt[0][j] = INF; } vector<pii> edges; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> S >> Q >> E; 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}); edges.push_back({u, v}); } for(int i = 1; i <= S; i++){ int C; cin >> C; isShop[C] = true; } init(); dfs(E, 0); dfs2(E, 0); for(int i = 0; i < Q; i++){ int I, R, tp, lw; cin >> I >> R; I--; if(dep[edges[I].fi] < dep[edges[I].se]) tp = edges[I].fi, lw = edges[I].se; else tp = edges[I].se, lw = edges[I].fi; if(in[lw] <= in[R] && in[R] <= out[lw]){ // contained int curr = R, ans = INF; for(int j = LG - 1; j >= 0; j--){ if(dep[sp[curr][j]] > dep[lw]){ chmin(ans, wt[curr][j]); curr = sp[curr][j]; } } chmin(ans, wt[curr][0]); curr = sp[curr][0]; chmin(ans, dist[lw]); if(ans == INF) cout << "oo\n"; else cout << dep[R] + ans << '\n'; }else{ cout << "escaped\n"; } } }

Compilation message (stderr)

valley.cpp: In function 'int main()':
valley.cpp:73:13: warning: variable 'tp' set but not used [-Wunused-but-set-variable]
   73 |   int I, R, tp, lw; cin >> I >> R; I--;
      |             ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...