Submission #712015

#TimeUsernameProblemLanguageResultExecution timeMemory
712015LoboValley (BOI19_valley)C++17
100 / 100
234 ms36564 KiB
#include<bits/stdc++.h> using namespace std; const long long inf = (long long) 1e18 + 10; const int inf1 = (int) 1e9 + 10; #define int long long #define dbl long double #define endl '\n' #define sc second #define fr first #define mp make_pair #define pb push_back #define all(x) x.begin(), x.end() const int maxn = 1e5+10; int n, in[maxn], out[maxn], dp[maxn], sub[maxn], sp[maxn]; int h[maxn], d[maxn], par[maxn], timer, s, q, rt, tr[4*maxn]; vector<pair<int,int>> g[maxn], edg, qrys[maxn]; pair<int,string> ansq[maxn]; void dfsdp(int u, int ant) { in[u] = ++timer; dp[u] = inf; sub[u] = 1; if(sp[u]) dp[u] = d[u]; for(auto V : g[u]) if(V.fr != ant) { int v = V.fr; int w = V.sc; h[v] = h[u]+1; d[v] = d[u]+w; par[v] = u; dfsdp(v,u); sub[u]+= sub[v]; dp[u] = min(dp[u],dp[v]); } out[u] = timer; } void build(int no, int l, int r) { tr[no] = inf; if(l == r) return; int lc=2*no,rc=2*no+1,mid=(l+r)>>1; build(lc,l,mid); build(rc,mid+1,r); } void upd(int no, int l, int r, int pos, int val) { if(l > pos || r < pos) return; if(l == r) { tr[no] = val; return; } int lc=2*no,rc=2*no+1,mid=(l+r)>>1; upd(lc,l,mid,pos,val); upd(rc,mid+1,r,pos,val); tr[no] = min(tr[lc],tr[rc]); } int qry(int no, int l, int r, int L, int R) { if(l > R || r < L) return inf; if(l >= L && r <= R) return tr[no]; int lc=2*no,rc=2*no+1,mid=(l+r)>>1; return min(qry(lc,l,mid,L,R),qry(rc,mid+1,r,L,R)); } void dfsqry(int u, int ant) { if(dp[u] == inf) upd(1,0,n,h[u],inf); else upd(1,0,n,h[u],dp[u]-2*d[u]); for(auto X : qrys[u]) { int id = X.fr; int i = X.sc; int b; if(h[edg[id-1].fr] > h[edg[id-1].sc]) b = edg[id-1].fr; else b = edg[id-1].sc; if(in[b] <= in[u] && in[u] <= out[b]) { int ans = qry(1,0,n,h[b],h[u]); if(ans == inf) ansq[i] = mp(-1,"oo"); else ansq[i] = mp(ans+d[u],""); } else { ansq[i] = mp(-1,"escaped"); } } for(auto V : g[u]) if(V.fr != ant) { int v = V.fr; dfsqry(v,u); } } void solve() { cin >> n >> s >> q >> rt; for(int i = 1; i <= n-1; i++) { int u,v,w; cin >> u >> v >> w; g[u].pb(mp(v,w)); g[v].pb(mp(u,w)); edg.pb(mp(u,v)); } for(int i = 1; i <= s; i++) { int v; cin >> v; sp[v] = 1; } dfsdp(rt,0); for(int i = 1; i <= q; i++) { int id, u; cin >> id >> u; qrys[u].pb(mp(id,i)); } build(1,0,n); dfsqry(rt,0); for(int i = 1; i <= q; i++) { if(ansq[i].fr == -1) cout << ansq[i].sc << endl; else cout << ansq[i].fr << endl; } // while(q--) { // int id, u; // cin >> id >> u; // int b; // if(h[edg[id-1].fr] > h[edg[id-1].sc]) b = edg[id-1].fr; // else b = edg[id-1].sc; // if(in[b] <= in[u] && in[u] <= out[b]) { // int ans = inf; // int v = u; // while(true) { // if(dp[v] != inf) { // ans = min(ans, dp[v]-2*d[v]); // } // if(v == b) break; // v = par[v]; // } // if(ans == inf) cout << "oo" << endl; // else cout << ans+d[u] << endl; // } // else { // cout << "escaped" << endl; // } // } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("in.in", "r", stdin); // freopen("out.out", "w", stdout); int tt = 1; // cin >> tt; while(tt--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...