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 Rust (1.42.0) (judge verdict: Accepted)

use proconio::input;

#[allow(unused_imports)]
use proconio::marker::{Bytes, Chars, Usize1};
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::ops::*;

#[derive(Clone, Debug, Default)]
struct Struct;

//#[proconio::fastout]
fn main() {
    input! {
        n: usize,
        c: [Usize1;n],
        ab:[(Usize1,Usize1);n-1],
    }

    let mut v = vec![Vec::new(); n];
    for &p in &ab {
        v[p.0].push(p.1);
        v[p.1].push(p.0);
    }

    struct Ctx {
        c: Vec<usize>,
        color_ct: Vec<Vec<i64>>,
        color_sizes: Vec<Vec<i64>>,
        clock: i64,
    }
    let mut ctx = Ctx {
        c,
        color_ct: vec![vec![0; 1]; n], // 他の部分木内で過ごした時間
        color_sizes: vec![Vec::new(); n],
        clock: 0,
    };

    /*
       ノード数Nの木の、全てのパスの数 = N*(N+1)/2
       色kを通るパスの数 = 全てのパスの数 - 色kを通らないパスの数

       木を、色kを含まない複数の部分木(ノード数 s1, s2, ... sm)に分ける。
       色kを通らないパスの数 = Σ si*(si+1)/2

       部分木のサイズをどうやって求めるか？
       dfsで
       部分木のサイズ = 子ノードに入ってから出るまでの時間 - Σ 子ノードの先の他の部分木に入ってから出るまでの時間
    */
    impl Ctx {
        fn dfs(&mut self, node: usize, parent: usize, v: &Vec<Vec<usize>>) {
            self.clock += 1;
            let color = self.c[node];
            let start_clock = self.clock;

            for &next_node in &v[node] {
                if next_node != parent {
                    let start_clock = self.clock;
                    self.color_ct[color].push(0);
                    self.dfs(next_node, node, v);
                    let ct = self.color_ct[color].pop().unwrap();
                    self.color_sizes[color].push(self.clock - start_clock - ct);
                }
            }

            *self.color_ct[color].last_mut().unwrap() += 1 + self.clock - start_clock;
        }
    }

    ctx.dfs(0, n, &v);
    for i in 0..n {
        ctx.color_sizes[i].push(n as i64 - ctx.color_ct[i][0]);
    }

    for i in 0..n {
        let mut ans = n as i64 * (n + 1) as i64 / 2;
        //print!(" i={} ", i);
        for &size in &ctx.color_sizes[i] {
            let size = size as i64;
            ans -= size * (size + 1) / 2;
            //print!(" s={} ", size);
        }
        println!("{} ", ans);
    }
}