open Base
open Stdio

(* Core.read_line is deprecated *)
let read_line () =
    (* Out_channel.(flush stdout); *)
    In_channel.(input_line_exn stdin)

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

let et_size = ref 0

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 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 et_in = !et_size in
    let memo = sums.(colors.(par)) in
    et_size := !et_size + 1;
    let rec loop e = if e = 0 then () else
        let ch = g_to.(e) in
        ((if ch = par then () else dfs ~cur:ch ~par:cur ()); loop g_nx.(e)) in
    loop (g_hd.(cur));
    let et_out = !et_size in
    et_size := !et_size + 1;

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

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 _ =
    dfs ();
    let tree_size = !et_size / 2 in
    for i = 1 to n do
        let component_size = Int64.of_int @@ tree_size - 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
