제출 #328633

#제출 시각아이디문제언어결과실행 시간메모리
328633soroushValley (BOI19_valley)C++14
100 / 100
314 ms66796 KiB
//* #pragma GCC optimize("O2") //#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") //#pragma GCC target("avx,avx2,sse,sse2,fma,tune=native") //*/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll ,ll > pii; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll maxn = 1e5 + 100; const ll mod =1e9+7; const ld PI = acos((ld)-1); #define pb push_back #define endl '\n' #define dokme(x) cout << x , exit(0) #define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ms(x , y) memset(x , y , sizeof x) ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);} int n , q , s , ex; bool shop[maxn]; pii e[maxn]; ll w[maxn]; int par[maxn][23]; vector < pii > adj[maxn]; int h[maxn]; int l[maxn] , r[maxn] , t = 0; ll H[maxn]; int ord[maxn]; void dfs(int v){ r[v] = l[v] = ++t; ord[t] = v; for (auto uw : adj[v]){ auto u = uw.first; auto w = uw.second; if(u!=par[v][0]){ par[u][0] = v; h[u] = h[v]+1; H[u] = H[v] + w; dfs(u); r[v] = r[u]; } } } int lca(int u , int v){ if(h[u] > h[v]){ swap(u , v); } for (int i = 20 ; i >= 0 ; i --){ if(par[v][i] and h[par[v][i]]>=h[u]){ v = par[v][i]; } } if(v == u){ return(v); } for (int i = 20 ; i >= 0 ; i --){ if(par[v][i] != par[u][i]){ v = par[v][i] , u = par[u][i]; } } return(par[v][0]); } void init(){ dfs(ex); for (int j = 1; j <= 20 ; j ++){ for (int i = 1 ; i <= n ; i ++){ par[i][j] = par[par[i][j - 1]][j - 1]; } } } ll dist(int u , int v){ return(H[u] + H[v] - 2*H[lca(u, v)]); } ll f[maxn][23]; ll val[maxn][23]; ll mn(ll l , ll r){ ll cur = l; ll ans = val[l][0]; for(int i = 20 ; i >= 0 ; i --){ if(cur + (1 << i) > r)continue; ans = min(ans , val[cur][i]); cur+=(1 << i); ans = min(ans , val[cur][0]); } return(ans); } int32_t main(){ migmig; cin >> n >> s >> q >> ex; for(int i = 1 ; i < n ; i ++) cin >> e[i].first >> e[i].second >> w[i], adj[e[i].first].pb({e[i].second , w[i]}), adj[e[i].second].pb({e[i].first , w[i]}); for(int i = 0 ; i < s ; i ++){ int x; cin >> x; shop[x] = 1; } init(); h[0] = -1; for(int i = 1 ; i <= n ; i ++){ if(shop[i])val[l[i]][0] = H[i]; else val[l[i]][0] = 1e18; } for(int j = 0 ; j < 20 ; j ++){ for(int i = 1 ; i <= n ; i ++){ if(i + (1 << j) > n)continue; val[i][j+1] = min(val[i][j] , val[i + (1 << j)][j]); } } for(int i = 1 ; i <= n ; i ++){ if(mn(l[i] , r[i]) == 1e18) f[i][0] = 1e18; else f[i][0] = mn(l[i] , r[i]) - 2LL*H[i]; } for(int j = 1 ; j <= 20 ; j ++) for(int i = 1 ; i <= n ; i ++) f[i][j] = min(f[i][j-1] ,f[par[i][j-1]][j-1]); while(q -- ){ int i , V; cin >> i >> V; auto u = e[i].first; auto v = e[i].second; if(h[u] < h[v])swap(u , v); if(l[u] <= l[V] and l[V] <= r[u]){ if(f[u][0] == 1e18){ cout << "oo" << endl; } else{ int cur = V; ll ans = 1e18; for(int i = 20 ; i >= 0 ; i --){ if(h[par[cur][i]] < h[u] )continue; ans = min(ans , f[cur][i]); cur = par[cur][i]; ans = min(ans , f[cur][0]); } ans = min(ans , f[cur][0]); cout << ans + H[V] << endl; } } else{ cout << "escaped" << endl; } } 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...