제출 #411567

#제출 시각아이디문제언어결과실행 시간메모리
411567ak2006Valley (BOI19_valley)C++14
100 / 100
498 ms72024 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vi = vector<int>; using vvi = vector<vi>; using vb = vector<bool>; #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define pb push_back const ll inf = 1e15; void setIO() { fast; } int n = 1e5 + 5,s,q,root; vi in(n),out(n),num(n); vl magic(n,inf),dist(n); vvl anc(n,vl(21)),dp(n,vl(21)); vvvl adj(n); vb shop(n); int t; void dfs(int u,int p,ll wt) { if (p != -1)dist[u] = dist[p] + wt,num[u] = num[p] + 1; in[u] = t++; if (shop[u])magic[u] = dist[u]; for (auto x:adj[u]){ int v = x[0]; ll w = x[1]; if (v == p)continue; dfs(v,u,w); magic[u] = min(magic[u],magic[v]); } out[u] = t++; } bool is_ancestor(int u,int v) { return in[u] <= in[v] && out[u] >= out[v]; } void dfs2(int u,int p) { anc[u][0] = p; dp[u][0] = magic[u]; for (int i = 1;i<=20;i++){ anc[u][i] = anc[anc[u][i - 1]][i - 1]; dp[u][i] = min(dp[u][i - 1],dp[anc[u][i - 1]][i - 1]); } for (auto x:adj[u]){ int v = x[0]; if (v == p)continue; dfs2(v,u); } } int main() { setIO(); cin>>n>>s>>q>>root; vvi edges; for (int i = 1;i<n;i++){ int u,v,w; cin>>u>>v>>w; adj[u].pb({v,w}); adj[v].pb({u,w}); edges.pb({u,v}); } for (int i = 1;i<=s;i++){ int x; cin>>x; shop[x] = 1; } dfs(root,-1,0); for (int i = 1;i<=n;i++)if (magic[i] < inf)magic[i] += -2 * dist[i]; dfs2(root,root); while (q--){ int pos,u; cin>>pos>>u; int child = edges[pos - 1][0],par = edges[pos - 1][1]; if (in[child] < in[par])swap(child,par); if (!is_ancestor(child,u))cout<<"escaped"<<'\n'; else { //now find min of magic[v] for all v on the path from u to child int distance = - num[child] + num[u] + 1; int node = u; ll val = inf; for (int i = 0;i<=20;i++){ if (distance&(1<<i)){ val = min(val,dp[u][i]); u = anc[u][i]; } } if (val >= inf)cout<<"oo"<<'\n'; else cout<<dist[node] + val<<'\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...