Submission #1095380

#TimeUsernameProblemLanguageResultExecution timeMemory
1095380abcvuitunggioCat Exercise (JOI23_ho_t4)C++17
100 / 100
307 ms77652 KiB
#include <bits/stdc++.h> #define int long long using namespace std; int n,h[200001],p[200001][20],d[200001],pos[200001],l[200001],u,v,mx[200001],res; vector <int> ke[200001],ke2[200001]; void dfs(int u, int par){ for (int v:ke[u]) if (v!=par){ p[v][0]=u; for (int i=1;i<20;i++) p[v][i]=p[p[v][i-1]][i-1]; d[v]=d[u]+1; dfs(v,u); } } int lca(int u, int v){ if (d[u]<d[v]) swap(u,v); for (int i=19;i>=0;i--) if (d[p[u][i]]>=d[v]) u=p[u][i]; if (u==v) return u; for (int i=19;i>=0;i--) if (p[u][i]!=p[v][i]){ u=p[u][i]; v=p[v][i]; } return p[u][0]; } int dist(int u, int v){ return d[u]+d[v]-d[lca(u,v)]*2; } int f(int i){ return (l[i]==i?i:l[i]=f(l[i])); } void unite(int i, int j){ i=f(i),j=f(j); l[i]=j; mx[j]=max(mx[i],mx[j]); } void dfs2(int u, int val){ res=max(res,val); for (int v:ke2[u]) dfs2(v,val+dist(u,v)); } int32_t main(){ ios_base::sync_with_stdio(NULL);cin.tie(nullptr); cin >> n; for (int i=1;i<=n;i++){ cin >> h[i]; pos[h[i]]=i; l[i]=i; mx[i]=h[i]; } for (int i=1;i<n;i++){ cin >> u >> v; ke[u].push_back(v); ke[v].push_back(u); } dfs(pos[n],0); for (int i=1;i<=n;i++){ int u=pos[i]; for (int j:ke[u]) if (h[j]<h[u]){ int v=pos[mx[f(j)]]; ke2[u].push_back(v); unite(u,j); } } dfs2(pos[n],0); cout << res; }
#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...