#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
template <typename X, typename Y> istream& operator>>(istream& is, pair<X, Y>& p) { return is >> p.first >> p.second; }
template <bool Directed, typename TEdge, bool Index> struct graph {
    using EType = TEdge;
    vector<TEdge> edges;
    vector<vector<int>> adj;
    graph(int n) : adj(n + Index) {}
    graph(int n, int m) : graph(n) { edges.reserve(m << not Directed); }
    TEdge& operator()(int e) { return edges[e]; }
    vector<int>& operator[](int u) { return adj[u]; }
    int size() { return adj.size() - Index; }
    void add_edge(const TEdge& e) {
        adj[e.u].push_back(edges.size());
        edges.push_back(e);
        if(not Directed) {
            adj[e.v].push_back(edges.size());
            edges.push_back(e.reverse());
        }
    }
};
struct edge {
    int u, v;
    edge reverse() const { return {v, u}; }
    friend ostream& operator<<(ostream& os, const edge& e) {
        return os << "(" << e.u << ", " << e.v << ")";
    }
};
template <typename T> using f = function<T>;
template <typename TEdge> struct dfs_params {
    f<void(int)> pre_node{[](int) -> void {}};
    f<void(int)> pos_node{[](int) -> void {}};
    f<void(TEdge&)> pre_edge{[](TEdge&) -> void {}};
    f<void(TEdge&)> pos_edge{[](TEdge&) -> void {}};
    int root = 0;
};
template <bool Directed, typename TEdge, bool Index, typename TParamsetter = f<void(dfs_params<TEdge>&)>>
vector<int> dfs(graph<Directed, TEdge, Index>& g, const TParamsetter& psetter = [](dfs_params<TEdge>&) {}) {
    static_assert(is_convertible<decltype(psetter), f<void(dfs_params<TEdge>&)>>::value, "psetter must be void(dfs_params<TEdge>&)");
    dfs_params<TEdge> params;
    psetter(params);
    vector<int> component(g.adj.size());
    function<void(int, int)> dfs = [&](int u, int c) {
        component[u] = c;
        params.pre_node(u);
        for(int e : g[u]) if(not component[g(e).v]) {
            params.pre_edge(g(e)), dfs(g(e).v, c), params.pos_edge(g(e));
        }
        params.pos_node(u);
    };
    if(params.root) {
        dfs(params.root, 1);
    } else {
        for(int u = Index, c = 0; u < g.adj.size(); u++) if(not component[u]) {
            dfs(u, ++c);
        }
    }
    return component;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> c(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> c[i];
    }
    graph<0, edge, 1> g(n, n - 1);
    for(int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g.add_edge({u, v});
    }
    vector<int> subtree_size(n + 1, 1), parent(n + 1);
    vector<vector<int>> most_recent_ceiling_subtree(n + 1, vector<int>(1, n));
    vector<ll> ans(n + 1);
    dfs(g, [&](auto& p) {
        p.pos_edge = [&](edge& e) {
            subtree_size[e.u] += subtree_size[e.v];
            parent[e.v] = e.u;
        };
    });
    dfs(g, [&](auto& p) {
        p.pre_edge = [&](edge& e) {
            most_recent_ceiling_subtree[c[e.u]].push_back(subtree_size[e.v]);
        };
        p.pos_edge = [&](edge& e) {
            most_recent_ceiling_subtree[c[e.u]].pop_back();
        };
        p.pos_node = [&](int u) {
            ll sum_sizes = most_recent_ceiling_subtree[c[u]].back() - subtree_size[u];
            for(int e : g[u]) {
                if(g(e).v != parent[u]) {
                    ans[c[u]] += subtree_size[g(e).v] * sum_sizes;
                    sum_sizes += subtree_size[g(e).v];
                }
            }
            ans[c[u]] += sum_sizes + 1;
            most_recent_ceiling_subtree[c[u]].back() -= subtree_size[u];
        };
    });
    for(int i = 1; i <= n; i++) {
        cout << ans[i] << endl;
    }
    return 0;
}


Measured memory_bytes: 56224