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);
    }
}


Measured memory_bytes: 81344