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
| #include <bits/stdc++.h> using namespace std;
inline int read() { int x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();} while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();} return x * f; }
const int maxn = 100010;
struct Edge { int from, to; Edge(int u, int v): from(u), to(v) {} }; vector<Edge> edges; vector<int> G[maxn]; void add(int u, int v) { edges.push_back(Edge(u, v)); int mm = edges.size(); G[u].push_back(mm - 1); }
int n, Mx, Son, col[maxn], son[maxn], siz[maxn], cnt[maxn]; long long sum = 0, ans[maxn];
void dfs1(int x, int fa) { siz[x] = 1; for (int i = 0; i < G[x].size(); ++i) { Edge& e = edges[G[x][i]]; if (e.to == fa) continue; dfs1(e.to, x); siz[x] += siz[e.to]; if (siz[e.to] > siz[son[x]]) { son[x] = e.to; } } }
void Add(int x, int fa, int val) { cnt[col[x]] += val; if (cnt[col[x]] > Mx) { Mx = cnt[col[x]]; sum = col[x]; } else if (cnt[col[x]] == Mx) { sum += (long long)col[x]; } for (int i = 0; i < G[x].size(); ++i) { Edge& e = edges[G[x][i]]; if (e.to == fa || e.to == Son) continue; Add(e.to, x, val); } }
void dfs2(int x, int fa, int opt) { for (int i = 0; i < G[x].size(); ++i) { Edge& e = edges[G[x][i]]; if (e.to == fa) continue; if (e.to != son[x]) dfs2(e.to, x, 0); } if (son[x]) { dfs2(son[x], x, 1); Son = son[x]; } Add(x, fa, 1); Son = 0; ans[x] = sum; if (!opt) { Add(x, fa, -1); sum = 0, Mx = 0; } }
int main() { n = read(); for (int i = 1; i <= n; ++i) { col[i] = read(); } for (int i = 1; i < n; ++i) { int u = read(), v = read(); add(u, v); add(v, u); } dfs1(1, 0); dfs2(1, 0, 0); for (int i = 1; i <= n; ++i) { printf("%lld ", ans[i]); } return 0; }
|