Language: C++

#include <bits/stdc++.h>
#define LL long long
#define maxn 200010
using namespace std;
int color[maxn],head[maxn],tot,n,size[maxn],sum[maxn];
LL ans[maxn];
struct node{
	int to,next;
}e[maxn<<1];
void add_edge(int u,int v){
	tot++,e[tot].to=v,e[tot].next=head[u],head[u]=tot;
}
LL get_num(LL x){
	return x*(x+1)/2;
}
void dfs(int u,int fa){
	int v,b,c,t,dt;
	LL pre;
	size[u]=1,c=color[u];//,pre=sum[c];//非u子树已算出的c颜色为根的节点数量
	for(b=head[u];b;b=e[b].next){
		v=e[b].to;
		if(v==fa)continue;
		t=sum[c];
		dfs(v,u);
		dt=sum[c]-t;//遍历了v之后，c颜色对应的变化量
		size[u]+=size[v];
		ans[c]-=get_num(size[v]-dt);//两个c颜色之间的，其他颜色节点数量
	}

	sum[c]=size[u];//总的c颜色为根的节点数量
	//cout << "u: " << u << "    pre: " << pre << "     c: " << c << "    sum: " << sum[c] << "     size: " << size[u] << endl;
}
int main(){
	int i,u,v;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&color[i]);
	for(i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		add_edge(u,v),add_edge(v,u);
	}
	for(i=1;i<=n;i++)ans[i]=get_num(n);
	dfs(1,0);
	for(i=1;i<=n;i++)ans[i]-=get_num(n-sum[i]);//n-sum[i]指离树根最近的i颜色之外的其他颜色(整棵树扣除所有以i颜色为根的子树)数量
	for(i=1;i<=n;i++)printf("%lld\n",ans[i]);
	return 0;
}


Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:19:9: error: reference to 'size' is ambiguous
   19 |         size[u]=1,c=color[u];//,pre=sum[c];//非u子树已算出的c颜色为根的节点数量
      |         ^~~~
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:5:34: note:                 'int size [200010]'
    5 | int color[maxn],head[maxn],tot,n,size[maxn],sum[maxn];
      |                                  ^~~~
a.cc:26:17: error: reference to 'size' is ambiguous
   26 |                 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:5:34: note:                 'int size [200010]'
    5 | int color[maxn],head[maxn],tot,n,size[maxn],sum[maxn];
      |                                  ^~~~
a.cc:26:26: error: reference to 'size' is ambiguous
   26 |                 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:5:34: note:                 'int size [200010]'
    5 | int color[maxn],head[maxn],tot,n,size[maxn],sum[maxn];
      |                                  ^~~~
a.cc:27:33: error: reference to 'size' is ambiguous
   27 |                 ans[c]-=get_num(size[v]-dt);//两个c颜色之间的，其他颜色节点数量
      |                                 ^~~~
/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:5:34: note:                 'int size [200010]'
    5 | int color[maxn],head[maxn],tot,n,size[maxn],sum[maxn];
      |                                  ^~~~
a.cc:30:16: error: reference to 'size' is ambiguous
   30 |         sum[c]=size[u];//总的c颜色为根的节点数量
      |                ^~~~
/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:5:34: note:                 'int size [200010]'
    5 | int color[maxn],head[maxn],tot,n,size[maxn],sum[maxn];
      |                                  ^~~~