제출 #792681

#제출 시각아이디문제언어결과실행 시간메모리
792681Theo830Cat Exercise (JOI23_ho_t4)C++17
100 / 100
341 ms65156 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
ll n;
ll p[200005];
vector<vll>adj;
ll dp[200005];
ll par[200005];
ll an[200005][20];
ll depth[200005] = {0};
ll find_par(ll x){
    if(par[x] == x){
        return x;
    }
    return par[x] = find_par(par[x]);
}
void enose(ll a,ll b){
    ll p1 = find_par(a);
    ll p2 = find_par(b);
    par[p1] = p2;
}
ll kth(ll a,ll k){
    ll pos = 0;
    while(k){
        if(k % 2){
            a = an[a][pos];
        }
        pos++;
        k /= 2;
    }
    return a;
}
ll lca(ll a,ll b){
    if(depth[a] > depth[b]){
        swap(a,b);
    }
    b = kth(b,depth[b] - depth[a]);
    if(a == b){
        return a;
    }
    for(ll j = 19;j >= 0;j--){
        if(an[a][j] != an[b][j]){
            a = an[a][j];
            b = an[b][j];
        }
    }
    return an[a][0];
}
ll dist(ll a,ll b){
    return depth[a] + depth[b] - 2 * depth[lca(a,b)];
}
void build(ll idx,ll par){
    an[idx][0] = par;
    f(j,1,20){
        an[idx][j] = an[an[idx][j-1]][j-1];
    }
    for(auto x:adj[idx]){
        if(x != par){
            depth[x] = depth[idx] + 1;
            build(x,idx);
        }
    }
}
ll solve(ll idx){
    if(dp[idx] != -1){
        return dp[idx];
    }
    ll ans = 0;
    for(auto x:adj[idx]){
        if(p[x] < p[idx]){
            ans = max(ans,solve(find_par(x)) + dist(find_par(x),idx));
            enose(x,idx);
        }
    }
    return dp[idx] = ans;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    adj.assign(n+5,vll());
    ll root = -1;
    vector<ii>ex;
    f(i,0,n){
        cin>>p[i+1];
        ex.pb(ii(p[i+1],i+1));
        if(p[i+1] == n){
            root = i+1;
        }
    }
    f(i,0,n-1){
        ll a,b;
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    f(i,0,n+5){
        par[i] = i;
    }
    memset(dp,-1,sizeof dp);
    build(root,0);
    sort(all(ex));
    for(auto x:ex){
        solve(x.S);
    }
    cout<<solve(root)<<"\n";
}
#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...