Language: C++

#include<bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define pi pair<ll,ll>
#define endl "\n"
using namespace std;
const ll N=500010;

ll n;
vector<ll> color;
vector<ll> adj[N];
vector<ll> nodeslist[N];
ll size[N];
vector<ll> ans;

vector<ll> tsenter,tsexit;
ll tim;

void dfs(ll v,ll p)
{
    ++tim;
    tsenter[v]=tim;
    size[v]=1;
    ll c=color[v];
    for(auto u:adj[v])
    {
        if(u==p){continue;}
        dfs(u,v);
        size[v]+=size[u];
        ll w=size[u];
        while( nodeslist[c].size()>0 && tsenter[nodeslist[c].back()]>tsenter[v] )
        {
            ll node=nodeslist[c].back();
            w-=size[node];
            nodeslist[c].pop_back();
        }
        ans[c]+=(w*(w+1))/2;
    }
    nodeslist[color[v]].pb(v);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n;
    color.resize(n+1);
    ans.resize(n+1,0);
    tsenter.resize(n+1,-1);
    tsexit.resize(n+1,-1);
    tim=-1;
    for(ll i=1;i<=n;++i){cin>>color[i];}
    for(ll i=0;i<n-1;++i){
        ll a,b;
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    dfs(1,1);

    for(ll i=1;i<=n;++i){
        ll w=n;
        while( nodeslist[i].size()>0 ){
            ll node=nodeslist[i].back();
            w-=size[node];
            nodeslist[i].pop_back();
        }
        ans[i]+=(w*(w+1))/2;
    }

    for(ll i=1;i<=n;++i){
        ll x=(n*(n+1))/2;
        x-=ans[i];
        cout<<x<<endl;
    }

    return (0);
}


Compiler output:
a.cc: In function 'void dfs(long long int, long long int)':
a.cc:26:5: error: reference to 'size' is ambiguous
   26 |     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:16:4: note:                 'long long int size [500010]'
   16 | ll size[N];
      |    ^~~~
a.cc:32:9: error: reference to 'size' is ambiguous
   32 |         size[v]+=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:16:4: note:                 'long long int size [500010]'
   16 | ll size[N];
      |    ^~~~
a.cc:32:18: error: reference to 'size' is ambiguous
   32 |         size[v]+=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:16:4: note:                 'long long int size [500010]'
   16 | ll size[N];
      |    ^~~~
a.cc:33:14: error: reference to 'size' is ambiguous
   33 |         ll w=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:16:4: note:                 'long long int size [500010]'
   16 | ll size[N];
      |    ^~~~
a.cc:37:16: error: reference to 'size' is ambiguous
   37 |             w-=size[node];
      |                ^~~~
/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:16:4: note:                 'long long int size [500010]'
   16 | ll size[N];
      |    ^~~~
a.cc: In function 'int main()':
a.cc:71:16: error: reference to 'size' is ambiguous
   71 |             w-=size[node];
      |                ^~~~
/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:16:4: note:                 'long long int size [500010]'
   16 | ll size[N];
      |    ^~~~