Submission #693431

#TimeUsernameProblemLanguageResultExecution timeMemory
693431anhduc2701Valley (BOI19_valley)C++17
100 / 100
240 ms72576 KiB
/* #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimize("unroll-loops") */ #include<bits/stdc++.h> #define int long long using namespace std; #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define eb emplace_back #define PI 3.14159265359 #define fi first #define se second #define mp make_pair #define pb push_back #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define BIT(x,i) (1&((x)>>(i))) #define MASK(x) (1LL<<(x)) #define task "tnc" typedef long long ll; const ll INF=1e18; const int maxn=1e6+5; const int mod=1e9+7; const int mo=998244353; using pi=pair<ll,ll>; using vi=vector<ll>; using pii=pair<pair<ll,ll>,ll>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<pair<int,int>>G[maxn]; int high[maxn]; int h[maxn]; int a[maxn]; pair<pair<int,int>,int>edge[maxn]; int midis[maxn]; int n,s,root,q; int pa[maxn][19]; int mi[maxn][19]; void dfs(int u,int par){ for(auto v:G[u]){ if(v.fi==par)continue; high[v.fi]=high[u]+1; h[v.fi]=h[u]+v.se; pa[v.fi][0]=u; dfs(v.fi,u); midis[u]=min(midis[u],midis[v.fi]+v.se); } for(auto v:G[u]){ mi[v.fi][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)]; } signed main() { cin.tie(0),cout.tie(0)->sync_with_stdio(0); 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].fi.fi; int v=edge[index].fi.se; 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...