Language: C++

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

llint n;
vector<llint> G[400005];
llint c[400005];
llint size[400005];

vector<llint> G2[400005];
llint pre[400005];
vector<llint> vec[400005];
llint ans[400005];

llint dfs(llint v, llint p)
{
	size[v] = 1;
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == p) continue;
		size[v] += dfs(G[v][i], v);
	}
	return size[v];
}

void dfs2(llint v, llint p)
{
	G2[pre[c[v]]].push_back(v);
	
	llint tmp = pre[c[v]];
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == p) continue;
		llint u = G[v][i];
		pre[c[v]] = u;
		dfs2(u, v);
	}
	pre[c[v]] = tmp;
}

void dfs3(llint v, llint p)
{
	llint ret = size[v];
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == p) continue;
		llint u = G[v][i], sum = size[u];
		for(int j = 0; j < G2[u].size(); j++){
			sum -= size[G2[u][j]];
		}
		vec[c[v]].push_back(sum);
	}
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == p) continue;
		llint u = G[v][i];
		dfs3(u, v);
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> c[i];
	llint u, v;
	for(int i = 1; i <= n-1; i++){
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(1, -1);
	
	for(int i = 1; i <= n; i++) pre[i] = n+i;
	for(int i = 1; i <= n; i++) size[n+i] = n+1;
	
	dfs2(1, -1);
	
	dfs3(1, -1);
	for(int i = 1; i <= n; i++){
		llint sum = n;
		for(int j = 0; j < G2[n+i].size(); j++){
			sum -= size[G2[n+i][j]];
		}
		vec[i].push_back(sum);
	}
	
	for(int i = 1; i <= n; i++){
		ans[i] = n*(n+1)/2;
		for(int j = 0; j < vec[i].size(); j++){
			llint m = vec[i][j];
			ans[i] -= m*(m+1)/2;
		}
	}
	
	/*for(int i = 1; i <= n; i++){
		for(int j = 0; j < vec[i].size(); j++) cout << vec[i][j] << " ";  cout << endl;
	}*/
	
	for(int i = 1; i <= n; i++) cout << ans[i] << endl;
	
	return 0;
}

Compiler output:
a.cc: In function 'long long int dfs(long long int, long long int)':
a.cc:41:9: error: reference to 'size' is ambiguous
   41 |         size[v] = 1;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 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:32:7: note:                 'long long int size [400005]'
   32 | llint size[400005];
      |       ^~~~
a.cc:44:17: error: reference to 'size' is ambiguous
   44 |                 size[v] += dfs(G[v][i], 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:32:7: note:                 'long long int size [400005]'
   32 | llint size[400005];
      |       ^~~~
a.cc:46:16: error: reference to 'size' is ambiguous
   46 |         return 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:32:7: note:                 'long long int size [400005]'
   32 | llint size[400005];
      |       ^~~~
a.cc: In function 'void dfs3(long long int, long long int)':
a.cc:65:21: error: reference to 'size' is ambiguous
   65 |         llint ret = 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:32:7: note:                 'long long int size [400005]'
   32 | llint size[400005];
      |       ^~~~
a.cc:68:42: error: reference to 'size' is ambiguous
   68 |                 llint u = G[v][i], sum = 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:32:7: note:                 'long long int size [400005]'
   32 | llint size[400005];
      |       ^~~~
a.cc:70:32: error: reference to 'size' is ambiguous
   70 |                         sum -= size[G2[u][j]];
      |                                ^~~~
/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:32:7: note:                 'long long int size [400005]'
   32 | llint size[400005];
      |       ^~~~
a.cc: In function 'int main()':
a.cc:97:37: error: reference to 'size' is ambiguous
   97 |         for(int i = 1; i <= n; i++) size[n+i] = n+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:32:7: note:                 'long long int size [400005]'
   32 | llint size[400005];
      |       ^~~~
a.cc:105:32: error: reference to 'size' is ambiguous
  105 |                         sum -= size[G2[n+i][j]];
      |                                ^~~~
/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:32:7: note:                 'long long int size [400005]'
   32 | llint size[400005];
      |       ^~~~