Language: C++

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

// 0-indexed
template <class T>
struct BIT {
  int treesize;
  vector<T> lst;
  // constructor
  BIT(int newn = 0) : treesize(newn), lst(newn + 1, 0) {}
  // a_place += num
  void add(int place, T num) {
    ++place;
    while (place <= treesize) {
      lst[place] += num;
      place += place & -place;
    }
  }
  // sum between [0,place)
  T sum(int place) {
    T res = 0;
    while (place > 0) {
      res += lst[place];
      place -= place & -place;
    }
    return res;
  }
  // sum [l,r)
  T sum(int left, int right) { return sum(right) - sum(left); }
};

struct EulerTour {
  int n, root;
  vector<vector<int>> g;
  vector<int> par, dep, in, out, lst;
  EulerTour(int n = 1, int _root = 0)
      : root(_root), g(n), par(n), dep(n), in(n), out(n) {}
  EulerTour(const vector<vector<int>> &_g, const int _root = 0)
      : root(_root),
        g(_g),
        par(_g.size()),
        dep(_g.size()),
        in(_g.size()),
        out(_g.size()),
  {
    build();
  }
  void add(int a, int b) {
    g[a].push_back(b);
    g[b].push_back(a);
  }
  void build() {
    lst.clear();
    dfsv(root, -1, 0);
    // dfse(root, -1,  0);
  }
  void dfsv(int now, int bf, int d) {
    dep[now] = d;
    par[now] = bf;
    in[now] = lst.size();
    lst.push_back(now);
    for (auto &to : g[now])
      if (to != bf) {
        dfsv(to, now, d + 1);
        lst.push_back(now);
      }
    lst.push_back(now);
    out[now] = lst.size();
  }
  void dfse(int now, int bf, int d) {
    dep[now] = d;
    par[now] = bf;
    if (bf == -1) {
      in[now] = 0;
      lst.push_back(now);
    }
    for (auto &to : g[now])
      if (to != bf) {
        in[to] = lst.size();
        lst.push_back(to);
        dfse(to, now, d + 1);
        out[to] = lst.size();
        lst.push_back(to);
      }
    if (bf == -1) {
      in[now] = lst.size();
      lst.push_back(now);
    }
  }
};


long long n;
vector<int> c;
vector<vector<int>> edge, col, memo;

void solve();

int main() {
  cin >> n;
  c.resize(n);
  edge.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  solve();
  return 0;
}

void solve() {
  EulerTour et(edge);
  BIT<long long> bit(et.lst.size());
  col.resize(n);
  memo.resize(n);
  for (int i = 0; i < n; ++i) bit.add(et.in[i], 1);
  for (int i = 0; i < et.lst.size(); ++i) memo[et.lst[i]].push_back(i);
  for (int i = 0; i < n; ++i) col[c[i]].push_back(i);
  for (int i = 0; i < n; ++i) {
    long long res = n * (n + 1) / 2;
    using P = pair<int, int>;
    vector<P> v;
    map<int, int> mp;
    for (int node : col[i]) {
      bit.add(et.in[node], -1);
      mp[node] = 1;
      for (int id = 0; id < (int)memo[node].size() - 1; ++id)
        v.emplace_back(memo[node][id + 1], node);
    }
    sort(v.begin(), v.end());
    for (P p : v) {
      long long now = bit.sum(et.in[p.second], p.first);
      res -= now * (now + 1) / 2;
      mp[p.second] += now;
      bit.add(et.in[p.second], -now);
    }
    long long last = bit.sum(et.lst.size());
    res -= last * (last + 1) / 2;
    cout << res << endl;
    for (auto p : mp) bit.add(et.in[p.first], p.second);
  }
}


Compiler output:
a.cc: In constructor 'EulerTour::EulerTour(const std::vector<std::vector<int> >&, int)':
a.cc:45:3: error: expected identifier before '{' token
   45 |   {
      |   ^