Looks like dotted, in fact, there \ (O (n) \) practice?
In fact, I just do not want to write it so written in dotted result feels a burned
Consider a color contribution of the answer.
Consider this point in the tree colors are deleted, then there will be a lot of trees, these trees in points between each other will not contribute, but will have contributed between two points of different trees.
Thus, we can get every color, sum-point value is n - size of trees where.
Thus, a point is the sum total of n * number of colors - size of small trees where each color.
We consider size for a small tree, there is a minimum depth of a node, then the latter can use a tree difference to achieve coverage.
Ah, probably with a virtual tree it is possible,But I will not
So how size requirements?I can not speak, fucks like
#include<bits/stdc++.h>
#define LL long long
#define re register
#define fr(i,x,y) for(int i=(x);i<=(y);i++)
#define rf(i,x,y) for(int i=(x);i>=(y);i--)
#define frl(i,x,y) for(int i=(x);i<(y);i++)
using namespace std;
const int N=100002;
const int M=N<<1;
int n,a[N];
int cnt,head[N],Next[M],v[M];
inline void read(int &x){
char ch=getchar();x=0;
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
}
void add(int x,int y){
Next[++cnt]=head[x];
head[x]=cnt;
v[cnt]=y;
}
int sz[N],f[N],pre[N];
LL tag[N],c[N];
void dfs(int x,int fa){
sz[x]=1;f[x]=pre[a[x]];
for(re int i=head[x];i;i=Next[i])
if (v[i]!=fa){
pre[a[x]]=v[i];
dfs(v[i],x);
tag[v[i]]+=sz[v[i]];
sz[x]+=sz[v[i]];
}
tag[f[x]]-=sz[x];
if (f[x]==1) c[a[x]]-=sz[x];
pre[a[x]]=f[x];
}
LL sum[N];
void dfs(int x,int fa,LL s){
int cc=c[a[x]];
s+=tag[x]-cc;
sum[x]=s;
for(re int i=head[x];i;i=Next[i])
if (v[i]!=fa){
c[a[x]]=tag[v[i]];
dfs(v[i],x,s);
}
c[a[x]]=cc;
}
int b[N];
int main(){
read(n);
fr(i,1,n) read(a[i]),b[a[i]]=1;
int x,y;
fr(i,2,n){
read(x);read(y);
add(x,y);add(y,x);
}
//fr(i,1,100000) ver[i].push_back(0);
int tot=0;
fr(i,1,100000) if (b[i]) tot++,pre[i]=1,c[i]=n;
tag[1]=1LL*tot*n;
dfs(1,0);
dfs(1,0,0);
fr(i,1,n) printf("%lld\n",1LL*n*tot-sum[i]);
return 0;
}