Submission #1095790

#TimeUsernameProblemLanguageResultExecution timeMemory
1095790lampoopppCat Exercise (JOI23_ho_t4)C++17
54 / 100
128 ms40952 KiB
#include <bits/stdc++.h> using namespace std; const int N=2e5+10; int up[N+1][20], h[N+1]; vector<int> adj[N+1]; bool vis[N+1]; void dfs(int u) { vis[u]=1; for(int v : adj[u]) { if(vis[v]) continue; h[v]=h[u]+1; up[v][0]=u; for(int i=1;i<20;++i) up[v][i] = up[up[v][i-1]][i-1]; dfs(v); } } int lca(int u,int v) { if(h[u]<h[v]) swap(u,v); int k = h[u]-h[v]; for(int i=0;i<20;++i) { if(k>>i&1) u=up[u][i]; } if(u==v) return u; k=__lg(h[u]); for(k;k>=0;--k) { if(up[u][k]!=up[v][k]) { u=up[u][k]; v=up[v][k]; } } return up[u][0]; } int a[N+1]; int dp[N+1]; int lab[N+1]; int mx[N+1]; int findset(int u) { if(lab[u]<0) return u; else return lab[u]=findset(lab[u]); } void Merge(int s,int t) { if(lab[s]>lab[t]) swap(s,t); lab[s]+=lab[t]; mx[s]=max(mx[s],mx[t]); lab[t]=s; } void unite(int a,int b) { int s=findset(a); int t=findset(b); if(s!=t) Merge(s,t); } int dis(int u,int v) { int c = lca(u,v); return h[u]+h[v]-2*h[c]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i=1;i<=n;++i) { cin >> a[i]; lab[i]=-1; mx[i]=i; } for(int i=1;i<n;++i) { int u,v; cin >> u >> v; adj[a[u]].push_back(a[v]); adj[a[v]].push_back(a[u]); } h[1]=1; dfs(1); dp[1]=0; for(int i=2;i<=n;++i) { for(int v : adj[i]) { if(v<i) { int k = mx[findset(v)]; dp[i]=max(dp[i],dp[k]+dis(k,i)); unite(v,i); } } } cout << dp[n]; }

Compilation message (stderr)

Main.cpp: In function 'int lca(int, int)':
Main.cpp:33:9: warning: statement has no effect [-Wunused-value]
   33 |     for(k;k>=0;--k)
      |         ^
#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...