Language: C++

#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
#define INF 2e9

class compare
{
    public:
    bool operator()(pair<int,pair<int,int> > n1,pair<int,pair<int,int> > n2)
    {
        if(n1.first<n2.first)
            return true;
        else if(n1.first==n2.first)
        {
            if(n1.second.second>n2.second.second)
                return true;
            else if(n1.second.second==n2.second.second)
                return n1.second.first<n2.second.first;
        }
        return false;
    }
};

void print(set<pair<int,pair<int,int> >,compare>s)
{
    set<pair<int,pair<int,int> >,compare>:: iterator itr;
    for(itr=s.begin();itr!=s.end();itr++)
        cout << itr->first << "," << itr->second.first << "," << itr->second.second << " ";
    cout << endl; 
}

void print1(map<int,int>m)
{
    map<int,int>:: iterator itr;
    for(itr=m.begin();itr!=m.end();itr++)
        cout << itr->first << "," << itr->second << " ";
    cout << endl;
}

vector<vector<int> >v;
vector<int>col;
set<pair<int,pair<int,int> >,compare>s;
set<pair<int,pair<int,int> >,compare>:: iterator itr;
map<int,int>imp;
multiset<int>:: iterator itr2;
vector<int>ans;
void dfs(int u,int par)
{
    //cout << u << endl;
    itr=s.lower_bound({-col[u],{u,0}});
    pair<int,pair<int,int> >p;
    int t;
    if(itr==s.end())
    {
        t=1;
        p={-col[u],{u,1}};
    }
    else
    {
        if(itr->first==-col[u])
            t=itr->second.second;
        else
            t=itr->second.second+1;
        p= {-col[u],{u,t}};  
    }
    s.insert(p);
    //print(s);
    imp[t]+=1;
    set<pair<int,pair<int,int> >,compare>:: iterator itr1=s.find(p);
    //print1(imp);
    map<int,int>:: reverse_iterator rit;
    rit=imp.rbegin();
    ans[u]=rit->first;
    for(int i=0;i<v[u].size();i++)
    {
        if(v[u][i]!=par)
            dfs(v[u][i],u);
    }
    s.erase(itr1);
    //cout << "here" << endl;
    imp[t]-=1;
    if(imp[t]==0)
        imp.erase(imp.find(t));
}


int main()
{
    fastio;
    int n;
    cin >> n;
    v.resize(n);
    col.resize(n);
    ans.resize(n);
    for(int i=0;i<n;i++)
        cin >> col[i];
    for(int i=1;i<n;i++)
    {
        int x,y;
        cin >> x >> y;
        x-=1;y-=1;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0,-1);
    for(int i=0;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::pair<int, std::pair<int, int> >; _Val = std::pair<int, std::pair<int, int> >; _KeyOfValue = std::_Identity<std::pair<int, std::pair<int, int> > >; _Compare = compare; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; _Const_Link_type = const std::_Rb_tree_node<std::pair<int, std::pair<int, int> > >*]':
/usr/include/c++/14/bits/stl_tree.h:1948:36:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_lower_bound(_Link_type, _Base_ptr, const _Key&) [with _Key = std::pair<int, std::pair<int, int> >; _Val = std::pair<int, std::pair<int, int> >; _KeyOfValue = std::_Identity<std::pair<int, std::pair<int, int> > >; _Compare = compare; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; iterator = std::_Rb_tree<std::pair<int, std::pair<int, int> >, std::pair<int, std::pair<int, int> >, std::_Identity<std::pair<int, std::pair<int, int> > >, compare, std::allocator<std::pair<int, std::pair<int, int> > > >::iterator; _Link_type = std::_Rb_tree_node<std::pair<int, std::pair<int, int> > >*; _Base_ptr = std::_Rb_tree_node_base*]'
 1948 |         if (!_M_impl._M_key_compare(_S_key(__x), __k))
      |                                     ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:1267:30:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::lower_bound(const key_type&) [with _Key = std::pair<int, std::pair<int, int> >; _Val = std::pair<int, std::pair<int, int> >; _KeyOfValue = std::_Identity<std::pair<int, std::pair<int, int> > >; _Compare = compare; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; iterator = std::_Rb_tree<std::pair<int, std::pair<int, int> >, std::pair<int, std::pair<int, int> >, std::_Identity<std::pair<int, std::pair<int, int> > >, compare, std::allocator<std::pair<int, std::pair<int, int> > > >::iterator; key_type = std::pair<int, std::pair<int, int> >]'
 1267 |       { return _M_lower_bound(_M_begin(), _M_end(), __k); }
      |                ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:832:32:   required from 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::lower_bound(const key_type&) [with _Key = std::pair<int, std::pair<int, int> >; _Compare = compare; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; iterator = std::_Rb_tree<std::pair<int, std::pair<int, int> >, std::pair<int, std::pair<int, int> >, std::_Identity<std::pair<int, std::pair<int, int> > >, compare, std::allocator<std::pair<int, std::pair<int, int> > > >::const_iterator; key_type = std::pair<int, std::pair<int, int> >]'
  832 |       { return _M_t.lower_bound(__x); }
      |                ~~~~~~~~~~~~~~~~^~~~~
a.cc:54:22:   required from here
   54 |     itr=s.lower_bound({-col[u],{u,0}});
      |         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/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 compare&, const std::pair<int, std::pair<int, int> >&, const std::pair<int, std::pair<int, int> >&>' evaluates to false