Language: C++

#include <bits/stdc++.h>
#define nl '\n'
#define ar eventsay
#define AC ios_base::sync_with_stdio(0); cin.tie(NULL);
using ll = long long;
using namespace std;
const int N = 2*1e5;
int n;
ll val[N+5];
int ans[N+5];
bool visited[N+5];
vector<int> adj[N+5];

struct cmp{
    bool operator()(const stack<ll>& a, const stack<ll>& b){
        return a.top() < b.top();
    }
};
set<stack<ll>, cmp> S;

void dfs(int cur){
    //cout<<"visiting "<<cur<<endl;
    auto big_it = S.end();
    big_it--;
    ll biggest = (*big_it).top();
    //cout<<biggest.top()<<endl;
    stack<ll> tmp; tmp.push(val[cur]);
    if(val[cur] > biggest)
        S.insert(tmp);
    else{
        auto it = S.lower_bound(tmp);
        auto stktmp = *it;
        stktmp.push(val[cur]);
        S.erase(it);
        S.insert(stktmp);
    }
    ans[cur] = S.size();

    visited[cur] = true;
    for(int v : adj[cur]){
        if(!visited[v]){
            dfs(v);
        }
    }

    auto kk = S.find(tmp);
    if((*kk).size()==1)
        S.erase(kk);
    else{
        auto stktmp = *kk;
        stktmp.pop();
        S.erase(kk);
        S.insert(stktmp);
    }
}

int main(){
    AC
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>val[i];
    for(int i=1; i<=n-1; i++){
        int a, b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    stack<ll> tmp; tmp.push(val[1]);
    S.insert(tmp);
    visited[1] = true;
    ans[1] = 1;
    for(int v : adj[1])
        dfs(v);

    for(int i=1; i<=n; i++){
        cout<<ans[i]<<endl;
    }
        
}

Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::stack<long long int>; _Val = std::stack<long long int>; _KeyOfValue = std::_Identity<std::stack<long long int> >; _Compare = cmp; _Alloc = std::allocator<std::stack<long long int> >; _Const_Link_type = const std::_Rb_tree_node<std::stack<long long int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::stack<long long int>; _Val = std::stack<long long int>; _KeyOfValue = std::_Identity<std::stack<long long int> >; _Compare = cmp; _Alloc = std::allocator<std::stack<long long int> >; key_type = std::stack<long long int>]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::stack<long long int>&; _Key = std::stack<long long int>; _Val = std::stack<long long int>; _KeyOfValue = std::_Identity<std::stack<long long int> >; _Compare = cmp; _Alloc = std::allocator<std::stack<long long int> >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::stack<long long int>; _Compare = cmp; _Alloc = std::allocator<std::stack<long long int> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::stack<long long int>, std::stack<long long int>, std::_Identity<std::stack<long long int> >, cmp, std::allocator<std::stack<long long int> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::stack<long long int> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::stack<long long int> >, std::stack<long long int> >::rebind<std::stack<long long int> >; typename _Alloc::value_type = std::stack<long long int>; value_type = std::stack<long long int>]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:29:17:   required from here
   29 |         S.insert(tmp);
      |         ~~~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const cmp&, const std::stack<long long int, std::deque<long long int, std::allocator<long long int> > >&, const std::stack<long long int, std::deque<long long int, std::allocator<long long int> > >&>' evaluates to false