Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.

For each k=1, 2, ..., N, solve the following problem:

Find the number of simple paths that visit a vertex painted in the color k one or more times.

Note: The simple paths from Vertex u to v and from v to u are not distinguished.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq c_i \leq N

1 \leq a_i,b_i \leq N

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print the answers for k = 1, 2, ..., N in order, each in its own line.

Sample Input 1

3
1 2 1
1 2
2 3

Sample Output 1

5
4
0

Let P_{i,j} denote the simple path connecting Vertex i and j.

There are 5 simple paths that visit a vertex painted in the color 1 one or more times:

P_{1,1}\,,\,
P_{1,2}\,,\,
P_{1,3}\,,\,
P_{2,3}\,,\,
P_{3,3}

There are 4 simple paths that visit a vertex painted in the color 2 one or more times:

P_{1,2}\,,\,
P_{1,3}\,,\,
P_{2,2}\,,\,
P_{2,3}

There are no simple paths that visit a vertex painted in the color 3 one or more times.

Sample Input 2

1
1

Sample Output 2

1

Sample Input 3

2
1 2
1 2

Sample Output 3

2
2

Sample Input 4

5
1 2 3 4 5
1 2
2 3
3 4
3 5

Sample Output 4

5
8
10
5
5

Sample Input 5

8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8

Sample Output 5

18
15
0
14
23
0
23
0

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

vector<vector<int>> adj;
vector<vector<pair<int, int>>> et_count;
vector<int> c, count_c, order;

void euler_tour(int n, int p, int &o) {
  order[n] = o;
  et_count[c[n]].push_back(make_pair(order[n],o));
  for (auto x: adj[n]) {
    if (x==p) continue;
    o++; euler_tour(x,n,o);
    et_count[c[n]].push_back(make_pair(order[n],o));
  }
}

int main() {
  int N; cin >> N;
  c.resize(N); rep(i,N) { cin >> c[i]; c[i]--; }
  count_c.resize(N); rep(i,N) count_c[c[i]]++;

  adj.resize(N);
  rep(i,N-1) {
    int a, b; cin >> a >> b; a--; b--;
    adj[a].push_back(b); adj[b].push_back(a);
  }

  et_count.resize(N); order.resize(N);
  int o = 0; euler_tour(0,-1,o);
  // rep(i,N) { 
  //   cout << i << "->"; 
  //   for (auto x: et_count[i]) cout << x.first << "," << x.second << " ";
  //   cout << endl;
  // }

  rep(i,N) {
    ll result = N*(N+1)/2;
    ll left = N - count_c[i];
    // cout << left << endl;
    rep(j,et_count[i].size()-1) {
      if (et_count[i][j].first==et_count[i][j+1].first) {
        ll diff = et_count[i][j+1].second - et_count[i][j].second;
        result -= diff*(diff+1)/2;
        left -= diff;
      }
      else if (et_count[i][j].first<et_count[i][j+1].first) {
        ll diff = et_count[i][j+1].second - et_count[i][j].second - 1;
        result -= diff*(diff+1)/2;
        left -= diff;
      }
      // cout << left << endl;
    }
    result -= left*(left+1)/2;
    cout << result << endl;
  }
  return 0;
}