Submission #555730

#TimeUsernameProblemLanguageResultExecution timeMemory
555730SavicSValley (BOI19_valley)C++17
100 / 100
215 ms44028 KiB
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<ld,ld> pdd; #define ff(i,a,b) for(int i = a; i <= b; i++) #define fb(i,b,a) for(int i = b; i >= a; i--) #define trav(a,x) for (auto& a : x) #define sz(a) (int)(a).size() #define pb push_back #define fi first #define se second #define lb lower_bound #define ub upper_bound #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // os.order_of_key(k) the number of elements in the os less than k // *os.find_by_order(k) print the k-th smallest number in os(0-based) const int mod = 1000000007; const ll inf = 1e18 + 5; const int mxN = 200005; int n, m, q, root; pii edge[mxN]; vector<pii> g[mxN]; bool mark[mxN]; int timer = 0; int in[mxN]; int out[mxN]; ll dist[mxN]; ll best[mxN]; int deda[mxN][20]; void dfs1(int v, int p){ if(mark[v] == 1)best[v] = 0; deda[v][0] = p; ff(i,1,19)deda[v][i] = deda[deda[v][i - 1]][i - 1]; in[v] = ++timer; for(auto c : g[v]){ int u = c.fi; int w = c.se; if(u != p){ dist[u] = dist[v] + w; dfs1(u, v); best[v] = min(best[v], best[u] + w); } } out[v] = timer; } ll mn[mxN][20]; void dfs2(int v, int p){ if(p != 0 && best[p] != inf)mn[v][0] = best[p] - dist[p]; ff(i,1,19)mn[v][i] = min(mn[v][i - 1], mn[deda[v][i - 1]][i - 1]); for(auto c : g[v]){ if(c.fi != p){ dfs2(c.fi, v); } } } // da li je y predak od x bool predak(int x, int y){ return in[y] <= in[x] && in[x] <= out[y]; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m >> q >> root; ff(i,1,n - 1){ int u, v, w; cin >> u >> v >> w; g[u].pb({v, w}); g[v].pb({u, w}); edge[i] = {u, v}; } ff(i,1,m){ int X; cin >> X; mark[X] = 1; } ff(i,1,n)best[i] = inf; ff(i,0,n)ff(j,0,19)mn[i][j] = inf; dfs1(root, 0); dfs2(root, 0); while(q--){ int i, v; cin >> i >> v; int a = edge[i].fi; int b = edge[i].se; if(dist[a] < dist[b])swap(a, b); if(!predak(v, a)){ cout << "escaped" << '\n'; continue; } ll ans = (best[v] != inf ? best[v] - dist[v] : inf); int X = v; fb(j,19,0){ if(deda[X][j] && dist[deda[X][j]] >= dist[a]){ ans = min(ans, mn[X][j]); X = deda[X][j]; } } if(ans == inf)cout << "oo" << '\n'; else cout << ans + dist[v] << '\n'; } return 0; } /* 5 2 3 1 1 2 3 1 3 2 3 4 1 3 5 2 2 4 2 2 2 5 4 5 10 2 5 4 7 2 3 4 8 3 9 10 1 6 7 3 9 2 3 10 1 2 8 2 2 5 2 1 3 8 2 8 7 2 1 1 5 8 4 6 2 7 7 // probati bojenje sahovski */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...