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::{fastout, input};

struct Data<'a> {
    open: Vec<usize>,
    close: Vec<usize>,
    tour: Vec<usize>,
    list: Vec<Vec<usize>>,
    g: &'a Vec<Vec<usize>>,
    c: &'a Vec<usize>,
}

impl<'a> Data<'a> {
    fn rec(&mut self, pos: usize, par: usize) {
        let d = self.tour.len();
        let col = self.c[pos];
        self.tour.push(pos);
        self.open[pos] = d;
        self.list[col].push(d);
        for nex in &self.g[pos] {
            if *nex == par {
                continue;
            }
            let d = self.tour.len();
            self.tour.push(pos);
            self.list[col].push(d);

            self.rec(*nex, pos);

            let d = self.tour.len();
            self.tour.push(pos);
            self.list[col].push(d);
        }
        let d = self.tour.len();
        let col = self.c[pos];
        self.tour.push(pos);
        self.close[pos] = d;
        self.list[col].push(d);
    }
}

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

    let c = c.into_iter().map(|x| x - 1).collect();

    let mut g: Vec<Vec<usize>> = vec![vec![]; n];
    for (a, b) in e.iter() {
        let a = a - 1;
        let b = b - 1;
        g[a].push(b);
        g[b].push(a);
    }

    let open: Vec<usize> = vec![!0; n + 1];
    let close: Vec<usize> = vec![!0; n + 1];
    let color_list: Vec<Vec<usize>> = vec![vec![0, 1]; n + 1];
    let tour: Vec<usize> = Vec::new();

    let mut d: Data = Data {
        open,
        close,
        list: color_list,
        tour,
        g: &g,
        c: &c,
    };

    d.tour.push(n);
    d.tour.push(n);
    d.open[n] = 0;

    d.rec(0, n);

    d.tour.push(n);
    for i in 0..n {
        d.list[i].push(d.tour.len() - 1);
    }

    d.tour.push(n);
    d.close[n] = d.tour.len() - 1;
    for i in 0..n {
        d.list[i].push(d.tour.len() - 1);
    }

    let mut sum: Vec<usize> = vec![0; d.tour.len() + 1];
    for (i, pos) in d.tour.iter().enumerate() {
        sum[i + 1] = sum[i];
        if d.open[*pos] == i {
            sum[i + 1] += 1;
        }
    }
    // [l, r)
    let get = |l: usize, r: usize| sum[r] - sum[l];
    let get_subtree_size = |root: usize| get(d.open[root], d.close[root]);
    let mut ans = vec![0 as i64; n];

    // dbg!(&d.tour);
    // dbg!(&d.list);
    // dbg!(&d.open);
    // dbg!(&d.close);
    for i in 0..n {
        let col = i;
        let pos = n;
        let beg = d.list[col][..].binary_search(&d.open[pos]).unwrap();
        let end = d.list[col][..].binary_search(&d.close[pos]).unwrap();
        let mut now = beg + 1;
        // dbg!(beg, end);
        while now != end {
            let mut now2 = now + 1;
            let mut tmp = 0 as i64;
            while true {
                let tour_idx = d.list[col][now2];
                let v = d.tour[tour_idx];
                // dbg!(&v);
                if v == pos {
                    break;
                }
                let k = get_subtree_size(v) as i64;
                // dbg!(&k);
                tmp -= k;
                let nex = d.close[v];
                let w = d.list[col][..].binary_search(&nex).unwrap();
                now2 = w + 1;
            }
            // dbg!(tmp);
            // dbg!(&d.list[col][now + 1]);
            // dbg!(&d.list[col][now2]);
            tmp += sum[d.list[col][now2]] as i64;
            tmp -= sum[d.list[col][now]] as i64;
            // dbg!(tmp);
            let tmp = tmp as i64;
            ans[col] += tmp * (tmp - 1) / 2 + tmp;
            now = now2 + 1;
            // dbg!(now);
        }
    }
    // dbg!(&ans);
    for i in 0..n {
        let pos = i;
        let col = c[pos];
        let beg = d.list[col][..].binary_search(&d.open[pos]).unwrap();
        let end = d.list[col][..].binary_search(&d.close[pos]).unwrap();
        let mut now = beg + 1;
        // dbg!(beg, end);
        while now != end {
            let mut now2 = now + 1;
            let mut tmp = 0 as i64;
            while true {
                let tour_idx = d.list[col][now2];
                let v = d.tour[tour_idx];
                // dbg!(&v);
                if v == pos {
                    break;
                }
                let k = get_subtree_size(v) as i64;
                // dbg!(&k);
                tmp -= k;
                let nex = d.close[v];
                let w = d.list[col][..].binary_search(&nex).unwrap();
                now2 = w + 1;
            }
            // dbg!(tmp);
            // dbg!(&d.list[col][now + 1]);
            // dbg!(&d.list[col][now2]);
            tmp += sum[d.list[col][now2]] as i64;
            tmp -= sum[d.list[col][now]] as i64;
            // dbg!(tmp);
            let tmp = tmp as i64;
            ans[col] += tmp * (tmp - 1) / 2 + tmp;
            now = now2 + 1;
            // dbg!(now);
        }
    }

    // dbg!(&ans);
    let n = n as i64;
    let base = n * (n - 1) / 2 + n;
    for ans in &ans {
        println!("{}", base - ans);
    }
}