#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ll int
typedef pair<int,int> pi;
typedef pair<ll,ll> pill;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF (ll)1e18
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
typedef pair <pi,pi> pipi;
#define maxn 500010
int N,M,K,sr,sc,gr,gc;
int dp[maxn],A[maxn],B[maxn],best[maxn];
vi adj[maxn];
void dfs(int x,int p, int h, int v){
if (best[x] > v) return;
best[x] = v;
aFOR(i,adj[x]) if (i != p && A[i] < h) dfs(i,x,h,v+1);
}
int32_t main(){
fast;
cin >> N;
FOR(i,1,N) cin >> A[i];
FOR(i,1,N) B[A[i]] = i;
int a,b;
FOR(i,1,N-1){
cin >> a >> b;
adj[a].pb(b); adj[b].pb(a);
}
FOR(i,1,N){
int x = B[i];
dp[x] = 0;
aFOR(j, adj[x]) if (A[j] < i){
dp[x] = max(dp[x], best[j] + 1);
}
dfs(x,-1,i,dp[x]);
}
//~ FOR(i,1,N) cout << dp[i] << ' ';
//~ cout << '\n';
cout << dp[B[N]];
}
# | 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... |