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: Runtime Error)

// https://atcoder.jp/contests/abc163/tasks/abc163_f
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===


type Graph = Vec<Vec<usize>>;

fn dfs_cpo(g: &Graph, root: usize) -> (Vec<usize>, Vec<Option<usize>>, Vec<usize>) {
    let n = g.len();
    struct Cpo {
        count: Vec<usize>,
        parent: Vec<Option<usize>>,
        order: Vec<usize>,
        oidx: usize
    };
    let mut cpo = Cpo {
        count: vec![0; n],
        parent: vec![None; n],
        order: vec![0; n],
        oidx: 0
    };

    fn dfs(g: &Graph, now: usize, par: Option<usize>, cpo: &mut Cpo) {
        for &to in &g[now] {
            if Some(to) != par {
                dfs(g, to, Some(now), cpo);
            }
        }
        cpo.count[now] = 1;
        for &to in &g[now] {
            if Some(to) != par {
                cpo.count[now] += cpo.count[to];
            }
        }
        cpo.parent[now] = par;
        cpo.order[cpo.oidx] = now;
        cpo.oidx += 1;

    }
    dfs(&g, root, None, &mut cpo);
    (cpo.count, cpo.parent, cpo.order)
}
//===


fn dfs(g: &Graph, now: usize, par: Option<usize>, color: &Vec<usize>, color_parent: &mut Vec<(usize,usize)>, last_color: &mut Vec<usize>, last_color_idx: &mut Vec<usize>) {
    let n = g.len();
    let c = color[now];
    if last_color[c] == n {
        color_parent[now] = (now, 0);
    } else {
        color_parent[now] = (last_color[c], last_color_idx[c]);
    }

    let prev = last_color[c];
    last_color[c] = now;
    for (idx, &to) in g[now].iter().enumerate() {
        if Some(to) == par {
            continue;
        }
        let prev = last_color[c];
        let prev2 = last_color_idx[c];
        last_color[c] = now;
        last_color_idx[c] = idx;

        dfs(g, to, Some(now), color, color_parent, last_color, last_color_idx);

        last_color[c] = prev;
        last_color_idx[c] = prev2;
    }
}

fn main() {
    input! {
        n: usize,
        c: [usize1; n],
        edges: [(usize1, usize1); n-1]
    };

    let mut g = vec![vec![]; n];

    for (u, v) in edges {
        g[u].push(v);
        g[v].push(u);
    }

    let mut nw = n as i64;

    let mut component_list = vec![vec![]; n];
    let mut answer = vec![(nw*(nw+1))/2; n];
    let mut leftover = vec![0; n];

    let mut total_removed = vec![];
    for i in 0..n {
        total_removed.push(vec![0; g[i].len()]);
    }
    let (children, parent, order) = dfs_cpo(&g, 0);
    let mut color_parent = vec![(0,0); n];
    let mut last_color = vec![n; n];
    let mut last_color_idx = vec![0; n];

    dfs(&g, 0, None, &c, &mut color_parent, &mut last_color, &mut last_color_idx);

    for i in 0..n {
        let v = order[i];
        let col = c[v];

        for (idx, &to) in g[v].iter().enumerate() {
            if Some(to) == parent[v] {
                continue;
            }
            if children[to] > total_removed[v][idx] {
                component_list[col].push(children[to]-total_removed[v][idx]);
                let sz = (children[to]-total_removed[v][idx]) as i64;
                answer[col] -= sz*(sz+1)/2;
            }
        }

        let (cp, idx) = color_parent[v];
        if cp == v {
            leftover[col] += children[v];
        } else {
            total_removed[cp][idx] += children[v];
        }
    }

    for i in 0..n {
        let lo = (n-leftover[i]) as i64;
        answer[i] -= lo * (lo+1)/2;
        println!("{}", answer[i]);
    }

    // debug!(component_list);
}