CSES - Shared codeLink to this code: https://cses.fi/paste/a0d16dafacc8037a61ad2c/
#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
#define IOS ios_base::sync_with_stdio(0);  cin.tie(0); cout.tie(0);

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
typedef pair<int, int>pr;
#define all(i)     i.begin() , i.end()
#define ft     first
#define sn     second
#define pb push_back

#define totalone(mask) __builtin_popcount(mask)
#define chkbit(mask,bit) (mask&(1LL << bit))
#define setbit(mask,bit) (mask|(1LL << bit))
#define cngbit(mask,bit) (mask^(1LL << bit))

#define en "\n"
#define dbg(x) cerr<<#x<<" is : "<<x<<en;
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define report cout<<-1<<en
#define sum(n) ((1LL*(n)*(n+1))/ 2LL)
#define sqr(n) (1LL*(n)*(n))
#define vag(a,b) ((a + b - 1)/b)
#define coutv(v) for(auto i: v) cout<<i<<" ";cout<<en;
#define cinv(v) for(auto &i: v) cin >> i;

const int MAXN = 500005;
#define inf 1e15
const int mod = 1e9 + 7;

vector<ll>g[MAXN];
ll vis[MAXN];
vector<ll>a;
ll v[MAXN];

ll n, q, timer;
int an[MAXN];

set<int> dfs(int nd)
{
    set<int>st;
    st.insert(v[nd]);

    vis[nd] = 1;
    for (auto i : g[nd])
    {
        if (vis[i]) continue;
        set<int>st2 = dfs(i);
        if (st.size() < st2.size()) swap(st, st2);

        for (auto j : st2) st.insert(j);
    }

    an[nd] = st.size();
    return st;
}
void solve()
{
    cin >> n;

    a.pb(0);

    for (ll i = 1; i <= n; i++) {
        cin >> v[i];
    }

    for (ll i = 1; i < n; i++)
    {
        ll x, y;
        cin >> x >> y;
        g[x].pb(y); g[y].pb(x);
    }

    dfs(1);

    for (int i = 1; i <= n; i++) cout << an[i] << " "; cout << en;
}
int main()
{
    IOS;
    ll t;
    t = 1;
    // cin >> t;

    int c = 0;
    while ( t-- )
    {
        // cout<<"Case "<<++c<<": ";
        solve();
    }
    return 0;
}