import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto C = readln.split.map!(x => x.to!int-1).array;
    auto G = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }

    auto sub = new int[](N);
    auto colorsub = new int[](N);
    colorsub[] = N;

    void dfs1(int n, int p) {
        sub[n] = 1;
        foreach (m; G[n]) if (m != p) {
            dfs1(m, n);
            sub[n] += sub[m];
        }
    }

    dfs1(0, -1);

    auto ans = new long[](N);
    auto prev = new int[](N);
    prev[] = -1;
    auto tmp = new long[](N);

    void dfs(int n, int p) {
        auto c = C[n];
        if (prev[c] != -1) {
            tmp[prev[c]] -= sub[n];
        } else {
            colorsub[c] -= sub[n];
        }
        auto prevprev = prev[c];
        prev[c] = n;
        foreach (m; G[n]) if (m != p) {
            tmp[n] = sub[m];
            dfs(m, n);
            ans[c] += 1L * tmp[n] * (tmp[n] + 1) / 2;
        }
        prev[c] = prevprev;
    }


    dfs(0, -1);

    foreach (i; 0..N) {
        ans[i] += 1L * colorsub[i] * (colorsub[i] + 1) / 2;
    }

    auto S = 1L * N * (N + 1) / 2;
    ans.each!(a => writeln(S - a));
}
