import sys

from collections import defaultdict



sys.setrecursionlimit(2 * 10 ** 5)



(N,) = [int(x) for x in input().split()]

colors = [int(x) for x in input().split()]

edges = [[int(x) for x in input().split()] for i in range(N - 1)]

graph = defaultdict(list)

for u, v in edges:

    graph[u].append(v)

    graph[v].append(u)

parent = {1: None}

children = defaultdict(list)





def rootTree(node):

    for nbr in graph[node]:

        if nbr not in parent:

            parent[nbr] = node

            children[node].append(nbr)

            rootTree(nbr)





rootTree(1)





colorlessCount = [0 for c in range(N + 1)]

colorlessTime = [0 for c in range(N + 1)]

t = 0





def buildComps(node):

    global t

    t += 1

    currColor = colors[node - 1]

    colorlessTime[currColor] += 1

    for child in children[node]:

        oldCount = t - colorlessTime[currColor]

        colorlessTime[currColor] = t

        buildComps(child)

        s = t - colorlessTime[currColor]

        colorlessTime[currColor] = t - oldCount

        colorlessCount[currColor] += s * (s + 1) // 2





buildComps(1)



for c in range(1, N + 1):

    s = t - colorlessTime[c]

    colorlessCount[c] += s * (s + 1) // 2

maxPairs = N * (N + 1) // 2

for c in range(1, N + 1):

    print((maxPairs - colorlessCount[c]))

import sys



sys.setrecursionlimit(2 * 10 ** 5)



N = int(eval(input()))

colors = [int(x) for x in input().split()]

edges = [[int(x) for x in input().split()] for i in range(N - 1)]

graph = [[] for i in range(N + 1)]

for u, v in edges:

    graph[u].append(v)

    graph[v].append(u)

parent = {1: None}

children = {}





def rootTree(node):

    children[node] = []

    for nbr in graph[node]:

        if nbr not in parent:

            parent[nbr] = node

            children[node].append(nbr)

            rootTree(nbr)





rootTree(1)





# Count the paths that don't contain a particular color

# If we can find a max c-less component (in the sense that the nodes are either leaves or bordered by color c) of size s, then there are (s * s + 1 / 2) c-less paths in that component

colorlessCount = [0 for c in range(N + 1)]

# In our DFS we want to increment the size of each component except for the one matching current color all at once but that will TLE

# Instead implicitly increment them by tracking current size as dfs time `t` minus some offset. Then we can increment all just by adding to t or subtract from one by fiddling with the offset

colorlessTime = [0 for c in range(N + 1)]

t = 0





def buildComps(node):

    global t

    currColor = colors[node - 1]

    # Increment all by 1 except currColor

    t += 1

    colorlessTime[currColor] += 1

    for child in children[node]:

        # Each child forms a new currColor-less component so we need to save the old count

        oldCount = t - colorlessTime[currColor]

        # Start new comp

        colorlessTime[currColor] = t

        buildComps(child)

        # Done with child comp, count the paths and discard comp

        s = t - colorlessTime[currColor]

        colorlessCount[currColor] += s * (s + 1) // 2

        # Restore old count

        colorlessTime[currColor] = t - oldCount





buildComps(1)



# Count the colorless components path from root too

for c in range(1, N + 1):

    s = t - colorlessTime[c]

    colorlessCount[c] += s * (s + 1) // 2



# We counted colorless but want colored paths so take complement

maxPairs = N * (N + 1) // 2

for c in range(1, N + 1):

    print((maxPairs - colorlessCount[c]))

import sys
from collections import defaultdict

sys.setrecursionlimit(2 * 10**5)
(N,) = [int(x) for x in input().split()]
colors = [int(x) for x in input().split()]
edges = [[int(x) for x in input().split()] for i in range(N - 1)]
graph = defaultdict(list)
for u, v in edges:
    graph[u].append(v)
    graph[v].append(u)
parent = {1: None}
children = defaultdict(list)


def rootTree(node):
    for nbr in graph[node]:
        if nbr not in parent:
            parent[nbr] = node
            children[node].append(nbr)
            rootTree(nbr)


rootTree(1)
colorlessCount = [0 for c in range(N + 1)]
colorlessTime = [0 for c in range(N + 1)]
t = 0


def buildComps(node):
    global t
    t += 1
    currColor = colors[node - 1]
    colorlessTime[currColor] += 1
    for child in children[node]:
        oldCount = t - colorlessTime[currColor]
        colorlessTime[currColor] = t
        buildComps(child)
        s = t - colorlessTime[currColor]
        colorlessTime[currColor] = t - oldCount
        colorlessCount[currColor] += s * (s + 1) // 2


buildComps(1)
for c in range(1, N + 1):
    s = t - colorlessTime[c]
    colorlessCount[c] += s * (s + 1) // 2
maxPairs = N * (N + 1) // 2
for c in range(1, N + 1):
    print((maxPairs - colorlessCount[c]))

import sys

sys.setrecursionlimit(2 * 10**5)
N = int(eval(input()))
colors = [int(x) for x in input().split()]
edges = [[int(x) for x in input().split()] for i in range(N - 1)]
graph = [[] for i in range(N + 1)]
for u, v in edges:
    graph[u].append(v)
    graph[v].append(u)
parent = {1: None}
children = {}


def rootTree(node):
    children[node] = []
    for nbr in graph[node]:
        if nbr not in parent:
            parent[nbr] = node
            children[node].append(nbr)
            rootTree(nbr)


rootTree(1)
# Count the paths that don't contain a particular color
# If we can find a max c-less component (in the sense that the nodes are either leaves or bordered by color c) of size s, then there are (s * s + 1 / 2) c-less paths in that component
colorlessCount = [0 for c in range(N + 1)]
# In our DFS we want to increment the size of each component except for the one matching current color all at once but that will TLE
# Instead implicitly increment them by tracking current size as dfs time `t` minus some offset. Then we can increment all just by adding to t or subtract from one by fiddling with the offset
colorlessTime = [0 for c in range(N + 1)]
t = 0


def buildComps(node):
    global t
    currColor = colors[node - 1]
    # Increment all by 1 except currColor
    t += 1
    colorlessTime[currColor] += 1
    for child in children[node]:
        # Each child forms a new currColor-less component so we need to save the old count
        oldCount = t - colorlessTime[currColor]
        # Start new comp
        colorlessTime[currColor] = t
        buildComps(child)
        # Done with child comp, count the paths and discard comp
        s = t - colorlessTime[currColor]
        colorlessCount[currColor] += s * (s + 1) // 2
        # Restore old count
        colorlessTime[currColor] = t - oldCount


buildComps(1)
# Count the colorless components path from root too
for c in range(1, N + 1):
    s = t - colorlessTime[c]
    colorlessCount[c] += s * (s + 1) // 2
# We counted colorless but want colored paths so take complement
maxPairs = N * (N + 1) // 2
for c in range(1, N + 1):
    print((maxPairs - colorlessCount[c]))

-from collections import defaultdict -(N,) = [int(x) for x in input().split()] +N = int(eval(input())) -graph = defaultdict(list) +graph = [[] for i in range(N + 1)] -children = defaultdict(list) +children = {} +    children[node] = [] +# Count the paths that don't contain a particular color +# If we can find a max c-less component (in the sense that the nodes are either leaves or bordered by color c) of size s, then there are (s * s + 1 / 2) c-less paths in that component +# In our DFS we want to increment the size of each component except for the one matching current color all at once but that will TLE +# Instead implicitly increment them by tracking current size as dfs time `t` minus some offset. Then we can increment all just by adding to t or subtract from one by fiddling with the offset +    currColor = colors[node - 1] +    # Increment all by 1 except currColor -    currColor = colors[node - 1] +        # Each child forms a new currColor-less component so we need to save the old count +        # Start new comp +        # Done with child comp, count the paths and discard comp +        colorlessCount[currColor] += s * (s + 1) // 2 +        # Restore old count -        colorlessCount[currColor] += s * (s + 1) // 2 +# Count the colorless components path from root too +# We counted colorless but want colored paths so take complement