from typing import List

import sys



n = int(eval(input()))

c = list(map(int, input().split()))

c = [x-1 for x in c]

# print(c)

adj = [[] for i in range(n)]

for i in range(n-1):

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

    a -= 1

    b -= 1

    adj[a].append(b)

    adj[b].append(a)





size: List[int] = [1 for i in range(n)]

stack: List[List[int]] = [[] for color in range(n)]

in_time: List[int] = [-1 for i in range(n)]

out_time: List[int] = [-1 for i in range(n)]

timer = 0

total = n*(n+1)//2

ans: List[int] = [total for color in range(n)]

# print(ans)





def dfs(parent: int, root: int):

    global timer

    in_time[root] = timer

    timer += 1



    for child in adj[root]:

        if parent == child:

            continue

        dfs(root, child)

        size[root] += size[child]



        cnt = size[child]

        while stack[c[root]]:

            x = stack[c[root]][-1]

            if in_time[x] > in_time[root] and out_time[x] != -1:

                cnt -= size[x]

                stack[c[root]].pop()

            else:

                break

        ans[c[root]] -= cnt*(cnt+1)//2



    out_time[root] = timer

    timer += 1



    stack[c[root]].append(root)





sys.setrecursionlimit(10**6)



dfs(0, 0)

# print(size)

for color in range(n):

    cnt = n

    while len(stack[color]) > 0:

        x = stack[color][-1]

        cnt -= size[x]

        stack[color].pop()

    # print("node:", -1, "color:", color, "cnt:", cnt)

    ans[color] -= cnt*(cnt+1)//2

    print((ans[color]))

from typing import List

import sys



input = lambda: sys.stdin.readline().rstrip()

n = int(eval(input()))

c = list(map(int, input().split()))

c = [x-1 for x in c]

# print(c)

adj = [[] for i in range(n)]

for i in range(n-1):

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

    a -= 1

    b -= 1

    adj[a].append(b)

    adj[b].append(a)





size: List[int] = [1 for i in range(n)]

stack: List[List[int]] = [[] for color in range(n)]

in_time: List[int] = [-1 for i in range(n)]

out_time: List[int] = [-1 for i in range(n)]

timer = 0

total = n*(n+1)//2

ans: List[int] = [total for color in range(n)]

# print(ans)





def dfs(parent: int, root: int):

    global timer

    in_time[root] = timer

    timer += 1



    for child in adj[root]:

        if parent == child:

            continue

        dfs(root, child)

        size[root] += size[child]



        cnt = size[child]

        while stack[c[root]]:

            x = stack[c[root]][-1]

            if in_time[x] > in_time[root] and out_time[x] != -1:

                cnt -= size[x]

                stack[c[root]].pop()

            else:

                break

        ans[c[root]] -= cnt*(cnt+1)//2



    out_time[root] = timer

    timer += 1



    stack[c[root]].append(root)





sys.setrecursionlimit(10**6)



dfs(0, 0)

# print(size)

for color in range(n):

    cnt = n

    while len(stack[color]) > 0:

        x = stack[color][-1]

        cnt -= size[x]

        stack[color].pop()

    # print("node:", -1, "color:", color, "cnt:", cnt)

    ans[color] -= cnt*(cnt+1)//2

    print((ans[color]))

from typing import List
import sys

n = int(eval(input()))
c = list(map(int, input().split()))
c = [x - 1 for x in c]
# print(c)
adj = [[] for i in range(n)]
for i in range(n - 1):
    a, b = tuple(map(int, input().split()))
    a -= 1
    b -= 1
    adj[a].append(b)
    adj[b].append(a)
size: List[int] = [1 for i in range(n)]
stack: List[List[int]] = [[] for color in range(n)]
in_time: List[int] = [-1 for i in range(n)]
out_time: List[int] = [-1 for i in range(n)]
timer = 0
total = n * (n + 1) // 2
ans: List[int] = [total for color in range(n)]
# print(ans)
def dfs(parent: int, root: int):
    global timer
    in_time[root] = timer
    timer += 1
    for child in adj[root]:
        if parent == child:
            continue
        dfs(root, child)
        size[root] += size[child]
        cnt = size[child]
        while stack[c[root]]:
            x = stack[c[root]][-1]
            if in_time[x] > in_time[root] and out_time[x] != -1:
                cnt -= size[x]
                stack[c[root]].pop()
            else:
                break
        ans[c[root]] -= cnt * (cnt + 1) // 2
    out_time[root] = timer
    timer += 1
    stack[c[root]].append(root)


sys.setrecursionlimit(10**6)
dfs(0, 0)
# print(size)
for color in range(n):
    cnt = n
    while len(stack[color]) > 0:
        x = stack[color][-1]
        cnt -= size[x]
        stack[color].pop()
    # print("node:", -1, "color:", color, "cnt:", cnt)
    ans[color] -= cnt * (cnt + 1) // 2
    print((ans[color]))

from typing import List
import sys

input = lambda: sys.stdin.readline().rstrip()
n = int(eval(input()))
c = list(map(int, input().split()))
c = [x - 1 for x in c]
# print(c)
adj = [[] for i in range(n)]
for i in range(n - 1):
    a, b = tuple(map(int, input().split()))
    a -= 1
    b -= 1
    adj[a].append(b)
    adj[b].append(a)
size: List[int] = [1 for i in range(n)]
stack: List[List[int]] = [[] for color in range(n)]
in_time: List[int] = [-1 for i in range(n)]
out_time: List[int] = [-1 for i in range(n)]
timer = 0
total = n * (n + 1) // 2
ans: List[int] = [total for color in range(n)]
# print(ans)
def dfs(parent: int, root: int):
    global timer
    in_time[root] = timer
    timer += 1
    for child in adj[root]:
        if parent == child:
            continue
        dfs(root, child)
        size[root] += size[child]
        cnt = size[child]
        while stack[c[root]]:
            x = stack[c[root]][-1]
            if in_time[x] > in_time[root] and out_time[x] != -1:
                cnt -= size[x]
                stack[c[root]].pop()
            else:
                break
        ans[c[root]] -= cnt * (cnt + 1) // 2
    out_time[root] = timer
    timer += 1
    stack[c[root]].append(root)


sys.setrecursionlimit(10**6)
dfs(0, 0)
# print(size)
for color in range(n):
    cnt = n
    while len(stack[color]) > 0:
        x = stack[color][-1]
        cnt -= size[x]
        stack[color].pop()
    # print("node:", -1, "color:", color, "cnt:", cnt)
    ans[color] -= cnt * (cnt + 1) // 2
    print((ans[color]))

+input = lambda: sys.stdin.readline().rstrip()