#![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()
    }
}


Measured memory_bytes: 54684