Submission #931851

#TimeUsernameProblemLanguageResultExecution timeMemory
931851XiaoyangCat Exercise (JOI23_ho_t4)C++17
100 / 100
252 ms68212 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define fi first #define se second #define pii pair<int,int> #define pll pair<long long,long long> #define pb push_back #define debug(x) cerr<<#x<<"="<<x<<endl #define pq priority_queue #define inf 1ll<<60 #define rep(i,a,b) for (ll i=a;i<(b);i++) #define MP make_pair #define SZ(x) (int(x.size())) #define mod 1000000007 #define ALL(x) x.begin(),x.end() #define endl "\n" void inc(ll &a,ll b) {a=(a+b)%mod;} void dec(ll &a,ll b) {a=(a-b+mod)%mod;} int prod(ll a,ll b) {return ll(a)*ll(b)%mod;} int lowbit(ll x) {return x&(-x);} ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;} const ll maxn=222222; ll ht[maxn],dep[maxn],up[20][maxn],sz[maxn],fa[maxn]; ll dp[maxn]; vector<ll>alist[maxn]; //dp i is the max distance that cat moves at pillar with height i void init(){ rep(i,0,maxn)sz[i]=1,fa[i]=i; } ll findset(ll u){ if(fa[u]==u)return u; return fa[u]=findset(fa[u]); } bool mergeset(ll u,ll v){ u=findset(u); v=findset(v); if(u!=v){ if(sz[u]<sz[v])swap(u,v); fa[v]=u; sz[u]+=sz[v]; } } void dfs(ll u,ll p){ up[0][u]=p; for(auto x:alist[u]){ if(x==p)continue; dep[x]=dep[u]+1; dfs(x,u); } } ll lca(ll a,ll b){ if(dep[a]<dep[b])swap(a,b); ll k=dep[a]-dep[b]; rep(i,0,20){ if((1ll<<i)&k)a=up[i][a]; } if(a==b)return a; for(ll i=19;i>=0;i--){ if(up[i][a]!=up[i][b]){ a=up[i][a]; b=up[i][b]; } } return up[0][a]; } ll dis(ll a,ll b){ return dep[a]+dep[b]-2*dep[lca(a,b)]; } int main(){ ios::sync_with_stdio(0); cin.tie(0); //freopen("i.txt","r",stdin); ll n;cin>>n; rep(i,1,n+1)cin>>ht[i]; rep(i,1,n){ ll a,b;cin>>a>>b; alist[ht[a]].pb(ht[b]); alist[ht[b]].pb(ht[a]); } init(); memset(up,-1,sizeof up); dfs(1,-1); rep(k,1,20){ rep(i,1,n+1){ if(up[k-1][i]!=-1){ up[k][i]=up[k-1][up[k-1][i]]; } } } rep(i,1,n+1){//iterate all the p values for(auto x:alist[i]){ x=findset(x); if(x<i){ fa[x]=i; dp[i]=max(dp[i],dp[x]+dis(i,x)); } } } cout<<dp[n]; return 0; }

Compilation message (stderr)

Main.cpp: In function 'bool mergeset(ll, ll)':
Main.cpp:50:1: warning: no return statement in function returning non-void [-Wreturn-type]
   50 | }
      | ^
#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...