Submission #924368

#TimeUsernameProblemLanguageResultExecution timeMemory
924368Edu175Cat Exercise (JOI23_ho_t4)C++17
100 / 100
308 ms60056 KiB
#include <bits/stdc++.h> #define pb push_back #define fst first #define snd second #define fore(i,a,b) for(ll i=a,ggdem=b;i<ggdem;++i) #define SZ(x) ((int)x.size()) #define ALL(x) x.begin(),x.end() #define mset(a,v) memset((a),(v),sizeof(a)) #define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define imp(v) for(auto messi:v)cout<<messi<<" "; cout<<"\n" using namespace std; typedef long long ll; typedef pair<ll,ll> ii; const ll MAXN=2e5+5; ll uf[MAXN],pa[MAXN]; ll uf_find(ll x){return uf[x]<0?x:uf[x]=uf_find(uf[x]);} void uf_join(ll x, ll y){ x=uf_find(x),y=uf_find(y); if(-uf[x]>-uf[y])swap(x,y); uf[x]+=uf[y]; uf[y]=x; } ll dp[MAXN]; vector<ll>g[MAXN]; ll n; const ll K=18; //K such that 2^K>=n ll F[K][MAXN],D[MAXN]; void lca_dfs(ll x){ for(auto y:g[x]){ if(y==F[0][x])continue; F[0][y]=x;D[y]=D[x]+1;lca_dfs(y); } } void lca_init(){ D[0]=0;F[0][0]=-1; lca_dfs(0); fore(k,1,K)fore(x,0,n){ if(F[k-1][x]<0)F[k][x]=-1; else F[k][x]=F[k-1][F[k-1][x]]; } } ll lca(ll x,ll y){ if(D[x]<D[y])swap(x,y); for(ll k=K-1;k>=0;k--)if(D[x]-(1ll<<k)>=D[y])x=F[k][x]; if(x==y)return x; for(ll k=K-1;k>=0;k--)if(F[k][x]!=F[k][y])x=F[k][x],y=F[k][y]; return F[0][x]; } ll dis(ll x, ll y){return D[x]+D[y]-2*D[lca(x,y)];} int main(){FIN; cin>>n; vector<ll>a(n),p(n); fore(i,0,n)cin>>a[i],a[i]--,p[a[i]]=i; fore(i,0,n-1){ ll u,v; cin>>u>>v; u--,v--; g[u].pb(v); g[v].pb(u); } mset(uf,-1); lca_init(); fore(i,0,n){ auto x=p[i]; for(auto y:g[x])if(a[y]<a[x]){ y=pa[uf_find(y)]; dp[x]=max(dp[x],dp[y]+dis(x,y)); uf_join(x,y); } pa[uf_find(x)]=x; } cout<<dp[p[n-1]]<<"\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...