//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, -);
}

最新文章

  1. jmeter使用IP欺骗进行压力测试
  2. WampServer服务中MySQL无法正常启动解决方案
  3. 转载 yii2-按需加载并管理CSS样式/JS脚本
  4. CCPC网络赛,HDU_5842 Lweb and String
  5. 个人博客设计:创建Sql数据库操作类。
  6. MYSQL根据分类分组取每组一条数据且按条件能排序的写法
  7. 自定义JQuery插件之 beforeFocus
  8. 利用FreeMarker静态化网页
  9. android4.4组件分析--service组件-bindService源代码分析
  10. 省前训练...Orz
  11. 【SSRS】入门篇(七) -- 报表发布
  12. hdu_2670Girl Love Value(dp)
  13. Jupyter notebook入门
  14. openlayers4 入门开发系列之批量叠加 zip 压缩 SHP 图层篇(附源码下载)
  15. HTML常用基础标签
  16. git冲突解决的几种办法
  17. SQL 查询 技巧
  18. 数学软件Matlab的使用感受
  19. C#将图片存放到SQL SERVER数据库中的方法
  20. virgo-tomcat-server最大并发连接数的修改

热门文章

  1. CodeForces 731F Video Cards (数论+暴力)
  2. P2700逐个击破(并查集/树形dp)
  3. P1266 速度限制(分层图spfa)
  4. 清北考前刷题day3下午好
  5. bzoj1015星球大战(并查集+离线)
  6. codevs1297 硬币(背包dp,方案数)
  7. 个人微信号二次开发SDK协议,个人微信号二次开发api接口
  8. 搞定springboot项目连接远程服务器上kafka遇到的坑以及完整的例子
  9. linux编译安装gcc5.3.0
  10. 递推DP UVA 1424 Salesmen