Language: C++

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

long long N;
long long c[40001];//color
long long time = 1;

vector<long long> G_origin[20001];
vector<long long> G_directed[40001];
vector<long long> G_monochro[40001];
queue<long long> que_whose_color_is[20001];

bool val[20001];
long long num_children[20001];
long long time_stamp[40001][2];
long long memory_for_G_monochro[20001];
long long num_paths_whose_color_is_not[20001];

long long dfs_origin(long long v){
    val[v] = 1;

    time_stamp[v][0] = time;
    time++;

    que_whose_color_is[c[v]].push(v);
    
    num_children[v]=1;
    for(long long i=0; i<G_origin[v].size(); ++i){
        if(val[G_origin[v][i]]==0){
            G_directed[v].push_back(G_origin[v][i]);
            num_children[v] += dfs_origin(G_origin[v][i]);
        }
    }

    time_stamp[v][1] = time;
    time++;
    
    return num_children[v];
}

void dfs_monochro(long long v){
    long long num;
    long long p=0;
    for(long long j=0; j<G_directed[v].size();++j){
        if(p<G_monochro[v].size()){
            while(time_stamp[G_monochro[v][p]][0]<time_stamp[G_directed[v][j]][0]){
                p++;
                if(p>=G_monochro[v].size()) break;
            }
        }
        num=num_children[G_directed[v][j]];
        if(p<G_monochro[v].size()){
            while(time_stamp[G_monochro[v][p]][1]<=time_stamp[G_directed[v][j]][1]){
                num -= num_children[G_monochro[v][p]];
                p++;
                if(p>=G_monochro[v].size()) break;
            }
        }
        if(num>0){
            num_paths_whose_color_is_not[c[v]] += (num+1)*num/2;
        }
    }
    for(p=0; p<G_monochro[v].size(); ++p){
        dfs_monochro(G_monochro[v][p]);
    }
}

int main(){
    //input
    cin >> N;
    for(long long i=1; i<=N; i++){
        cin >> c[i];
    }
    long long a, b;
    for(long long i=1; i<=N-1; i++){
        cin >> a >> b;
        G_origin[a].push_back(b);
        G_origin[b].push_back(a);
    }

    //calc_dfs_origin
    for(long long i=1; i<=N; i++){
        que_whose_color_is[i].push(20000+i);
    }
    fill(val,val+N+1,0);
    dfs_origin(1);
    for(long long i=1; i<=N; i++){
        c[20000+i]=i;
        time_stamp[20000+i][0]=0;
        time_stamp[20000+i][1]=60000;
        num_children[20000+i] = num_children[1]+1;
        G_directed[20000+i].push_back(1);
    }

    //calc_create_G_monochro
    long long p, memo;
    for(long long i=1; i<=N; i++){
        p=0;//depth of G_monochro whose color is i
        while(!que_whose_color_is[i].empty()){
            if(p==0){
                memo = que_whose_color_is[i].front();
                que_whose_color_is[i].pop();
                memory_for_G_monochro[0]=memo;
                p++;
            }else{
                memo = que_whose_color_is[i].front();
                que_whose_color_is[i].pop();
                while(time_stamp[memo][1]>time_stamp[memory_for_G_monochro[p-1]][1]){
                    p--;
                }
                memory_for_G_monochro[p] = memo;
                G_monochro[memory_for_G_monochro[p-1]].push_back(memo);
                p++;
            }
        }
    }

    //calc_num_of_out_paths
    fill(num_paths_whose_color_is_not,num_paths_whose_color_is_not+N+1,0);
    for(long long i=1; i<=N; i++){
        dfs_monochro(20000+i);
    }
    
    //answer
    for(long long i=1; i<=N; i++){
        cout << (N+1)*N/2 - num_paths_whose_color_is_not[i] << endl;
    }
    return 0;
}

Compiler output:
a.cc:8:11: error: 'long long int time' redeclared as different kind of entity
    8 | long long time = 1;
      |           ^~~~
In file included from /usr/include/pthread.h:23,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/gthr.h:157,
                 from /usr/include/c++/14/ext/atomicity.h:35,
                 from /usr/include/c++/14/bits/ios_base.h:39,
                 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/time.h:76:15: note: previous declaration 'time_t time(time_t*)'
   76 | extern time_t time (time_t *__timer) __THROW;
      |               ^~~~
a.cc: In function 'long long int dfs_origin(long long int)':
a.cc:24:24: error: invalid conversion from 'time_t (*)(time_t*) noexcept' {aka 'long int (*)(long int*) noexcept'} to 'long long int' [-fpermissive]
   24 |     time_stamp[v][0] = time;
      |                        ^~~~
      |                        |
      |                        time_t (*)(time_t*) noexcept {aka long int (*)(long int*) noexcept}
a.cc:25:5: warning: ISO C++ forbids incrementing a pointer of type 'time_t (*)(time_t*) noexcept' {aka 'long int (*)(long int*) noexcept'} [-Wpointer-arith]
   25 |     time++;
      |     ^~~~
a.cc:25:9: error: lvalue required as increment operand
   25 |     time++;
      |         ^~
a.cc:37:24: error: invalid conversion from 'time_t (*)(time_t*) noexcept' {aka 'long int (*)(long int*) noexcept'} to 'long long int' [-fpermissive]
   37 |     time_stamp[v][1] = time;
      |                        ^~~~
      |                        |
      |                        time_t (*)(time_t*) noexcept {aka long int (*)(long int*) noexcept}
a.cc:38:5: warning: ISO C++ forbids incrementing a pointer of type 'time_t (*)(time_t*) noexcept' {aka 'long int (*)(long int*) noexcept'} [-Wpointer-arith]
   38 |     time++;
      |     ^~~~
a.cc:38:9: error: lvalue required as increment operand
   38 |     time++;
      |         ^~