Language: C++

#include <bits/stdc++.h>
using namespace std;

using ll  = long long;
using vl  = vector<ll>;
using vvl = vector<vl>;
#define rng(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,n) for(int i = 0; i < (n); ++i)

ll N, sum = 0;
vvl edges;
vl color, ans, cnts, size;

void dfs(int v, int parent) {
    size[v] = 1;
    ll prev_dfs = cnts[color[v]];
    for (auto to : edges[v]) {
        if (to == parent) continue;
        ll prev = sum - cnts[color[v]];
        dfs(to, v);
        size[v] += size[to];
        ll next = sum - cnts[color[v]];
        ll diff = next - prev;
        ans[color[v]] -= diff * (diff-1) / 2;
    }
    cnts[color[v]] = prev_dfs + size[v];
    sum++;
}

int main() {
    cin >> N;
    edges.resize(N);
    color.resize(N);
    size.resize(N);
    ans.assign(N,N*(N-1)/2);
    cnts.assign(N,0);
    rep(i,N) { cin >> color[i]; color[i]--; }
    rep(i,N-1) {
        int a, b; cin >> a >> b;
        a--; b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    dfs(0,-1);
    rep(c,N) {
        ans[c] -= (N-cnts[c]) * (N-cnts[c]-1) / 2;
    }
    rep(i,N) ans[color[i]]++;
    rep(i,N) cout << ans[i] << endl;
    return 0;
}


Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:15:5: error: reference to 'size' is ambiguous
   15 |     size[v] = 1;
      |     ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:12:22: note:                 'vl size'
   12 | vl color, ans, cnts, size;
      |                      ^~~~
a.cc:21:9: error: reference to 'size' is ambiguous
   21 |         size[v] += size[to];
      |         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:12:22: note:                 'vl size'
   12 | vl color, ans, cnts, size;
      |                      ^~~~
a.cc:21:20: error: reference to 'size' is ambiguous
   21 |         size[v] += size[to];
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:12:22: note:                 'vl size'
   12 | vl color, ans, cnts, size;
      |                      ^~~~
a.cc:26:33: error: reference to 'size' is ambiguous
   26 |     cnts[color[v]] = prev_dfs + size[v];
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:12:22: note:                 'vl size'
   12 | vl color, ans, cnts, size;
      |                      ^~~~
a.cc: In function 'int main()':
a.cc:34:5: error: reference to 'size' is ambiguous
   34 |     size.resize(N);
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:12:22: note:                 'vl size'
   12 | vl color, ans, cnts, size;
      |                      ^~~~