中学毕业了,十七号就要前往武汉报道。中学的终点是武汉大学,人生的终点却不是,最初的热情依然失却,我还是回来看看这分类排版皆惨淡的博客吧,只是是用来保存代码也好。想要换一个新博客,带着之前的经验能把它整理得更好。就如人生总渴求一个新的开始,却往往是在过去的殷墟间孑孓独行。既然如此,暂且继续用这个博客吧。中学时的OI结局是我多年来的遗憾,这可能也是支持我果断参加ACM并用整个假期参与多校训练赛的本心吧。我从不相信天才,那只是兴趣遇上合适教育与一点幸运的人,都是一样的。然而,多年来也意识到自己匮乏的天分与耽误的时光,不由想到刚起程的ACM也可能以新的遗憾收尾。可是,不走下去,又怎能见证一路的风景?我不是个积极的人,所以需要一个方向指引我,只是回忆路边的景致就令我沉醉。我刚开始的时候,DEV连c++11都不支持,而现在,我习惯了用C++20,所以,多少是有点改变的,是吧?过去的憧憬,如今的理性,像两颗枝蔓不断衍生的线段树,就这样合并起来。把路上的一切剖分,锁定每个节点对应的色彩,又一点点开出新的节点,一路上,差分,合并,统计,便是答案

# include "bits/stdc++.h"
using namespace std;
constexpr int N = 1e5 + 3;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector G(n + 1, vector<int>());
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
vector<int> fa(n + 1), siz(n + 1), dep(n + 1), son(n + 1, 0);
dep[0] = 0;
auto dfs_first = [&](auto dfs_first, int u, int father) -> void {
fa[u] = father, siz[u] = 1, dep[u] = dep[father] + 1;
for(auto v : G[u]) {
if(v == father) continue;
dfs_first(dfs_first, v, u);
siz[u] += siz[v];
if(siz[son[u]] < siz[v]) son[u] = v;
}
};
dfs_first(dfs_first, 1, 0);
vector<int> top(n + 1);
auto dfs_second = [&](auto dfs_second, int u, int ancestor) -> void {
top[u] = ancestor;
if(!son[u]) return;
dfs_second(dfs_second, son[u], ancestor);
for(auto v : G[u]) {
if(son[u] != v && fa[u] != v) {
dfs_second(dfs_second, v, v);
}
}
};
dfs_second(dfs_second, 1, 1);
struct node {
int l, r, id, val; // id -> color
} t[N << 6]; // weight segment tree : for each color, build a tree
vector<int> rt(n + 1, 0);
int tree_index = 0;
# define ls t[rt].l
# define rs t[rt].r
# define lson t[rt].l, l, mid
# define rson t[rt].r, mid + 1, r
auto push_up = [&](int rt) -> void {
if(t[ls].val < t[rs].val) { // if the same, output the smaller color-index
t[rt].val = t[rs].val;
t[rt].id = t[rs].id;
}
else {
t[rt].val = t[ls].val;
t[rt].id = t[ls].id; // id -> color
}
};
vector<int> ans(n + 1, 0);
while(m--) {
int x, y, col;
cin >> x >> y >> col;
auto LCA = [&](int x, int y) -> int {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) x ^= y ^= x ^= y;
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
};
int lca = LCA(x, y);
auto update = [&](auto update, int &rt, int l, int r, int col, int val) -> void {
if(!rt) rt = ++tree_index;
if(l == r) {
t[rt].val += val;
t[rt].id = col;
return;
}
int mid = l + r >> 1;
if(col <= mid)
update(update, lson, col, val);
else
update(update, rson, col, val);
push_up(rt);
};
update(update, rt[x], 1, N - 1, col, 1);
update(update, rt[y], 1, N - 1, col, 1);
update(update, rt[lca], 1, N - 1, col, -1);
if(lca != 1) update(update, rt[fa[lca]], 1, N - 1, col, -1);
}
auto merge = [&](auto merge, int x, int y, int l, int r) -> int { // x, y -> different segment tree
if(!x || !y) return x | y;
if(l == r) { // l == r -> (x, y -> same color node)
t[x].val += t[y].val;
return x;
}
int mid = l + r >> 1;
t[x].l = merge(merge, t[x].l, t[y].l, l, mid);
t[x].r = merge(merge, t[x].r, t[y].r, mid + 1, r);
push_up(x);
return x;
};
auto dfs = [&](auto dfs, int u, int father) -> void {
for(auto v : G[u]) {
if(v == father) continue;
dfs(dfs, v, u);
rt[u] = merge(merge, rt[u], rt[v], 1, N - 1);
}
if(t[rt[u]].val) {
ans[u] = t[rt[u]].id;
}
};
dfs(dfs, 1, 0);
for(int i = 1; i <= n; ++i) cout << ans[i] << "\n"[i == n];
return 0;
}

最新文章

  1. JSPatch打补丁
  2. git无法定位程序输入点libiconv
  3. Apache 的ab压力测试工具
  4. C语言的选择和循环上机题目(部分)
  5. mybatis插入的同时获取主键id
  6. mybatis 基础1(动态代理)
  7. JqueryAjax
  8. Spring AOP 原理
  9. rsyslog 日志格式和输出
  10. 51nod1138(math)
  11. uva12563
  12. js 动态绑定鼠标事件
  13. 我的SQL SERVER数据库会装满吗?
  14. 如何给Windows Server 2012 R2 添加“磁盘清理”选项
  15. 应用多种变形CSS3
  16. 快速切题 sgu116. Index of super-prime bfs+树思想
  17. hbase使用MapReduce操作1(基本增删改查)
  18. spring boot整合Swagger2
  19. zabbix之自动发现Tomcat多实例(第一种:已经部署完成,后续不再添加;第二种:后续或根据需要添加Tomcat实例)
  20. php opensll加解密类

热门文章

  1. 获取并检查系统负载\CPU\内存\磁盘\网络
  2. 【单片机】NB-IoT移远BC28调试笔记
  3. STM32 CubeMx使用教程
  4. 关于『HTML』:第三弹
  5. 给小白的 PG 容器化部署教程(下)
  6. 《HALCON数字图像处理》第一、二章笔记
  7. 利用Github Action实现Tornadofx/JavaFx打包
  8. 【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
  9. ExtJS配置TabPanel可以拖拽Tab标签页
  10. mysql调优学习笔记