Submission #696933

#TimeUsernameProblemLanguageResultExecution timeMemory
696933DanetValley (BOI19_valley)C++14
100 / 100
600 ms49676 KiB
#include <bits/stdc++.h> using namespace std; #define endline '\n' #define sep " " #define FOR(i,a,b) for(int i = (a); i < (b); i++) #define Tof_Io ios_base::sync_with_stdio(false);cin.tie(0) , cout.tie(0); #define MODP (int)(1e9 + 7) #define cinarr(arr,n) for(int i=0; i< n; i++) cin >> arr[i]; #define coutarr(arr, x, n) for(int i=x; i< n; i++) {cout << arr[i]<<' ';} #define pb push_back #define F first #define S second #define dbg(...) 007 #define fast ios_base::sync_with_stdio(false), cin.tie(200010Uint), cout.tie(200010ULL) #define rall(x) x.rbegin(), x.rend() #define eb emplace_back #define pii pair <int, int> #define kil(x) return cout << x << '\n', void() #define kill(x) return cout << x << '\n', 0 #define yes cout << "Yes"<<endline; #define mk make_pair #define cin_pair_int(arr,n) for(int i=0; i<=n; i++) cin >> arr[i].Frst >> arr[i].Scondcond; #define int long long int #define revall(x) x.rbegin(),x.rend() #define all(v) v.begin(),v.end() #define ms(x,i) memset(x,i,szzzof(x)) #define vii vector<int> using namespace std; const int N = 1e5 + 23; const int inf = 1e18; const int LOG=18; vector<pair<int,int>>G[N]; int high[N]; int h[N]; int a[N]; pair<pair<int,int>,int>edge[N]; int midis[N]; int n,s,root,q; int pa[N][LOG]; int mi[N][LOG]; void dfs(int u,int par) { for(auto v:G[u]) { if(v.F==par)continue; high[v.F]=high[u]+1; h[v.F]=h[u]+v.S; pa[v.F][0]=u; dfs(v.F,u); midis[u]=min(midis[u],midis[v.F]+v.S); } for(auto v:G[u]) { mi[v.F][0]=midis[u]-h[u]; } } int LCA(int u,int v) { if(high[u]>high[v])swap(u,v); int d=high[v]-high[u]; for(int j=18;j>=0;j--) { if((1<<j)&d){ v=pa[v][j]; } } if(u==v) { return u; } for(int j=18;j>=0;j--) { if(pa[u][j]!=pa[v][j] && pa[u][j]!=0) { u=pa[u][j]; v=pa[v][j]; } } return pa[u][0]; } int fin(int u,int k) { for(int j=18;j>=0;j--) { if((1<<j)&k){ u=pa[u][j]; } } return u; } int fin1(int u,int k) { int d=midis[u]-h[u]; for(int j=18;j>=0;j--) { if((1<<j)&k){ d=min(d,mi[u][j]); u=pa[u][j]; } } return d; } void init(){ for(int j=1;(1<<j)<=n;j++) { for(int i=1;i<=n;i++){ pa[i][j]=pa[pa[i][j-1]][j-1]; mi[i][j]=min(mi[i][j-1],mi[pa[i][j-1]][j-1]); } } } int getdist(int u,int v) { return high[u]+high[v]-2*high[LCA(u,v)]; } int32_t main() { cin >> n >> s >> q >> root; for(int i=1;i<n;i++) { int u,v,w; cin>>u>>v>>w; edge[i]={{u,v},w}; G[u].pb({v,w}); G[v].pb({u,w}); } for(int i=1;i<=n;i++) { midis[i]=inf; } for(int i=1;i<=s;i++) { cin>>a[i]; midis[a[i]]=0; } dfs(root,root); init(); for(int i=1;i<=q;i++) { int index,r; cin>>index>>r; int u=edge[index].F.F; int v=edge[index].F.S; if(pa[v][0]==u)swap(v,u); if(LCA(r,u)!=u){ cout<<"escaped\n"; } else{ int k=high[r]-high[u]; int d1=fin1(r,k); if(d1+h[r]>=1e14) { cout<<"oo\n"; } else { cout<<d1+h[r]<<"\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...