import sys

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

N = int(input())

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

X = [[] for i in range(N)]

for i in range(N-1):

    x, y = map(int, input().split())

    X[x-1].append(y-1)

    X[y-1].append(x-1)



def EulerTour(n, X, i0):

    f = lambda k: k * (k + 1) // 2

    USED = [0] * n

    ORG = [0] * n

    TMP = [0] * n

    P = [-1] * n

    ct = -1

    ET1 = [0] * n

    ET2 = [0] * n

    ANS = [f(n)] * n

    IND = [len(x) for x in X]

    i = i0

    while i >= 0:

        ind = IND[i]

        if not ind:

            ET2[i] = ct

            USED[C[i]] += 1 + TMP[i]

            if i:

                k = ET2[i] - ET1[i] + 1 - USED[C[P[i]]] + ORG[i]

                ANS[C[P[i]]] -= f(k)

                TMP[P[i]] += k

            i = P[i]

            continue

        

        j = X[i][ind - 1]

        if P[i] == j:

            IND[i] -= 1

            continue

        

        P[j] = i

        IND[i] -= 1

        i = j

        ORG[i] = USED[C[P[i]]]

        ct += 1

        ET1[i] = ct

    

    for i in range(n):

        ANS[i] -= f(n - USED[i])

    return ANS



print(*EulerTour(N, X, 0), sep = "\n")

import sys

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

N = int(input())

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

X = [[] for i in range(N)]

head = [-1] * (N + 1)

to = [0] * (N - 1 << 1)

nxt = [0] * (N - 1 << 1)

for i in range(N-1):

    x, y = map(int, input().split())

    x, y = x-1, y-1

    nxt[i] = head[x]

    to[i] = y

    head[x] = i

    j = i + N - 1

    nxt[j] = head[y]

    to[j] = x

    head[y] = j



def EulerTour(n, i=0):

    f = lambda k: k * (k + 1) // 2

    USED = [0] * n

    ORG = [0] * n

    TMP = [0] * n

    ANS = [f(n)] * n

    

    P = [-1] * n

    ct = 0

    ET1 = [0] * n

    ET2 = [0] * n

    while i >= 0:

        e = head[i]

        if e < 0:

            ET2[i] = ct

            USED[C[i]] += 1 + TMP[i]

            if i:

                k = ET2[i] - ET1[i] + 1 - USED[C[P[i]]] + ORG[i]

                ANS[C[P[i]]] -= f(k)

                TMP[P[i]] += k

            i = P[i]

            continue

        

        j = to[e]

        if P[i] == j:

            head[i] = nxt[e]

            continue

        

        P[j] = i

        head[i] = nxt[e]

        i = j

        ORG[i] = USED[C[P[i]]]

        ct += 1

        ET1[i] = ct

    

    for i in range(n):

        ANS[i] -= f(n - USED[i])

    return ANS



print(*EulerTour(N, 0), sep = "\n")

import sys

input = lambda: sys.stdin.readline().rstrip()
N = int(input())
C = [int(a) - 1 for a in input().split()]
X = [[] for i in range(N)]
for i in range(N - 1):
    x, y = map(int, input().split())
    X[x - 1].append(y - 1)
    X[y - 1].append(x - 1)


def EulerTour(n, X, i0):
    f = lambda k: k * (k + 1) // 2
    USED = [0] * n
    ORG = [0] * n
    TMP = [0] * n
    P = [-1] * n
    ct = -1
    ET1 = [0] * n
    ET2 = [0] * n
    ANS = [f(n)] * n
    IND = [len(x) for x in X]
    i = i0
    while i >= 0:
        ind = IND[i]
        if not ind:
            ET2[i] = ct
            USED[C[i]] += 1 + TMP[i]
            if i:
                k = ET2[i] - ET1[i] + 1 - USED[C[P[i]]] + ORG[i]
                ANS[C[P[i]]] -= f(k)
                TMP[P[i]] += k
            i = P[i]
            continue
        j = X[i][ind - 1]
        if P[i] == j:
            IND[i] -= 1
            continue
        P[j] = i
        IND[i] -= 1
        i = j
        ORG[i] = USED[C[P[i]]]
        ct += 1
        ET1[i] = ct
    for i in range(n):
        ANS[i] -= f(n - USED[i])
    return ANS


print(*EulerTour(N, X, 0), sep="\n")

import sys

input = lambda: sys.stdin.readline().rstrip()
N = int(input())
C = [int(a) - 1 for a in input().split()]
X = [[] for i in range(N)]
head = [-1] * (N + 1)
to = [0] * (N - 1 << 1)
nxt = [0] * (N - 1 << 1)
for i in range(N - 1):
    x, y = map(int, input().split())
    x, y = x - 1, y - 1
    nxt[i] = head[x]
    to[i] = y
    head[x] = i
    j = i + N - 1
    nxt[j] = head[y]
    to[j] = x
    head[y] = j


def EulerTour(n, i=0):
    f = lambda k: k * (k + 1) // 2
    USED = [0] * n
    ORG = [0] * n
    TMP = [0] * n
    ANS = [f(n)] * n
    P = [-1] * n
    ct = 0
    ET1 = [0] * n
    ET2 = [0] * n
    while i >= 0:
        e = head[i]
        if e < 0:
            ET2[i] = ct
            USED[C[i]] += 1 + TMP[i]
            if i:
                k = ET2[i] - ET1[i] + 1 - USED[C[P[i]]] + ORG[i]
                ANS[C[P[i]]] -= f(k)
                TMP[P[i]] += k
            i = P[i]
            continue
        j = to[e]
        if P[i] == j:
            head[i] = nxt[e]
            continue
        P[j] = i
        head[i] = nxt[e]
        i = j
        ORG[i] = USED[C[P[i]]]
        ct += 1
        ET1[i] = ct
    for i in range(n):
        ANS[i] -= f(n - USED[i])
    return ANS


print(*EulerTour(N, 0), sep="\n")

+head = [-1] * (N + 1) +to = [0] * (N - 1 << 1) +nxt = [0] * (N - 1 << 1) -    X[x - 1].append(y - 1) -    X[y - 1].append(x - 1) +    x, y = x - 1, y - 1 +    nxt[i] = head[x] +    to[i] = y +    head[x] = i +    j = i + N - 1 +    nxt[j] = head[y] +    to[j] = x +    head[y] = j -def EulerTour(n, X, i0): +def EulerTour(n, i=0): +    ANS = [f(n)] * n -    ct = -1 +    ct = 0 -    ANS = [f(n)] * n -    IND = [len(x) for x in X] -    i = i0 -        ind = IND[i] -        if not ind: +        e = head[i] +        if e < 0: -        j = X[i][ind - 1] +        j = to[e] -            IND[i] -= 1 +            head[i] = nxt[e] -        IND[i] -= 1 +        head[i] = nxt[e] -print(*EulerTour(N, X, 0), sep="\n") +print(*EulerTour(N, 0), sep="\n")