从树上启发式合并搜出来的题
然而看着好像线段树合并就能解决???
那么就用线段树合并解决吧
维护(max, sum)表示值域区间中的一个数出现次数的最大值以及所有众数的和即可
复杂度(O(n log n))
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --)
#define gc getchar
inline int read() {
int p = 0, w = 1; char c = gc();
while(c < '0' || c > '9') { if(c == '-') w = -1; c = gc(); }
while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();
return p * w;
}
const int sid = 4e5 + 5;
int n, cnp, id;
ll sum[sid], ans[sid];
int rt[sid], ls[sid], rs[sid], mx[sid], c[sid];
int cap[sid], nxt[sid], node[sid];
inline void addedge(int u, int v) {
nxt[++ cnp] = cap[u]; cap[u] = cnp; node[cnp] = v;
}
inline void insert(int &o, int l, int r, int v) {
o = ++ id;
mx[o] = 1; sum[o] = v;
if(l == r) return;
int mid = (l + r) >> 1;
if(v <= mid) insert(ls[o], l, mid, v);
else insert(rs[o], mid + 1, r, v);
}
inline void upd(int o) {
int lc = ls[o], rc = rs[o];
mx[o] = mx[lc]; sum[o] = sum[lc];
if(mx[rc] == mx[o]) sum[o] += sum[rc];
else if(mx[rc] > mx[o]) mx[o] = mx[rc], sum[o] = sum[rc];
}
inline int merge(int x, int y, int l, int r) {
if(!x || !y) return x + y;
if(l == r) { mx[x] += mx[y]; return x; }
int mid = (l + r) >> 1;
ls[x] = merge(ls[x], ls[y], l, mid);
rs[x] = merge(rs[x], rs[y], mid + 1, r);
upd(x);
return x;
}
#define cur node[i]
inline void dfs(int o, int fa) {
insert(rt[o], 1, n, c[o]);
for(int i = cap[o]; i; i = nxt[i])
if(cur != fa) {
dfs(cur, o);
rt[o] = merge(rt[o], rt[cur], 1, n);
}
ans[o] = sum[rt[o]];
}
int main() {
n = read();
rep(i, 1, n) c[i] = read();
rep(i, 2, n) {
int u = read(), v = read();
addedge(u, v); addedge(v, u);
}
dfs(1, 0);
rep(i, 1, n) printf("%lld ", ans[i]);
return 0;
}