import sys

input = sys.stdin.buffer.readline

sys.setrecursionlimit(505050)

N = int(eval(input()))

C = [int(c)-1 for c in input().split()]

G = [[] for _ in range(N)]

for _ in range(N-1):

    a, b = list(map(int, input().split()))

    a -= 1

    b -= 1

    G[a].append(b)

    G[b].append(a)



Cnt = [0] * N

Ans = [0] * N

Siz = [0] * N



def dfs1(v):

    siz = 1

    for u in G[v]:

        G[u].remove(v)

        siz += dfs1(u)

    Siz[v] = siz

    return siz



#Cnt_down = [0] * N



def dfs(v):

    color = C[v]

    cnt_old = Cnt[color]

    siz = 1

    siz_up = N

    #cnt_down = 0

    for u in G[v]:

        #Cnt_down[color] = 0

        Cnt[color] = N - Siz[u]  # 上を塞ぐ

        s = dfs(u)

        siz += s

        siz_up -= s

        Ans[color] += s * (siz_up-cnt_old)

        #cnt_down += Cnt_down[color]

    Ans[color] += siz_up - cnt_old

    #print(siz_up, cnt_old, cnt_down)

    Cnt[color] = cnt_old + siz

    #print(v, Ans)

    return siz



dfs1(0)

dfs(0)

print(("\n".join(map(str, Ans))))

def numba_compile(numba_config):

    import os, sys

    if sys.argv[-1] == "ONLINE_JUDGE":

        from numba import njit

        from numba.pycc import CC

        cc = CC("my_module")

        for func, signature in numba_config:

            globals()[func.__name__] = njit(signature)(func)

            cc.export(func.__name__, signature)(func)

        cc.compile()

        exit()

    elif os.name == "posix":

        exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")

        for func, _ in numba_config:

            globals()[func.__name__] = vars()[func.__name__]

    else:

        from numba import njit

        for func, signature in numba_config:

            globals()[func.__name__] = njit(signature, cache=True)(func)

        print("compiled!", file=sys.stderr)



import numpy as np



def dfs1(v, G, Siz):

    siz = 1

    for u in G[v]:

        G[u].remove(v)

        siz += dfs1(u, G, Siz)

    Siz[v] = siz

    return siz



def dfs(v, N, C, G, Cnt, Siz, Ans):

    color = C[v]

    cnt_old = Cnt[color]

    siz = 1

    siz_up = N

    for u in G[v]:

        Cnt[color] = N - Siz[u]  # 上を塞ぐ

        s = dfs(u, N, C, G, Cnt, Siz, Ans)

        siz += s

        siz_up -= s

        Ans[color] += s * (siz_up - cnt_old)

    Ans[color] += siz_up - cnt_old

    Cnt[color] = cnt_old + siz

    return siz



def solve(N, C, AB):

    G = [[0]*0 for _ in range(N)]

    for i in range(N-1):

        a, b = AB[i]

        G[a].append(b)

        G[b].append(a)

    Cnt = np.zeros(N, dtype=np.int64)

    Ans = np.zeros(N, dtype=np.int64)

    Siz = np.zeros(N, dtype=np.int64)

    dfs1(0, G, Siz)

    dfs(0, N, C, G, Cnt, Siz, Ans)

    return Ans



numba_compile([

    [dfs1, "i8(i8,List(List(i8)),i8[:])"],

    [dfs, "i8(i8,i8,i8[:],List(List(i8)),i8[:],i8[:],i8[:])"],

    [solve, "i8[:](i8,i8[:],i8[:,:])"],

])



import sys

input = sys.stdin.buffer.readline

sys.setrecursionlimit(505050)

N = int(input())

C = np.array(input().split(), dtype=np.int64) - 1

AB = np.array([list(map(int, input().split())) for _ in range(N-1)], dtype=np.int64) - 1

Ans = solve(N, C, AB)

print("\n".join(map(str, Ans)))

import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(505050)
N = int(eval(input()))
C = [int(c) - 1 for c in input().split()]
G = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
Cnt = [0] * N
Ans = [0] * N
Siz = [0] * N


def dfs1(v):
    siz = 1
    for u in G[v]:
        G[u].remove(v)
        siz += dfs1(u)
    Siz[v] = siz
    return siz


# Cnt_down = [0] * N
def dfs(v):
    color = C[v]
    cnt_old = Cnt[color]
    siz = 1
    siz_up = N
    # cnt_down = 0
    for u in G[v]:
        # Cnt_down[color] = 0
        Cnt[color] = N - Siz[u]  # 上を塞ぐ
        s = dfs(u)
        siz += s
        siz_up -= s
        Ans[color] += s * (siz_up - cnt_old)
        # cnt_down += Cnt_down[color]
    Ans[color] += siz_up - cnt_old
    # print(siz_up, cnt_old, cnt_down)
    Cnt[color] = cnt_old + siz
    # print(v, Ans)
    return siz


dfs1(0)
dfs(0)
print(("\n".join(map(str, Ans))))

def numba_compile(numba_config):
    import os, sys

    if sys.argv[-1] == "ONLINE_JUDGE":
        from numba import njit
        from numba.pycc import CC

        cc = CC("my_module")
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature)(func)
            cc.export(func.__name__, signature)(func)
        cc.compile()
        exit()
    elif os.name == "posix":
        exec(
            f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}"
        )
        for func, _ in numba_config:
            globals()[func.__name__] = vars()[func.__name__]
    else:
        from numba import njit

        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature, cache=True)(func)
        print("compiled!", file=sys.stderr)


import numpy as np


def dfs1(v, G, Siz):
    siz = 1
    for u in G[v]:
        G[u].remove(v)
        siz += dfs1(u, G, Siz)
    Siz[v] = siz
    return siz


def dfs(v, N, C, G, Cnt, Siz, Ans):
    color = C[v]
    cnt_old = Cnt[color]
    siz = 1
    siz_up = N
    for u in G[v]:
        Cnt[color] = N - Siz[u]  # 上を塞ぐ
        s = dfs(u, N, C, G, Cnt, Siz, Ans)
        siz += s
        siz_up -= s
        Ans[color] += s * (siz_up - cnt_old)
    Ans[color] += siz_up - cnt_old
    Cnt[color] = cnt_old + siz
    return siz


def solve(N, C, AB):
    G = [[0] * 0 for _ in range(N)]
    for i in range(N - 1):
        a, b = AB[i]
        G[a].append(b)
        G[b].append(a)
    Cnt = np.zeros(N, dtype=np.int64)
    Ans = np.zeros(N, dtype=np.int64)
    Siz = np.zeros(N, dtype=np.int64)
    dfs1(0, G, Siz)
    dfs(0, N, C, G, Cnt, Siz, Ans)
    return Ans


numba_compile(
    [
        [dfs1, "i8(i8,List(List(i8)),i8[:])"],
        [dfs, "i8(i8,i8,i8[:],List(List(i8)),i8[:],i8[:],i8[:])"],
        [solve, "i8[:](i8,i8[:],i8[:,:])"],
    ]
)
import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(505050)
N = int(input())
C = np.array(input().split(), dtype=np.int64) - 1
AB = (
    np.array([list(map(int, input().split())) for _ in range(N - 1)], dtype=np.int64)
    - 1
)
Ans = solve(N, C, AB)
print("\n".join(map(str, Ans)))

+def numba_compile(numba_config): +    import os, sys + +    if sys.argv[-1] == "ONLINE_JUDGE": +        from numba import njit +        from numba.pycc import CC + +        cc = CC("my_module") +        for func, signature in numba_config: +            globals()[func.__name__] = njit(signature)(func) +            cc.export(func.__name__, signature)(func) +        cc.compile() +        exit() +    elif os.name == "posix": +        exec( +            f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}" +        ) +        for func, _ in numba_config: +            globals()[func.__name__] = vars()[func.__name__] +    else: +        from numba import njit + +        for func, signature in numba_config: +            globals()[func.__name__] = njit(signature, cache=True)(func) +        print("compiled!", file=sys.stderr) + + +import numpy as np + + +def dfs1(v, G, Siz): +    siz = 1 +    for u in G[v]: +        G[u].remove(v) +        siz += dfs1(u, G, Siz) +    Siz[v] = siz +    return siz + + +def dfs(v, N, C, G, Cnt, Siz, Ans): +    color = C[v] +    cnt_old = Cnt[color] +    siz = 1 +    siz_up = N +    for u in G[v]: +        Cnt[color] = N - Siz[u]  # 上を塞ぐ +        s = dfs(u, N, C, G, Cnt, Siz, Ans) +        siz += s +        siz_up -= s +        Ans[color] += s * (siz_up - cnt_old) +    Ans[color] += siz_up - cnt_old +    Cnt[color] = cnt_old + siz +    return siz + + +def solve(N, C, AB): +    G = [[0] * 0 for _ in range(N)] +    for i in range(N - 1): +        a, b = AB[i] +        G[a].append(b) +        G[b].append(a) +    Cnt = np.zeros(N, dtype=np.int64) +    Ans = np.zeros(N, dtype=np.int64) +    Siz = np.zeros(N, dtype=np.int64) +    dfs1(0, G, Siz) +    dfs(0, N, C, G, Cnt, Siz, Ans) +    return Ans + + +numba_compile( +    [ +        [dfs1, "i8(i8,List(List(i8)),i8[:])"], +        [dfs, "i8(i8,i8,i8[:],List(List(i8)),i8[:],i8[:],i8[:])"], +        [solve, "i8[:](i8,i8[:],i8[:,:])"], +    ] +) -N = int(eval(input())) -C = [int(c) - 1 for c in input().split()] -G = [[] for _ in range(N)] -for _ in range(N - 1): -    a, b = list(map(int, input().split())) -    a -= 1 -    b -= 1 -    G[a].append(b) -    G[b].append(a) -Cnt = [0] * N -Ans = [0] * N -Siz = [0] * N - - -def dfs1(v): -    siz = 1 -    for u in G[v]: -        G[u].remove(v) -        siz += dfs1(u) -    Siz[v] = siz -    return siz - - -# Cnt_down = [0] * N -def dfs(v): -    color = C[v] -    cnt_old = Cnt[color] -    siz = 1 -    siz_up = N -    # cnt_down = 0 -    for u in G[v]: -        # Cnt_down[color] = 0 -        Cnt[color] = N - Siz[u]  # 上を塞ぐ -        s = dfs(u) -        siz += s -        siz_up -= s -        Ans[color] += s * (siz_up - cnt_old) -        # cnt_down += Cnt_down[color] -    Ans[color] += siz_up - cnt_old -    # print(siz_up, cnt_old, cnt_down) -    Cnt[color] = cnt_old + siz -    # print(v, Ans) -    return siz - - -dfs1(0) -dfs(0) -print(("\n".join(map(str, Ans)))) +N = int(input()) +C = np.array(input().split(), dtype=np.int64) - 1 +AB = ( +    np.array([list(map(int, input().split())) for _ in range(N - 1)], dtype=np.int64) +    - 1 +) +Ans = solve(N, C, AB) +print("\n".join(map(str, Ans)))