CSES - Shared codeLink to this code: https://cses.fi/paste/a849254a65fcc49e3b5981/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;

// template <class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using namespace std;

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p)
{
    return os << "(" << p.first << ", " << p.second << ") ";
}
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, Container const &x)
{
    os << "[ ";
    for (auto &y : x)
        os << y << ", ";
    return os << "]\n";
}

#define int long long
#define len(a) (int)a.size()
const long long INF = 1e18;
const double EPS = 1e-9;
const int di[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dj[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// dp?, graph?, bs on answer?, compress/sort queries?, stupid observation?
const int N = 2e5 + 5;
map<int, int> freq[N];
vector<int> g[N];
int ans[N], a[N];

void dfs(int u, int par)
{
    freq[u][a[u]] = 1;
    for (int v : g[u])
    {
        if (v == par)
            continue;
        dfs(v, u);
        if (len(freq[v]) > len(freq[u]))
            swap(freq[u], freq[v]);
        for (auto [x, y] : freq[v])
            freq[u][x] += y;
    }
    ans[u] = len(freq[u]);
}

int solve_case()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 0; i < n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, 0);
    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';
    return 0;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    for (int _ = 1; _ <= t; _++)
    {
        solve_case();
    }
    return 0;
}