#pragma GCC optimize("O3", "unroll-loops")
//#pragma GCC target("avx2")
#pragma optimize("fast-math")

#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
#define endl '\n'
#define ull unsigned long long
#define si short int
#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int, int>
#define pdd pair<double, double>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define piii pair<pair<int, int>, int>
#define all(vec) (vec).begin(), (vec).end()
#define piiii pair<pair<int, int>, pair<int, int>>
vector<int> color(2e5+1);
vector<vector<int>> vert(2e5+1, vector<int>(0));
vector<ll> ans(2e5+1);
vector<bool> was(2e5+1);
int n;
vector<int> sons(2e5+1);
vector<int> hgt(2e5+1);
vector<vector<int>> parent(2e5+1, vector<int>(18));

int getHui(int v, int h){
    int h_now=hgt[v];
    if(h_now==h) return v;
    //h_now>h;
    for(int i=17;i>=0;i--){
        if(h_now-(1<<i)<h) continue;
        v=parent[v][i];
        h_now=hgt[v];
    }
    return v;
}


void dfs1(int v, int p){
    was[v]=true;
    hgt[v]=((p!=-1)?hgt[p]:0)+1;
    parent[v][0]=p;
    sons[v]=1;
    for(int i=1;i<18;i++){
        if(parent[v][i-1]!=-1) parent[v][i]=parent[parent[v][i-1]][i-1];
    }
    for(int son:vert[v]){
        if(was[son]) continue;
        dfs1(son, v);
        sons[v]+=sons[son];
    }
}

vector<int> lowest(2e5+1, 0);
vector<int> kek(2e5+1);
vector<int> seckek(2e5+1);
void dfs(int v){
    was[v]=true;
    if(lowest[color[v]]==0){
        //cout<<v<<" lowest "<<sons[v]<<endl;
        kek[color[v]]-=sons[v];
    }
    else{
        //cout<<v<<" "<<getHui(v,lowest[color[v]]+1)<<endl;
        seckek[getHui(v,lowest[color[v]]+1)]-=sons[v];
    }
    int tmp=lowest[color[v]];
    lowest[color[v]]=hgt[v];
    for(int son:vert[v]){
        if(was[son]) continue;
        dfs(son);
    }
    lowest[color[v]]=tmp;
}

void dfs3(int v){
    was[v]=true;
    for(int son:vert[v]){
        if(was[son]) continue;
        ans[color[v]]-=(ll)seckek[son]*(seckek[son]+1)/2;
        dfs3(son);
    }
}



void solve() {
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>color[i+1];
        kek[i+1]=n;
        ans[i+1]=((ll)n*(n+1))/2;
    }
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        vert[a].pb(b);
        vert[b].pb(a);
    }
    dfs1(1, -1);
    for(int i=0;i<n+1;i++) seckek[i]=sons[i];
    for(int i=0;i<n+1;i++){
        was[i]=false;
    }
    dfs(1);
    for(int i=0;i<n+1;i++){
        was[i]=false;
    }
    dfs3(1);
    for(int i=1;i<n+1;i++){
        //cout<<kek[i]<<" "<<i<<endl;
        ans[i]-=(ll)kek[i]*(kek[i]+1)/2;
    }
    for(int i=0;i<n;i++) cout<<ans[i+1]<<" ";

}

void testcases(int n) {
    n = 1 - n;
    if (!n) cin >> n;
    for (int i = 0; i < n; i++) {
        solve();
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    //freopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt", "r", stdin);
    //f=fopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt", "w");
    //freopen("C:\\Users\\Maximus\\CLionProjects\\Codeforces\\BestTeam\\output.txt", "w", stdout);

    unsigned int beg_time = clock();
    testcases(0);
    // 0 - no t, 1 - with t

    unsigned int end_time = clock();
    //cout << endl << endl << end_time - beg_time;
    return 0;
}

Measured memory_bytes: 56472