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
|
#include <cctype> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
namespace IO { const int MAXSIZE = 1 << 18 | 1; char buf[MAXSIZE], *p1, *p2;
inline int Gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2)? EOF: *p1++; } template<typename T> inline void read(T& x) { x = 0; int f = 0, ch = Gc(); while (!isdigit(ch)) f |= ch == '-', ch = Gc(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = Gc(); if (f) x = -x; } } using IO::read;
typedef long long LL; const int MAXN = 1e5+5;
int n; int C[MAXN]; LL Ans[MAXN];
namespace Graph { struct Edge { int nxt, to; } edges[MAXN << 1]; int head[MAXN], eidx;
inline void init() { memset(head, -1, sizeof head), eidx = 1; } inline void AddEdge(int from, int to) { edges[++eidx] = (Edge){ head[from], to }; head[from] = eidx; } }
namespace TreeDivide { using namespace Graph; bool vis[MAXN]; int cnt[MAXN], num; LL W[MAXN], tot, Y; int Balance[MAXN], size[MAXN], subsize, ct;
inline void init(const int& x) { subsize = x, Balance[ct = 0] = MAXN; }
void findCt(int u, int fa) { Balance[u] = 0, size[u] = 1; for (int v, i = head[u]; ~i; i = edges[i].nxt) { if ((v = edges[i].to) == fa || vis[v]) continue; findCt(v, u), size[u] += size[v]; Balance[u] = max(Balance[u], size[v]); } Balance[u] = max(Balance[u], subsize - size[u]); if (Balance[u] < Balance[ct]) ct = u; }
void dfs(int u, int fa) { ++cnt[C[u]], size[u] = 1; for (int v, i = head[u]; ~i; i = edges[i].nxt) { if ((v = edges[i].to) == fa || vis[v]) continue; dfs(v, u), size[u] += size[v]; } if (cnt[C[u]] == 1) tot += size[u], W[C[u]] += size[u]; --cnt[C[u]]; }
void subDfs(int u, int fa) { ++cnt[C[u]]; if (cnt[C[u]] == 1) tot -= W[C[u]], ++num; Ans[u] += tot + Y * num; for (int v, i = head[u]; ~i; i = edges[i].nxt) if ((v = edges[i].to) != fa && !vis[v]) subDfs(v, u); if (cnt[C[u]] == 1) tot += W[C[u]], --num; --cnt[C[u]]; }
void Mdy(int u, int fa, const int& type) { ++cnt[C[u]]; for (int v, i = head[u]; ~i; i = edges[i].nxt) if ((v = edges[i].to) != fa && !vis[v]) Mdy(v, u, type); if (cnt[C[u]] == 1) tot += 1LL * type * size[u], W[C[u]] += 1LL * type * size[u]; --cnt[C[u]]; }
void clear(int u, int fa) { W[C[u]] = cnt[C[u]] = 0; for (int v, i = head[u]; ~i; i = edges[i].nxt) if ((v = edges[i].to) != fa && !vis[v]) clear(v, u); }
void Divide(int u) { vis[u] = true; dfs(u, -1); Ans[u] += tot - W[C[u]] + size[u]; for (int v, i = head[u]; ~i; i = edges[i].nxt) { if (vis[v = edges[i].to]) continue; ++cnt[C[u]], tot -= size[v], W[C[u]] -= size[v]; Mdy(v, u, -1); --cnt[C[u]], Y = size[u] - size[v]; subDfs(v, u); ++cnt[C[u]], tot += size[v], W[C[u]] += size[v]; Mdy(v, u, 1); --cnt[C[u]]; } num = tot = 0, clear(u, -1); for (int v, i = head[u]; ~i; i = edges[i].nxt) { if (vis[v = edges[i].to]) continue; init(size[v]), findCt(v, -1), Divide(ct); } }
inline void solve() { init(n), findCt(1, -1), Divide(ct); } }
int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif Graph::init(); read(n); for (int i = 1; i <= n; ++i) read(C[i]); for (int u, v, i = 1; i < n; ++i) read(u), read(v), Graph::AddEdge(u, v), Graph::AddEdge(v, u); TreeDivide::solve(); for (int i = 1; i <= n; ++i) printf("%lld\n", Ans[i]); return 0; }
|