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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| // ============================================================== //
const int maxn = 1e5 + 10; const int inf = 0x3f3f3f3f; int n; int M = 0; int color[maxn];
class Edge { public: int to, w; Edge *next; Edge() {} Edge(int to, int w) : to(to), w(w) { next = NULL; } } edges[maxn << 1], *head[maxn];
void initG() { M = 0; memset(head, 0, sizeof(head)); }
void add(int u, int v, int w) { edges[++M] = Edge(v, w); edges[M].next = head[u]; head[u] = &edges[M]; }
int sn = n; int sz[maxn]; int root = 0; int vis[maxn]; ll ans[maxn];
void getRoot(int x, int pa, int &res) { sz[x] = 1; int maxpart = 0; for(const Edge* e = head[x]; e; e = e->next) { int y = e->to; int w = e->w; if(vis[y] || y == pa) continue;
getRoot(y, x, res); sz[x] += sz[y]; maxpart = max(maxpart, sz[y]); } maxpart = max(maxpart, sn - sz[x]); if(maxpart < res) { res = maxpart; root = x; } }
ll num[maxn], cnt[maxn];
void clear(int x, int pa) { cnt[color[x]] = num[color[x]] = 0; sz[x] = 1;
for(const Edge *e = head[x]; e; e = e->next) { int y = e->to; int w = e->w;
if(vis[y] || y == pa) continue;
clear(y, x); sz[x] += sz[y]; } }
void calculate1(int x, int pa, ll& sum) { if(++cnt[color[x]] == 1) { sum += sz[x]; num[color[x]] += sz[x]; }
for(const Edge *e = head[x]; e; e = e->next) { int y = e->to; int w = e->w; if(y == pa || vis[y]) continue;
calculate1(y, x, sum); }
--cnt[color[x]]; }
void change(int x, int pa, int sgn, ll& sum) { if(++cnt[color[x]] == 1) { sum += sgn * sz[x]; num[color[x]] += sgn * sz[x]; }
for(const Edge *e = head[x]; e; e = e->next) { int y = e->to; int w = e->w;
if(y == pa || vis[y]) continue;
change(y, x, sgn, sum); } --cnt[color[x]]; }
void calculate2(int x, int pa, int cnt2, int other, ll& sum) { if(++cnt[color[x]] == 1) { cnt2++; sum -= num[color[x]]; }
ans[x] += sum + 1ll * other * cnt2;
for(const Edge *e = head[x]; e; e = e->next) { int y = e->to; int w = e->w;
if(vis[y] || y == pa) continue; calculate2(y, x, cnt2, other, sum); }
if(--cnt[color[x]] == 0) { cnt2--; sum += num[color[x]]; } }
void work(int x, ll& sum) { vis[x] = 1; sum = 0; clear(x, -1);
calculate1(x, -1, sum); ans[x] += 1ll * sum;
for(const Edge* e = head[x]; e; e = e->next) { int y = e->to; int w = e->w; if(vis[y]) continue;
++cnt[color[x]]; change(y, x, -1, sum); sum -= sz[y]; num[color[x]] -= sz[y];
calculate2(y, x, 0, sz[x] - sz[y], sum);
change(y, x, 1, sum); sum += sz[y]; num[color[x]] += sz[y]; --cnt[color[x]]; }
for(const Edge* e = head[x]; e; e = e->next) { int y = e->to; if(vis[y]) continue;
root = -1; sn = sz[y]; int res = inf; getRoot(y, -1, res);
work(root, sum); } }
void init() { sn = n; memset(sz, 0, sizeof(sz)); memset(vis, 0, sizeof(vis)); memset(ans, 0, sizeof(ans)); memset(num, 0, sizeof(num)); memset(cnt, 0, sizeof(cnt)); }
int main() { freopen("input.txt", "r", stdin); scanf("%d", &n); initG(); init();
_rep(i, 1, n) scanf("%d", &color[i]); _for(i, 1, n) { int x, y; scanf("%d%d", &x, &y); add(x, y, 1); add(y, x, 1); }
// input data finished root = -1; ll sum = 0; int res = inf; getRoot(1, -1, res);
work(root, sum);
_rep(i, 1, n) printf("%lld\n", ans[i]); }
|