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
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; const int maxn = 100000 + 5;
vector<int> edge[maxn]; int n, c[maxn]; ll ans[maxn], cnt[maxn];
int sz[maxn], son[maxn]; void getsz(int u, int f){ sz[u] = 1; for (auto& v : edge[u]){ if (v == f) continue; getsz(v, u); sz[u] += sz[v]; if (sz[v] > sz[son[u]]){ son[u] = v; } } } ll res = 0; int mx = 0; bool sk[maxn]; void add(int u, int f, int x){ cnt[c[u]] += x; if (x > 0 && cnt[c[u]] >= mx){ if (cnt[c[u]] > mx) res = 0, mx = cnt[c[u]]; res += c[u]; } for (auto& v : edge[u]){ if (v == f || sk[v]) continue; add(v, u, x); } } void dfs(int u, int f, int kp){ for (auto& v : edge[u]){ if (v == f || v == son[u]) continue; dfs(v, u, 0); } if (son[u]) dfs(son[u], u, 1), sk[son[u]] = 1; add(u, f, 1); ans[u] = res; if (son[u]) sk[son[u]] = 0; if (!kp) add(u, f, -1), res = mx = 0; }
int main(){ scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", c + i); for (int i = 1; i < n; i++){ int x, y; scanf("%d%d", &x, &y); edge[x].push_back(y); edge[y].push_back(x); } getsz(1, 0); dfs(1, 0, 0); for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]); return 0; }
|