open Base
open Stdio

let read_line () =
    In_channel.(input_line_exn stdin)

let mapt2 ~f (a, b) = (f a, f b)

let array_replace arr i ~f = arr.(i) <- f (arr.(i))

let n = Int.of_string @@ read_line ()

let sums = Array.create ~len:(n+1) 0
let neg_res = Array.create ~len:(n+1) 0L
let g_hd = Array.create ~len:(n+1) 0
let g_nx = Array.create ~len:(2*n) 0
let g_to = Array.create ~len:(2*n) 0
let g_len = ref 0
let g_add u v =
    g_len := !g_len + 1;
    g_nx.(!g_len) <- g_hd.(u);
    g_to.(!g_len) <- v;
    g_hd.(u) <- !g_len
let g_adj_iter cur x0 f =
    let rec loop e acc = if e = 0 then acc else
        let ch = g_to.(e) in
        loop g_nx.(e) (f ch acc)
    in
    loop g_hd.(cur) x0

let colors = read_line () |> String.split ~on:' ' |> List.map ~f:Int.of_string
    |> List.cons 0 |> Array.of_list

let rec dfs ?(cur=1) ?(par=0) () =
    let memo = sums.(colors.(par)) in
    let tree_size = g_adj_iter cur 1 (fun ch acc ->
        acc + if ch = par then 0 else dfs ~cur:ch ~par:cur ())
    in

    array_replace sums colors.(cur) ((+) 1);
    let component_size = Int64.of_int @@ tree_size - (sums.(colors.(par)) - memo) in
    array_replace neg_res colors.(par) (fun x -> Int64.(x + component_size * (component_size + 1L) / 2L));
    sums.(colors.(par)) <- tree_size + memo;
    tree_size

let () =
    for i = 0 to n - 2 do
        let a, b = read_line () |> String.lsplit2_exn ~on:' ' |> mapt2 ~f:Int.of_string in
        g_add a b;
        g_add b a
    done

let () =
    ignore (dfs ());
    for i = 1 to n do
        let component_size = Int64.of_int @@ n - sums.(i) in
        let n64 = Int64.of_int n in
        let res = Int64.(n64 * (n64 + 1L) / 2L - component_size * (component_size + 1L) / 2L - neg_res.(i)) in
        printf "%Ld\n" res
    done
