import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

import numpy as np

from numba import njit





def EulerTour(graph, root=1):

    N = len(graph) - 1

    parent = [0] * (N + 1)

    stack = [-root, root]

    tour = []

    ind_L = [0] * (N + 1)

    ind_R = [0] * (N + 1)

    i = -1

    while stack:

        v = stack.pop()

        tour.append(v)

        i += 1

        if v > 0:

            ind_L[v] = i

            p = parent[v]

            for w in graph[v]:

                if w == p:

                    continue

                parent[w] = v

                stack.append(-w)

                stack.append(w)

        else:

            ind_R[-v] = i

    return tour, ind_L, ind_R, parent





N = int(readline())

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

C = np.zeros(N + 1, np.int64)

C[1:] = np.array(readline().split(), np.int64)

m = list(map(int, read().split()))

for a, b in zip(m, m):

    graph[a].append(b)

    graph[b].append(a)



tour, ind_L, ind_R, parent = [np.array(x, np.int64) for x in EulerTour(graph)]





@njit('i8[:](i8[:],i8[:],i8[:],i8[:],i8[:])', cache=True)

def main(C, tour, ind_L, ind_R, parent):

    N = len(C) - 1

    removed = np.zeros(N + 1, np.int64)

    answer = np.full_like(removed, N * (N + 1) // 2)

    memo = np.zeros_like(answer)

    for v in tour:

        if v > 0:

            memo[v] = removed[C[parent[v]]]

        else:

            v = -v

            removed[C[v]] += 1

            p = parent[v]

            x = (ind_R[v] - ind_L[v]) // 2 + 1  # subtree size

            x -= removed[C[p]] - memo[v]

            answer[C[p]] -= x * (x + 1) // 2

            removed[C[p]] += x

    for i, x in enumerate(removed):

        x = N - x

        answer[i] -= x * (x + 1) // 2

    return answer





answer = main(C, tour, ind_L, ind_R, parent)

print(('\n'.join(answer[1:].astype(str))))

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

import numpy as np

from numba import njit





def EulerTour(graph, root=1):

    N = len(graph) - 1

    parent = [0] * (N + 1)

    stack = [-root, root]

    tour = []

    ind_L = [0] * (N + 1)

    ind_R = [0] * (N + 1)

    i = -1

    while stack:

        v = stack.pop()

        tour.append(v)

        i += 1

        if v > 0:

            ind_L[v] = i

            p = parent[v]

            for w in graph[v]:

                if w == p:

                    continue

                parent[w] = v

                stack.append(-w)

                stack.append(w)

        else:

            ind_R[-v] = i

    return tour, ind_L, ind_R, parent





@njit('i8[:](i8[:],i8[:],i8[:],i8[:],i8[:])', cache=True)

def main(C, tour, ind_L, ind_R, parent):

    N = len(C) - 1

    removed = np.zeros(N + 1, np.int64)

    answer = np.full_like(removed, N * (N + 1) // 2)

    memo = np.zeros_like(answer)

    for v in tour:

        if v > 0:

            memo[v] = removed[C[parent[v]]]

        else:

            v = -v

            removed[C[v]] += 1

            p = parent[v]

            x = (ind_R[v] - ind_L[v]) // 2 + 1  # subtree size

            x -= removed[C[p]] - memo[v]

            answer[C[p]] -= x * (x + 1) // 2

            removed[C[p]] += x

    for i, x in enumerate(removed):

        x = N - x

        answer[i] -= x * (x + 1) // 2

    return answer





N = int(readline())

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

C = np.zeros(N + 1, np.int64)

C[1:] = np.array(readline().split(), np.int64)

m = list(map(int, read().split()))

for a, b in zip(m, m):

    graph[a].append(b)

    graph[b].append(a)



tour, ind_L, ind_R, parent = [np.array(x, np.int64) for x in EulerTour(graph)]



answer = main(C, tour, ind_L, ind_R, parent)

print(('\n'.join(answer[1:].astype(str))))

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from numba import njit


def EulerTour(graph, root=1):
    N = len(graph) - 1
    parent = [0] * (N + 1)
    stack = [-root, root]
    tour = []
    ind_L = [0] * (N + 1)
    ind_R = [0] * (N + 1)
    i = -1
    while stack:
        v = stack.pop()
        tour.append(v)
        i += 1
        if v > 0:
            ind_L[v] = i
            p = parent[v]
            for w in graph[v]:
                if w == p:
                    continue
                parent[w] = v
                stack.append(-w)
                stack.append(w)
        else:
            ind_R[-v] = i
    return tour, ind_L, ind_R, parent


N = int(readline())
graph = [[] for _ in range(N + 1)]
C = np.zeros(N + 1, np.int64)
C[1:] = np.array(readline().split(), np.int64)
m = list(map(int, read().split()))
for a, b in zip(m, m):
    graph[a].append(b)
    graph[b].append(a)
tour, ind_L, ind_R, parent = [np.array(x, np.int64) for x in EulerTour(graph)]


@njit("i8[:](i8[:],i8[:],i8[:],i8[:],i8[:])", cache=True)
def main(C, tour, ind_L, ind_R, parent):
    N = len(C) - 1
    removed = np.zeros(N + 1, np.int64)
    answer = np.full_like(removed, N * (N + 1) // 2)
    memo = np.zeros_like(answer)
    for v in tour:
        if v > 0:
            memo[v] = removed[C[parent[v]]]
        else:
            v = -v
            removed[C[v]] += 1
            p = parent[v]
            x = (ind_R[v] - ind_L[v]) // 2 + 1  # subtree size
            x -= removed[C[p]] - memo[v]
            answer[C[p]] -= x * (x + 1) // 2
            removed[C[p]] += x
    for i, x in enumerate(removed):
        x = N - x
        answer[i] -= x * (x + 1) // 2
    return answer


answer = main(C, tour, ind_L, ind_R, parent)
print(("\n".join(answer[1:].astype(str))))

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from numba import njit


def EulerTour(graph, root=1):
    N = len(graph) - 1
    parent = [0] * (N + 1)
    stack = [-root, root]
    tour = []
    ind_L = [0] * (N + 1)
    ind_R = [0] * (N + 1)
    i = -1
    while stack:
        v = stack.pop()
        tour.append(v)
        i += 1
        if v > 0:
            ind_L[v] = i
            p = parent[v]
            for w in graph[v]:
                if w == p:
                    continue
                parent[w] = v
                stack.append(-w)
                stack.append(w)
        else:
            ind_R[-v] = i
    return tour, ind_L, ind_R, parent


@njit("i8[:](i8[:],i8[:],i8[:],i8[:],i8[:])", cache=True)
def main(C, tour, ind_L, ind_R, parent):
    N = len(C) - 1
    removed = np.zeros(N + 1, np.int64)
    answer = np.full_like(removed, N * (N + 1) // 2)
    memo = np.zeros_like(answer)
    for v in tour:
        if v > 0:
            memo[v] = removed[C[parent[v]]]
        else:
            v = -v
            removed[C[v]] += 1
            p = parent[v]
            x = (ind_R[v] - ind_L[v]) // 2 + 1  # subtree size
            x -= removed[C[p]] - memo[v]
            answer[C[p]] -= x * (x + 1) // 2
            removed[C[p]] += x
    for i, x in enumerate(removed):
        x = N - x
        answer[i] -= x * (x + 1) // 2
    return answer


N = int(readline())
graph = [[] for _ in range(N + 1)]
C = np.zeros(N + 1, np.int64)
C[1:] = np.array(readline().split(), np.int64)
m = list(map(int, read().split()))
for a, b in zip(m, m):
    graph[a].append(b)
    graph[b].append(a)
tour, ind_L, ind_R, parent = [np.array(x, np.int64) for x in EulerTour(graph)]
answer = main(C, tour, ind_L, ind_R, parent)
print(("\n".join(answer[1:].astype(str))))

-N = int(readline()) -graph = [[] for _ in range(N + 1)] -C = np.zeros(N + 1, np.int64) -C[1:] = np.array(readline().split(), np.int64) -m = list(map(int, read().split())) -for a, b in zip(m, m): -    graph[a].append(b) -    graph[b].append(a) -tour, ind_L, ind_R, parent = [np.array(x, np.int64) for x in EulerTour(graph)] - - +N = int(readline()) +graph = [[] for _ in range(N + 1)] +C = np.zeros(N + 1, np.int64) +C[1:] = np.array(readline().split(), np.int64) +m = list(map(int, read().split())) +for a, b in zip(m, m): +    graph[a].append(b) +    graph[b].append(a) +tour, ind_L, ind_R, parent = [np.array(x, np.int64) for x in EulerTour(graph)]