let input f = stdin.ReadLine() |> f
let inputs f = stdin.ReadLine().Split() |> Array.map f

let N = input int
let C = inputs int |> Array.map (fun x -> x - 1)
let G = [|for i in 1..N -> [] |]

for _ in 1..N-1 do
    let [|u; v|] = inputs int |> Array.map (fun x -> x - 1)
    G.[u] <- v :: G.[u]
    G.[v] <- u :: G.[v]

let C2 (n : int64) = n * (n + 1L) / 2L

let cnt = Array.create N 0L
let del = Array.create N 0L
let mutable all = 0L
let ans = Array.create N (C2 (int64 N))

let rec dfs u p =
    let L = if p <> -1 then all - del.[C.[p]] else 0L
    for v in G.[u] do
        if v <> p then
            let l = all - del.[C.[u]]
            dfs v u
            let r = all - del.[C.[u]]
            del.[C.[u]] <- del.[C.[u]] + (r - l)
    del.[C.[u]] <- del.[C.[u]] + 1L
    all <- all + 1L
    let R = if p <> -1 then all - del.[C.[p]] else 0L
    if p <> -1 then
        ans.[C.[p]] <- ans.[C.[p]] - C2 (R - L)

dfs 0 (-1)
for i in 0..N-1 do
    ans.[i] <- ans.[i] - C2 (all - del.[i])
    printfn "%d" ans.[i]