传送门

Luogu

解题思路

线段树合并板子题(也可以 dsu on the tree)

好像没什么好讲的,就是要注意开 long long 。

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} typedef long long LL;
const int _ = 100010; int tot, head[_], nxt[_ << 1], ver[_ << 1];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; } int n, c[_];
int cnt, rt[_], lc[_ << 5], rc[_ << 5];
LL mx[_ << 5], col[_ << 5], ans[_]; inline void pushup(int p) {
if (mx[lc[p]] > mx[rc[p]]) mx[p] = mx[lc[p]], col[p] = col[lc[p]];
if (mx[lc[p]] < mx[rc[p]]) mx[p] = mx[rc[p]], col[p] = col[rc[p]];
if (mx[lc[p]] == mx[rc[p]]) mx[p] = mx[lc[p]], col[p] = col[lc[p]] + col[rc[p]];
} inline void update(int& p, int x, int l = 1, int r = n) {
if (!p) p = ++cnt;
if (l == r) { ++mx[p], col[p] = l; return; }
int mid = (l + r) >> 1;
if (x <= mid) update(lc[p], x, l, mid);
else update(rc[p], x, mid + 1, r);
pushup(p);
} inline int merge(int x, int y, int l = 1, int r = n) {
if (!x || !y) return x + y;
if (l == r)
return mx[x] += mx[y], col[x] = l, x;
int mid = (l + r) >> 1;
lc[x] = merge(lc[x], lc[y], l, mid);
rc[x] = merge(rc[x], rc[y], mid + 1, r);
return pushup(x), x;
} inline void dfs(int u, int f) {
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
dfs(v, u), rt[u] = merge(rt[u], rt[v]);
}
update(rt[u], c[u]), ans[u] = col[rt[u]];
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int i = 1; i <= n; ++i) read(c[i]), rt[++cnt] = i;
for (rg int u, v, i = 1; i < n; ++i)
read(u), read(v), Add_edge(u, v), Add_edge(v, u);
dfs(1, 0);
for (rg int i = 1; i <= n; ++i) printf("%lld ", ans[i]);
return 0;
}

完结撒花 \(qwq\)

最新文章

  1. (原创)vim配色------水果色,不伤眼。
  2. 12款免费的 WordPress 响应式主题下载
  3. window 安装 sass compass 记录
  4. java一般使用基础
  5. 学习笔记之html5相关内容
  6. Bate敏捷冲刺每日报告--day3
  7. 04、NetCore2.0下Web应用之Startup源码解析
  8. 超大文本文件浏览器Snaptext,支持不限制大小的文本文件浏览
  9. Thermostat:双层存储结构的透明巨页内存管理机制
  10. Django—常用功能
  11. Understanding Docker
  12. dj forms表单组件
  13. 取值为[1,n-1]含n个元素的整数数组,至少存在一个重复数,即可能存在多个重复数,O(n)时间内找出其中任意一个重复数,不使用额外存储空间。
  14. BASE_DIR 拼接文件路径
  15. 让QT/Embedded支持国际化
  16. iOS笔记之ScrollView
  17. Oracle11g 行列转换函数PIVOT and UNPIVOT
  18. Java基础21-构造函数之间的调用
  19. Azure镜像市场再下一城,中标软件入驻开启Azure国产操作系统时代
  20. Ubuntu pppoe 拨号上网

热门文章

  1. 【代码审计】XDCMS 报错注入
  2. node vue 项目git 管理
  3. Robot Framework 使用【2】-- MAC系统搭建Robot Framework
  4. 重装VisualSVN Server报错
  5. SearchRequest用于与搜索文档、聚合、定制查询有关的任何操作
  6. sftp和FTP
  7. 每个 JavaScript 开发者都该懂的 Unicode
  8. 微信小程序加密解密 C# 以及 填充无效,无法被移除错误的解决方案 Padding is invalid and cannot be removed
  9. c数据结构链式存储-静态链表
  10. nacos作为配置中心兼容xml配置文件