Submission #1094893

#TimeUsernameProblemLanguageResultExecution timeMemory
1094893TymondValley (BOI19_valley)C++17
100 / 100
99 ms43200 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define fi first #define se second #define vi vector<int> #define vll vector<long long> #define pii pair<int, int> #define pll pair<long long, long long> #define pb push_back #define mp make_pair #define eb emplace_back #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count()); inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);} inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);} #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; const ll INF = 1e18; const int MAXN = 1e5 + 7; const int MAXK = 20; int jump[MAXK][MAXN]; ll mn[MAXK][MAXN]; int parent[MAXN]; vector<pii> g[MAXN]; pii range[MAXN]; ll dist[MAXN]; int up[MAXN]; int dep[MAXN]; int f[MAXN]; ll len[MAXN]; int n, s, q, source; int t; vector<pii> edges; void dfs(int v, int p, int d, ll x){ dep[v] = d; parent[v] = p; len[v] = x; range[v].fi = t++; if(f[v]){ dist[v] = 0LL; }else{ dist[v] = INF; } for(auto [u, c] : g[v]){ if(u == p){ continue; } up[u] = c; dfs(u, v, d + 1, (ll)x + c); dist[v] = min(dist[v], (ll)dist[u] + c); } range[v].se = t++; } void init(){ for(int i = 1; i <= n; i++){ jump[0][i] = parent[i]; mn[0][i] = (ll)dist[i] - len[i]; } for(int j = 1; j < MAXK; j++){ for(int i = 1; i <= n; i++){ jump[j][i] = jump[j - 1][jump[j - 1][i]]; mn[j][i] = min(mn[j - 1][i], mn[j - 1][jump[j - 1][i]]); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> s >> q >> source; for(int i = 1; i < n; i++){ int u, v, c; cin >> u >> v >> c; g[u].pb({v, c}); g[v].pb({u, c}); edges.pb({u, v}); } for(int i = 1; i <= s; i++){ int v; cin >> v; f[v] = 1; } t = 0; dfs(source, source, 0, 0LL); init(); while(q--){ int ind, v; cin >> ind >> v; ind--; int u = edges[ind].fi; if(dep[edges[ind].se] > dep[u]){ u = edges[ind].se; } if(range[u].fi > range[v].fi || range[v].se > range[u].se){ cout << "escaped\n"; continue; } if(dist[u] == INF){ cout << "oo\n"; continue; } ll ans = dist[v]; int akt = v; for(int j = MAXK - 1; j >= 0; j--){ if(dep[akt] - (1 << j) > dep[u]){ ans = min(ans, (ll)mn[j][akt] + len[v]); akt = jump[j][akt]; } } ans = min(ans, (ll)dist[akt] - len[akt] + len[v]); ans = min(ans, (ll)dist[u] - len[u] + len[v]); cout << ans << '\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...