1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include<bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5+10; int head[N],tot,n; int rt,sum,maxp[N],siz[N],cnt[N],V[N],color[N],much,maxx; ll ans[N],num; bool vis[N]; struct edge{ int v,nex,w; edge(){} edge(int v, int nex, int w) : v(v), nex(nex), w(w) {} }edges[N<<1]; void add(int u,int v,int w){ edges[tot]=edge(v,head[u],w); head[u]=tot++; } void dfs1(int u,int fa){ siz[u]=1; cnt[V[u]]++; for(int i = head[u];~i;i=edges[i].nex){ int v = edges[i].v; if(vis[v]||v==fa)continue; dfs1(v,u); siz[u]+=siz[v]; } if(cnt[V[u]]==1){ sum+=siz[u]; color[V[u]]+=siz[u]; } cnt[V[u]]--; }
void getrt(int u,int fa){ siz[u]=1,maxp[u]=0; for(int i=head[u];~i;i=edges[i].nex){ int v =edges[i].v; if(vis[v]||v==fa)continue; getrt(v,u); siz[u]+=siz[v]; if(maxp[u]<siz[v])maxp[u]=siz[v]; } maxp[u]=max(maxp[u],maxx-siz[u]); if(maxp[u]<maxp[rt])rt=u; } void change(int u,int fa,int value){ cnt[V[u]]++; for(int i=head[u];~i;i=edges[i].nex){ int v = edges[i].v; if(v==fa||vis[v])continue; change(v,u,value); } if(cnt[V[u]]==1){ sum += 1ll*siz[u]*value; color[V[u]]+=1ll*siz[u]*value; } cnt[V[u]]--; } void dfs2(int u,int fa){ cnt[V[u]]++;
if(cnt[V[u]]==1){ sum-=color[V[u]]; num++; } ans[u]+=sum+num*much; for(int i=head[u];~i;i=edges[i].nex){ int v = edges[i].v; if(vis[v]||v==fa)continue; dfs2(v,u); }
if(cnt[V[u]]==1){ sum+=color[V[u]]; num--; } cnt[V[u]]--; } void clear(int u,int fa){ cnt[V[u]]=0; color[V[u]]=0; for(int i=head[u];~i;i=edges[i].nex){ int v = edges[i].v; if(vis[v]||v==fa)continue; clear(v,u); } } void solve(int u,int fa){ dfs1(u,fa); ans[u]+=sum-color[V[u]]+siz[u]; for(int i=head[u];~i;i=edges[i].nex){ int v = edges[i].v; if(vis[v]||v==fa)continue;
cnt[V[u]]++; sum-=siz[v]; color[V[u]]-=siz[v]; change(v,u,-1); cnt[V[u]]--; much=siz[u]-siz[v];
dfs2(v,u);
cnt[V[u]]++; sum+=siz[v]; color[V[u]]+=siz[v]; change(v,u,1); cnt[V[u]]--; } sum = 0,num = 0;
clear(u,fa); }
void divide(int u,int fa){ vis[u]=1; solve(u,fa); for(int i=head[u];~i;i=edges[i].nex){ int v = edges[i].v; if(vis[v])continue; maxp[rt=0]=maxx=siz[v]; getrt(v,0); getrt(rt,0); divide(rt,0); } } int main(){ memset(head,-1,sizeof(head)); scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&V[i]); for(int i=0;i<n-1;i++){ int u,v,w; scanf("%d %d",&u,&v); add(u,v,0); add(v,u,0); } maxp[0]=maxx=n; getrt(1,0); getrt(rt,0); divide(rt,0); for(int i=1;i<=n;i++)printf("%lld\n",ans[i]); }
|