#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

int N;
int C[200100];
std::vector<int> G[200100];
int par[200100], size[200100];
std::vector<int> order;
std::set<P> se[200100];
ll res[200100];
int cnt[200100];

void dfs(int v, int p){
    par[v] = p;
    size[v] = 1; // v itself

    for(int w : G[v]){
        if(w != p){
            dfs(w, v);
            size[v] += size[w];
        }
    }
    
    order.emplace_back(v);
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N;

    for(int i=1;i<=N;++i){
        std::cin >> C[i];
    }

    for(int i=0;i<N-1;++i){
        int a, b;
        std::cin >> a >> b;

        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    
    dfs(1, -1);

    for(int i=0;i<N;++i){
        int v = order[i];
        int c = C[v];

        // std::cout << v << ":" << std::endl;
        
        ll sum = 0, sq_sum = 0;

        for(int w : G[v]){
            if(w == par[v]){continue;}

            int n = size[w];
            
            {
                auto it = se[w].lower_bound(std::make_tuple(c, 0));
                    
                if(it != se[w].end()){
                    if(auto [d, m] = *it; d == c){
                        n -= m;
                    }
                }
            }
            
            sum += n;
            sq_sum += 1ll * n * n;
        }

        {
            int n = (N - size[v]) - (cnt[c] - (size[v] - 1 - sum));
            sum += n;
            sq_sum += 1ll * n * n;
        }

        res[c] += sum + 1;
        res[c] += (sum * sum - sq_sum) / 2;

        for(int w : G[v]){
            if(w == par[v]){continue;}
            
            // std::cout << "?" << w << std::endl;

            if(se[v].size() < se[w].size()){
                std::swap(se[v], se[w]);
            }

            for(auto [d, n] : se[w]){ // merge se[v] and se[w]
                // std::cout << "?" << d << ", " << n << std::endl;
                auto it = se[v].lower_bound(std::make_tuple(d, 0));
                    
                bool exist = true;
                    
                if(it == se[v].end()){
                    exist = false;
                }else{
                    auto [e, m] = *it;
                    
                    if(e != d){
                        exist = false;
                    }else{
                        se[v].erase(it);
                        se[v].emplace(d, n + m);
                    }
                }

                unless(exist){
                    se[v].emplace(d, n);
                }
            }
        }
        
        {
            auto it = se[v].lower_bound(std::make_tuple(c, 0));
            
            if(it != se[v].end()){
                if(auto [d, n] = *it; d == c){
                    cnt[c] -= n;
                    se[v].erase(it);
                }
            }
        }
        
        cnt[c] += size[v];
        se[v].emplace(c, size[v]);

        // std::cout << "cnt[c] = " << cnt[c] << std::endl;
        // std::cout << "sum = " << sum << std::endl;
        // std::cout << "res[c] = " << res[c] << std::endl;
        // for(auto [d, n] : se[v]){
        //     std::cout << d << ", " << n << std::endl;
        // }
    }

    for(int i=1;i<=N;++i){
        std::cout << res[i] << std::endl;
    }
}


Measured memory_bytes: 68216