Language: C++

#include<bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ld long double
#define endl '\n'
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define All(x) (x).begin(), (x).end()
#define rAll(x) (x).rbegin(), (x).rend()
#define _ <<' '<<
#define TIME 1.0*clock()/CLOCKS_PER_SEC
//#define int ll

using namespace std;

template<typename T, typename U> inline ostream &operator << (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator >> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator << (ostream &_out, const vector<T> &_v) { if (_v.empty()) return _out; _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) _out << ' ' << *_it; return _out; }
template<typename T> inline istream &operator >> (istream &_in, vector<T> &_v) { for (auto &_i : _v) _in >> _i; return _in; }
template<typename T> inline ostream &operator << (ostream &_out, const set<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it; return _out; }
template<typename T> inline ostream &operator << (ostream &_out, const multiset<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it; return _out; }
template<typename T> inline ostream &operator << (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it; return _out; }
template<typename T> inline ostream &operator << (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it; return _out; }
template<typename T, typename U> inline ostream &operator << (ostream &_out, const map<T, U> &_m) { if (_m.empty()) return _out; _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) _out << ", (" << _it->first << ": " << _it->second << ')'; return _out; }
template<typename T, typename U> inline ostream &operator << (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) return _out; _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) _out << ", (" << _it->first << ": " << _it->second << ')'; return _out; }
template<typename T, typename U> const operator+(const pair<T, U> &l, const pair<T, U> &r){return {l.F + r.F, l.S + r.S};}
template<typename T, typename U> const operator-(const pair<T, U> &l, const pair<T, U> &r){return {l.F - r.F, l.S - r.S};}
template<typename T, typename U> const operator+=(pair<T, U>& l, const pair<T, U>& r){l.F += r.F;l.S += r.S;}
template<typename T, typename U> const operator-=(pair<T, U>& l, const pair<T, U>& r){l.F -= r.F;l.S -= r.S;}

mt19937 gen(time(0) + 1);

const int N = 1e6 + 7;
const int M = 1e9 + 7;

int n, c[N], tin[N], sz[N], T;
ll res[N];
vector<int> g[N], st[N];

ll f(ll x){return x*(x + 1)/2;}

void dfs(int v, int p = -1){
    sz[v] = 1;
    tin[v] = ++T;
    for (auto to : g[v]){
        if (to == p) continue;
        dfs(to, v);
        sz[v] += sz[to];
        ll cur = sz[to];
        while (!st[c[v]].empty() && tin[st[c[v]].back()] >= tin[v]){
            cur -= sz[st[c[v]].back()];
            st[c[v]].pop_back();
        }
        res[c[v]] += f(cur);
    }
    st[c[v]].pb(v);
}

main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    cin >> n;
    for (int i = 0; i < n; i++) cin >> c[i];
    for (int i = 1; i < n; i++){
        int x, y;
        cin >> x >> y;
        x--;y--;
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs(0);
    for (int i = 1; i <= n; i++){
        ll cur = n;
        for (auto it : st[i])
            cur -= sz[it];
        res[i] += f(cur);
    }
    for (int i = 1; i <= n; i++)
    cout << f(n) - res[i] << ' ';
    return 0;
}


Compiler output:
a.cc:28:40: error: ISO C++ forbids declaration of 'operator+' with no type [-fpermissive]
   28 | template<typename T, typename U> const operator+(const pair<T, U> &l, const pair<T, U> &r){return {l.F + r.F, l.S + r.S};}
      |                                        ^~~~~~~~
a.cc:29:40: error: ISO C++ forbids declaration of 'operator-' with no type [-fpermissive]
   29 | template<typename T, typename U> const operator-(const pair<T, U> &l, const pair<T, U> &r){return {l.F - r.F, l.S - r.S};}
      |                                        ^~~~~~~~
a.cc:30:40: error: ISO C++ forbids declaration of 'operator+=' with no type [-fpermissive]
   30 | template<typename T, typename U> const operator+=(pair<T, U>& l, const pair<T, U>& r){l.F += r.F;l.S += r.S;}
      |                                        ^~~~~~~~
a.cc: In function 'const int operator+=(std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)':
a.cc:30:109: warning: no return statement in function returning non-void [-Wreturn-type]
   30 | template<typename T, typename U> const operator+=(pair<T, U>& l, const pair<T, U>& r){l.F += r.F;l.S += r.S;}
      |                                                                                                             ^
a.cc: At global scope:
a.cc:31:40: error: ISO C++ forbids declaration of 'operator-=' with no type [-fpermissive]
   31 | template<typename T, typename U> const operator-=(pair<T, U>& l, const pair<T, U>& r){l.F -= r.F;l.S -= r.S;}
      |                                        ^~~~~~~~
a.cc: In function 'const int operator-=(std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)':
a.cc:31:109: warning: no return statement in function returning non-void [-Wreturn-type]
   31 | template<typename T, typename U> const operator-=(pair<T, U>& l, const pair<T, U>& r){l.F -= r.F;l.S -= r.S;}
      |                                                                                                             ^
a.cc: At global scope:
a.cc:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   61 | main(){
      | ^~~~