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)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn dfs(
    cur: usize,
    parent: usize,
    idx: &mut usize,
    edges: &Vec<Vec<usize>>,
    in_order: &mut Vec<usize>,
    out_order: &mut Vec<usize>,
    color: &Vec<usize>,
    path_orders: &mut Vec<Vec<usize>>,
    parents: &mut Vec<usize>,
) {
    parents[cur] = parent;
    in_order[cur] = *idx;
    *idx += 1;
    path_orders[color[cur]].push(cur);
    for &to in edges[cur].iter().filter(|&&x| x != parent) {
        dfs(
            to,
            cur,
            idx,
            edges,
            in_order,
            out_order,
            color,
            path_orders,
            parents,
        );
    }
    out_order[cur] = *idx;
}

fn main() {
    let n = read::<usize>();
    let c = read_vec::<usize>()
        .into_iter()
        .map(|x| x - 1)
        .collect::<Vec<_>>();

    let mut edges = vec![vec![]; n];
    for i in 0..n - 1 {
        let v = read_vec::<usize>();
        let (a, b) = (v[0] - 1, v[1] - 1);
        edges[a].push(b);
        edges[b].push(a);
    }
    let mut in_order = vec![0; n];
    let mut out_order = vec![0; n];
    let mut path_orders = vec![vec![]; n];
    let mut parents = vec![0; n];
    dfs(
        0,
        0,
        &mut 0,
        &edges,
        &mut in_order,
        &mut out_order,
        &c,
        &mut path_orders,
        &mut parents,
    );

    let init = n as i64 * (n as i64 + 1) / 2;
    let mut answers = vec![init; n];
    let mut seg = SegTree::new(n, 0i64, |x, y| x + y);
    for i in 0..n {
        seg.update(i, 1);
    }
    for c in 0..n {
        for &cur in path_orders[c].iter().rev() {
            for &to in &edges[cur] {
                if to == parents[cur] {
                    continue;
                }
                let i = in_order[to];
                let o = out_order[to];
                let comb = seg.query(i, o);
                answers[c] -= comb * (comb + 1) / 2;
            }
            let i = in_order[cur];
            let o = out_order[cur];
            let comb = seg.query(i, o);
            seg.update(i, seg.query(i, i + 1) - comb);
        }

        // root
        let i = in_order[0];
        let o = out_order[0];
        let comb = seg.query(i, o);
        answers[c] -= comb * (comb + 1) / 2;

        // roll back
        for &cur in path_orders[c].iter() {
            let i = in_order[cur];
            seg.update(i, 1);
        }
    }

    for ans in answers {
        println!("{}", ans);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[derive(Clone)]
struct SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy + std::fmt::Debug,
{
    n: usize,
    dat: Vec<T>,
    init: T,
    functor: F,
}

impl<T, F> SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy + std::fmt::Debug,
{
    fn new(n: usize, init: T, f: F) -> SegTree<T, F> {
        let mut m = 1;
        // For simplicity, we use 2 ** n sized SegTree.
        while m < n {
            m *= 2;
        }
        SegTree {
            n: m,
            dat: vec![init; 2 * m - 1],
            init: init,
            functor: f,
        }
    }

    // dat[k] = a;
    fn update(&mut self, k: usize, a: T) {
        let mut k = k;
        k += self.n - 1;
        self.dat[k] = a;
        while k > 0 {
            k = (k - 1) / 2;
            self.dat[k] = (self.functor)(self.dat[k * 2 + 1], self.dat[k * 2 + 2]);
        }
    }

    // [a, b)
    fn query(&self, a: usize, b: usize) -> T {
        self.query_inner(a, b, 0, 0, self.n)
    }

    fn query_inner(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.init;
        }
        if a <= l && r <= b {
            return self.dat[k];
        }

        let vl = self.query_inner(a, b, k * 2 + 1, l, (l + r) / 2);
        let vr = self.query_inner(a, b, k * 2 + 2, (l + r) / 2, r);
        (self.functor)(vl, vr)
    }
}

impl<T, F> std::fmt::Debug for SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy + std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SegTree").field("x", &self.dat).finish()
    }
}