#include <bits/stdc++.h>
using namespace std;

#define int           long long
#define S             second
#define F             first
#define pb            push_back
#define all(c)        (c).begin(),(c).end()
#define rall(c)       (c).rbegin(),(c).rend() 
#define lb            lower_bound
#define si(c)         (int)((c).size())
#define lcm(a, b)      (a * (b / __gcd(a,b)))  
#define inf           (long long)(1e15)
#define endl          '\n'
#define mp            make_pair
#define time(s)       (double(clock()-s)/double(CLOCKS_PER_SEC))
#define debug(args...)    _F(#args, args)
#define vi            std::vector<int>
#define pii           pair<int, int> 
#define vpi           vector<pii>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template<typename T> void _F(const char *name, T arg1){
    cerr << name << " = " << arg1 << endl;
}template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args){
    const char *name = strchr(names, ',');
    cerr.write(names, name-names) << " = " << arg1 << endl;
    _F(name+2, args...);
}
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2> &q){
    in >> q.F >> q.S; return in;
}template<typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> &q){
    out << q.F << " " << q.S; return out;
}template< typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){
    return {p1.F+p2.F, p1.S+p2.S};
}template< typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){
    return {p1.F-p2.F, p1.S-p2.S};
}

const int N = 2e5+1;
vi g[N], vis(N), c(N), ans(N), tin(N), tot(N), sub(N), tout(N);
stack<pii> q[N];
int n, u, v, t;

void dfs(int v, int par){
    vis[v] = 1;
    tin[v] = ++t;
    sub[v]++;
    for(auto& u : g[v]){
        if(vis[u])continue;
        dfs(u, v);
        sub[v] += sub[u];
    }
    tout[v] = ++t;
    vpi vp;
    vi vv;
    int sum = 0, x = 0;
    while(!q[c[v]].empty() && q[c[v]].top().F > tin[v]){
        int u = q[c[v]].top().S;
        sum += sub[u];
        vp.pb({tin[u], u});
        vv.pb(tin[u]);
        q[c[v]].pop();
    }
    sort(all(vv));
    sort(all(vp));
    tot[c[v]] -= sum;
    ans[c[v]] += (sub[v]-sum)*(n-sub[v]+1-tot[c[v]]);
    q[c[v]].push({tin[v], v});
    sum++;
    for(auto u : g[v]){
        if(u == par)continue;
        int i = (int)(lb(all(vv), tin[u])-begin(vv));
        int j = (int)(lb(all(vv), tout[u])-begin(vv))-1;
        int tmp = 0;
        for(int k = i; k <= j; k++){
            tmp += sub[vp[k].S];
        }
        x += (sub[v]-sum-(sub[u]-tmp))*(sub[u]-tmp);
    }
    ans[c[v]] += x/2;
    tot[c[v]] += sub[v];
}

void solve(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> c[i];
    }
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        g[u].pb(v), g[v].pb(u);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i++){
        cout << ans[i] << endl;
    }
}

signed main(){

    ios_base::sync_with_stdio(0); 
    cin.tie(0); cout.tie(0);   

    int t = 1;
    start = clock(); 

    cout << fixed << setprecision(12);

    //#ifndef ONLINE_JUDGE
    //freopen("input.txt", "r", stdin); 
    //freopen("output.txt", "w", stdout);
    //freopen("error.txt", "w", stderr);
    //#endif

    //cin >> t;

    for(int i = 1; i <= t; ++i){
        solve();
        //cout << endl;
    }
    //cerr << time(start);
    return 0;
}

Measured memory_bytes: 203252