Language: C++

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

const int maxn = 2e5 + 5;

int n;
int c[maxn];
vector<int> G[maxn];

int size[maxn];
int tot;
int cnt[maxn];
typedef long long ll;

ll ans[maxn];

void dfs(int u, int fa){
    size[u] = 1;
    int in = cnt[c[u]]; 
    for(auto v : G[u]){
        if(v == fa)
            continue;
        int front = tot - cnt[c[u]];
        dfs(v, u);
        size[u] += size[v];
        int back = tot - cnt[c[u]];
        ans[c[u]] -= 1LL * (back - front) * (back - front + 1) / 2;
    } 
    tot++;
    cnt[c[u]] = in + size[u];
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> c[i];
    }
    for(int i = 1;i < n;i++){
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, 1);
    for(int i = 1;i <= n;i++){
        ans[i] -= 1LL * (n - cnt[i]) * (n - cnt[i] + 1) / 2;
        ans[i] += 1LL * n * (n + 1) / 2;
    }
    for(int i = 1;i <= n;i++){
        cout << ans[i] << endl;
    }
    return 0;
}


Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:18:5: error: reference to 'size' is ambiguous
   18 |     size[u] = 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:10:5: note:                 'int size [200005]'
   10 | int size[maxn];
      |     ^~~~
a.cc:25:9: error: reference to 'size' is ambiguous
   25 |         size[u] += 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:10:5: note:                 'int size [200005]'
   10 | int size[maxn];
      |     ^~~~
a.cc:25:20: error: reference to 'size' is ambiguous
   25 |         size[u] += 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:10:5: note:                 'int size [200005]'
   10 | int size[maxn];
      |     ^~~~
a.cc:30:22: error: reference to 'size' is ambiguous
   30 |     cnt[c[u]] = in + size[u];
      |                      ^~~~
/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:10:5: note:                 'int size [200005]'
   10 | int size[maxn];
      |     ^~~~