Submission #1049237

#TimeUsernameProblemLanguageResultExecution timeMemory
1049237khanhtbValley (BOI19_valley)C++14
0 / 100
104 ms212592 KiB
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define ld long double #define pb push_back #define pf push_front #define vi vector<ll> #define vii vector<vi> #define pll pair<ll, ll> #define vpll vector<pll> #define all(a) a.begin(), a.end() #define fi first #define se second using namespace std; const ll mod = 1e9+7; const ll inf = 2e18; const ll B = 320; const ll N = 6e5+8; int n,s,q,e; vector<pair<int,ll>> g[N]; vector<pair<int,int>> edge = {{0,0}}; bool mark[N]; int tin[N],tout[N],tdfs,d[N],h[N]; void dfs_shop(int u, int p){ tin[u] = ++tdfs; for(pair<int,ll> ed:g[u]){ int v = ed.fi; ll w = ed.se; if(v == p) continue; d[v] = d[u]+w; h[v] = h[u]+1; dfs_shop(v,u); } tout[u] = tdfs; } ll a[N]; void dfs_a(int u, int p){ a[u] = inf; if(mark[u]) a[u] = d[u]; for(pair<int,ll> ed:g[u]){ int v = ed.fi; ll w = ed.se; if(v == p) continue; dfs_a(v,u); a[u] = min(a[u],a[v]); } } struct node{ int pr; ll mn = inf; } up[N][20]; void dfs_pre(int u, int p){ for(pair<int,ll> ed:g[u]){ int v = ed.fi; if(v == p) continue; up[v][0].pr = u; up[v][0].mn = min(a[u]-2*d[u],a[v]-2*d[v]); for(int i = 1; i <= 19; i++){ up[v][i].pr = up[up[v][i-1].pr][i-1].pr; up[v][i].mn = min(up[v][i-1].mn,up[up[v][i-1].pr][i-1].mn); } dfs_pre(v,u); } } bool is_anc(int u, int v){ return tin[u] <= tin[v] && tout[v] <= tout[u]; } ll query(int u, int p){ int k = h[u]-h[p]; ll ans = inf; for(int i = 19; i >= 0; i--){ if(k>>i&1){ ans = min(ans,up[u][i].mn); u = up[u][i].pr; } } ans = min(ans,a[u]-2*d[u]); return ans; } void solve(){ cin >> n >> s >> q >> e; for(int i = 1; i < n; i++){ int u,v; ll w;cin >> u >> v >> w; g[u].pb({v,w}); g[v].pb({u,w}); edge.pb({u,v}); } for(int i = 1; i <= s; i++){ int x;cin >> x; mark[x] = 1; } dfs_shop(e,e); dfs_a(e,e); dfs_pre(e,e); while(q--){ int id,r;cin >> id >> r; int u; if(h[edge[id].fi] > h[edge[id].se]) u = edge[id].fi; else u = edge[id].se; if(!is_anc(u,r)) cout << "escaped\n"; else{ ll ans = query(r,u)+d[r]; if(ans >= 1e18) cout << "oo\n"; else cout << ans << '\n'; } } } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); if (fopen("test.inp", "r")) { freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout); } ll T = 1; // cin >> T; for (ll i = 1; i <= T; i++) { solve(); // cout << '\n'; } }

Compilation message (stderr)

valley.cpp: In function 'void dfs_a(int, int)':
valley.cpp:42:12: warning: unused variable 'w' [-Wunused-variable]
   42 |         ll w = ed.se;
      |            ^
valley.cpp: In function 'int main()':
valley.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...