树上启发式合并(DSU on tree)
2024-09-08 02:24:44
//heavy-light decomposition style .
//http://codeforces.com/blog/entry/44351
int cnt[maxn];
bool big[maxn];
void add(int v, int p, int x){
cnt[ col[v] ] += x;
for(auto u: g[v])
if(u != p && !big[u])
add(u, v, x)
}
void dfs(int v, int p, bool keep){
int mx = -, bigChild = -;
for(auto u : g[v])
if(u != p && sz[u] > mx)
mx = sz[u], bigChild = u;
for(auto u : g[v])
if(u != p && u != bigChild)
dfs(u, v, ); // run a dfs on small childs and clear them from cnt
if(bigChild != -)
dfs(bigChild, v, ), big[bigChild] = ; // bigChild marked as big and not cleared from cnt
add(v, p, );
//now cnt[c] is the number of vertices in subtree of vertex v that has color c. You can answer the queries easily.
if(bigChild != -)
big[bigChild] = ;
if(keep == )
add(v, p, -);
}
最新文章
- jmeter使用IP欺骗进行压力测试
- WampServer服务中MySQL无法正常启动解决方案
- 转载 yii2-按需加载并管理CSS样式/JS脚本
- CCPC网络赛,HDU_5842 Lweb and String
- 个人博客设计:创建Sql数据库操作类。
- MYSQL根据分类分组取每组一条数据且按条件能排序的写法
- 自定义JQuery插件之 beforeFocus
- 利用FreeMarker静态化网页
- android4.4组件分析--service组件-bindService源代码分析
- 省前训练...Orz
- 【SSRS】入门篇(七) -- 报表发布
- hdu_2670Girl Love Value(dp)
- Jupyter notebook入门
- openlayers4 入门开发系列之批量叠加 zip 压缩 SHP 图层篇(附源码下载)
- HTML常用基础标签
- git冲突解决的几种办法
- SQL 查询 技巧
- 数学软件Matlab的使用感受
- C#将图片存放到SQL SERVER数据库中的方法
- virgo-tomcat-server最大并发连接数的修改
热门文章
- CodeForces 731F Video Cards (数论+暴力)
- P2700逐个击破(并查集/树形dp)
- P1266 速度限制(分层图spfa)
- 清北考前刷题day3下午好
- bzoj1015星球大战(并查集+离线)
- codevs1297 硬币(背包dp,方案数)
- 个人微信号二次开发SDK协议,个人微信号二次开发api接口
- 搞定springboot项目连接远程服务器上kafka遇到的坑以及完整的例子
- linux编译安装gcc5.3.0
- 递推DP UVA 1424 Salesmen