//```// YF YUSUF
// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <queue>
#include <stack>
#include <cassert>
#include <climits>
#include <string>
#include <cstdlib>
#include <random>
#include <iomanip>
#include <ctime>
using namespace std;
#ifdef YF_CHECK
bool LOCAL = 1;
#else
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("O3")
bool LOCAL = 0;
#endif
using i128 = __int128;
using ll = long long;
using ld = long double;
using vll = vector <ll>;
using mll = map <ll,ll>;
using pll = pair <ll,ll>;
using vvl = vector <vll>;
using vpll = vector <pll>;
template<class T>T MIN(T&a,T b){a=min(a,b);return a;}
template<class T>T MAX(T&a,T b){a=max(a,b);return a;}
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sgr v+v+1,(tl+tr)/2+1,tr
#define sgl v+v,tl,(tl+tr)/2
#define pb push_back
#define ins insert
#define S second
#define F first
mt19937_64 MT(time(0));
ll BP(ll a,ll b,ll mod=1e9+7){
if(b==0)return 1;
ll q=BP(a,b/2,mod);
return ((q*q)%mod*(b%2?a:1ll))%mod;
}
ll f(ll x){return x*(x+1)/2;}
ll dup(ll a,ll b){return (a+b-1)/b;}
ll lcm(ll a,ll b){return a/__gcd(a,b)*b;}
ll invf(ll x){return (-1+sqrt(1+8*x))/2;}
ll lg(ll x){return (x ? 63 - __builtin_clzll(x) : -1);}
const ll mod=998244353;
const ll INF=1e18;
const ll inf=1e9+7;
const ll N =2e5+7;
ll n;
ll p[N], a[N];
vll g[N];
struct DSU{
vll p,sz,mx;
ll n;
DSU(ll n):n(n){
p.resize(n+1,0);
sz.resize(n+1,1);
mx.resize(n+1,1);
for(int i=1;i<=n;i++){
p[i] = i;
mx[i] = i;
}
}
ll get_mx(ll x){
x = get(x);
return mx[x];
}
ll get(ll x){
if(p[x]==x)
return x;
else
return p[x] = get(p[x]);
}
bool up(ll a,ll b){
ll A = a, B = b;
a = get(a);
b = get(b);
if(a==b)return 0;
if(sz[a] < sz[b])swap(a,b);
p[b] = a;
MAX(mx[a], mx[b]);
sz[a] += sz[b];
sz[b] = 0;
return 1;
}
};
ll timer, tin[N], tout[N];
ll up[20][N], d[N];
void pre_calc(ll v,ll p){
tin[v] = ++timer;
for(auto to : g[v])if(to != p){
d[to] = d[v] + 1;
up[0][to] = v;
for(int j=1;j<20;j++)
up[j][to] = up[j-1][up[j-1][to]];
pre_calc(to, v);
}
tout[v] = ++timer;
}
bool check(ll a,ll b){
return (tin[a] <= tin[b] && tout[b] <= tout[a]);
}
ll LCA(ll a,ll b){
if(check(a, b))return a;
if(check(b, a))return b;
for(int j=19;j>=0;j--){
ll u = up[j][a];
if(!u || check(u, b))continue;
a = u;
}
return up[0][a];
}
ll dist(ll a,ll b){
ll c = LCA(a, b);
return d[a] + d[b] - 2*d[c];
}
void YF_MAIN(ll TEST){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
p[a[i]] = i;
}
for(int i=1;i<n;i++){
ll v, u;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
DSU dsu(n);
for(int i=1;i<=n;i++)dsu.mx[i] = a[i];
pre_calc(1, 1);
// for(int i=1;i<=n;i++){
// for(int j=1;j<i;j++){
// cout<<i<<" "<<j<<" "<<dist(i, j)<<" q\n";
// }
// }
ll dp[n+1]{};
for(int v=1; v<=n; v++){
ll i = p[v];
for(auto j : g[i]){
ll to = dsu.get_mx(j);
// cout<<v<<" "<<to<<" :\n";
if(to > v)continue;
MAX(dp[v], dp[to] + dist(p[v], p[to]));
// cout<<v<<" "<<to<<", "<<dp[v]<<" "<<dp[to]<<", "<<i<<" "<<j<<", "<<dist(p[v], p[to])<<"\n";
dsu.up(i, j);
}
}
cout<<dp[n];
}
const bool TECT=0;
const bool FLSH=1;
const ll SN=1e0 + 7; ll SM[SN];
const ll FN=1e0 + 7; ll FACT[FN], inv[FN], FMOD=inf;
ll PER(ll n,ll k){return FACT[n] *inv[n-k]%FMOD;}
ll CNK(ll n,ll k){return PER(n,k)*inv[k ]%FMOD;}
void BEFORE(){
for(ll i=2;i<SN;i++){
if(SM[i])continue;
for(ll j=i;j<SN;j+=i)
MAX(SM[j],i);
}
FACT[0]=inv[0]=1;
for(int i=1;i<FN;i++){
FACT[i]=FACT[i-1]*i%FMOD;
inv[i]=BP(FACT[i],FMOD-2,FMOD);
}
}
signed main(){
// freopen(("input.txt"),"r",stdin);freopen(("output.txt"),"w",stdout);
if(FLSH){
ios_base::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(0);
cout.tie(0);
cin.tie(0);
}
cout<<(0 && LOCAL ? "\nYF_OUTPUT:\n\n" : "");
int TEST=1;
if(TECT)
cin>>TEST;
BEFORE();
for(int T=1;T<=TEST;T++){
// cout<<"Case "<<T<<": ";
YF_MAIN(T);
cout<<(T==TEST ? "" : "\n");
cout<<flush;
}
return 0;
}
// YF YUSUF ```
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |