Language: C++

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend(

/* do your best */

// quoted from beet-aizu
template <typename T, typename E, typename F, typename G>
struct SegmentTree{
    // using F = function<T(T, T)>
    // using G = function<T(T, E)>
    int n;
    F f;
    G g;
    T ti;
    vector<T> dat;
    SegmentTree(){};
    SegmentTree(F f,G g,T ti):f(f),g(g),ti(ti){}
    void init(int n_){    
        n=1;
        while(n<n_) n<<=1;
        dat.assign(n<<1,ti);
    }
    void build(const vector<T> &v){
        int n_=v.size();
        init(n_);
        for(int i=0;i<n_;i++) dat[n+i]=v[i];
        for(int i=n-1;i;i--)
            dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
    }
    void update(int k,const E &x){
        k += n;
        dat[k] = g(dat[k], x);
        while(k>>=1)
            dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);    
    }
    T operator [](int k) const { return dat[k+n]; }
    T query(int a,int b) const {
        T vl=ti,vr=ti;
        for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
            if(l&1) vl=f(vl,dat[l++]);
            if(r&1) vr=f(dat[--r],vr);
        }
        return f(vl,vr);
    }

    /* TODO わからない 聞く 
    template<typename C>
    int find(int a,int b,C &check,int k,int l,int r){
        if(!check(dat[k])||r<=a||b<=l) return -1;
        if(k>=n) return k-n;
        int m=(l+r)>>1;
        int vl=find(a,b,check,(k<<1)|0,l,m);
        if(~vl) return vl;
        return find(a,b,check,(k<<1)|1,m,r);
    }
    template<typename C>
    int find(int a,int b,C &check){
        return find(a,b,check,1,0,n);
    }*/
    
};


/**  テンプレ
int main(){
  using T = ***;  // type T
  using E = ***;  // type E
  auto f = [](T a, T b){ // return type T value
    return ***;
  };
  auto g = [](T a, E b){ // return type T value
    return ***;  // return b;
  };
  T ti = ***;  // identity element
  SegmentTree<T, E, decltype(f), decltype(g)> sg(f, g, ti);  // don't change
  sg.build(***);
}
**/

void dfs(int cur, int par, vector<vector<int>> &g, vector<int> &c, vector<vector<int>> &nodes, vector<int> &p) {
  p[cur] = par;
  for (auto nxt : g[cur]) {
    if (nxt == par) continue;
    dfs(nxt, cur, g, c, nodes, p);
  }

  nodes[c[cur]].push_back(cur);
}

void tour_dfs(int cur, int par, vector<vector<int>> &g, vector<int> &tour, vector<int> &fst, vector<int> &lst) {
  int idx = tour.size();
  fst[cur] = idx;
  tour.push_back(1);
  for (auto nxt : g[cur]) {
    if (nxt == par) continue;
    tour_dfs(nxt, cur, g, tour, fst, lst);
  }
  idx = tour.size();
  lst[cur] = idx;
  tour.push_back(1);
}

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

  vector<vector<int>> nodes(n); // 葉に近い順l
  vector<kint> p(n, -1);
  dfs(0, -1, g, c, nodes, p);
  vector<kint> fst(n);
  vector<lint> lst(n);
  vector<lint> tour;
  tour_dfs(0, -1, g, tour, fst, lst);

  using T = lint;  // type T
  using E = lint;  // type E
  auto f = [](T a, T b){ // return type T value
    return a + b;
  };
  auto gg = [](T a, E b){ // return type T value
    return b;
  };
  T ti = 0;  // identity element
  SegmentTree<T, E, decltype(f), decltype(gg)> sg(f, gg, ti);  // don't change
  sg.build(tour);

  for (int k = 0; k < n; k++) {

    // 全通り
    lint ans = n * (n - 1) / 2 + n;

    // 葉に近い順に処理
    lint used = 0;
    for (auto node : nodes[k]) {
      sg.update(fst[node], 0);
      sg.update(lst[node], 0);
    }
    for (auto node : nodes[k]) {
      // 連結成分のサイズを取得
      lint minus = used;
      for (auto ch : g[node]) {
        if (ch == p[node]) continue;
        lint sum = sg.query(fst[ch], lst[ch] + 1);
        sum /= 2;
        sum -= minus;
        ans -= sum * (sum - 1) / 2 + sum;
        used += sum;
      }
    }
    for (auto node : nodes[k]) {
      sg.update(fst[node], 1);
      sg.update(lst[node], 1);
    }
    
    lint rest = n - used - (lint)nodes[k].size();
    ans -= rest * (rest - 1) / 2 + rest;


    cout << ans << endl;
  }

  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:154:10: error: 'kint' was not declared in this scope; did you mean 'uint'?
  154 |   vector<kint> p(n, -1);
      |          ^~~~
      |          uint
a.cc:154:14: error: template argument 1 is invalid
  154 |   vector<kint> p(n, -1);
      |              ^
a.cc:154:14: error: template argument 2 is invalid
a.cc:154:23: error: expression list treated as compound expression in initializer [-fpermissive]
  154 |   vector<kint> p(n, -1);
      |                       ^
a.cc:155:27: error: invalid initialization of reference of type 'std::vector<int>&' from expression of type 'int'
  155 |   dfs(0, -1, g, c, nodes, p);
      |                           ^
a.cc:113:109: note: in passing argument 6 of 'void dfs(int, int, std::vector<std::vector<int> >&, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<int>&)'
  113 | void dfs(int cur, int par, vector<vector<int>> &g, vector<int> &c, vector<vector<int>> &nodes, vector<int> &p) {
      |                                                                                                ~~~~~~~~~~~~~^
a.cc:156:14: error: template argument 2 is invalid
  156 |   vector<kint> fst(n);
      |              ^
a.cc:159:22: error: invalid initialization of reference of type 'std::vector<int>&' from expression of type 'std::vector<long long int>'
  159 |   tour_dfs(0, -1, g, tour, fst, lst);
      |                      ^~~~
a.cc:123:70: note: in passing argument 4 of 'void tour_dfs(int, int, std::vector<std::vector<int> >&, std::vector<int>&, std::vector<int>&, std::vector<int>&)'
  123 | void tour_dfs(int cur, int par, vector<vector<int>> &g, vector<int> &tour, vector<int> &fst, vector<int> &lst) {
      |                                                         ~~~~~~~~~~~~~^~~~
a.cc:181:20: error: invalid types 'int[int]' for array subscript
  181 |       sg.update(fst[node], 0);
      |                    ^
a.cc:188:20: error: invalid types 'int[int]' for array subscript
  188 |         if (ch == p[node]) continue;
      |                    ^
a.cc:189:32: error: invalid types 'int[int]' for array subscript
  189 |         lint sum = sg.query(fst[ch], lst[ch] + 1);
      |                                ^
a.cc:197:20: error: invalid types 'int[int]' for array subscript
  197 |       sg.update(fst[node], 1);
      |                    ^