题面:codeforces600E

学习一下$dsu \ on \ tree$。。

这个东西可以处理很多无修改子树问题,复杂度通常为$O(nlogn)$。

主要操作是:我们先把整棵树链剖一下,然后每次先递归轻儿子,再递归重儿子。

对于每棵子树,我们暴力加入整棵子树的贡献。如果是重儿子的子树则另外处理:加入贡献时不考虑加重儿子所在的子树,而在消除贡献时也不消除重儿子的子树,直到它成为某个点的轻儿子的子树的一部分时再消除贡献。

复杂度:因为每个轻儿子最多被加入$O(logn)$次(递归轻儿子时$size$至少$/2$),每条重链最多只会被加入$O(logn)$次,所以复杂度是$O(nlogn)$的。

说得有点玄学,还是看看代码吧。。

  1. //It is made by wfj_2048~
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <cmath>
  9. #include <queue>
  10. #include <stack>
  11. #include <map>
  12. #include <set>
  13. #define inf (1<<30)
  14. #define N (200010)
  15. #define il inline
  16. #define RG register
  17. #define ll long long
  18.  
  19. using namespace std;
  20.  
  21. struct edge{ int nt,to; }g[N<<];
  22.  
  23. int head[N],col[N],son[N],sz[N],c[N],n,num,Mx,flag;
  24. ll ans[N],Sum;
  25.  
  26. il int gi(){
  27. RG int x=,q=; RG char ch=getchar();
  28. while ((ch<'' || ch>'') && ch!='-') ch=getchar();
  29. if (ch=='-') q=-,ch=getchar();
  30. while (ch>='' && ch<='') x=x*+ch-,ch=getchar();
  31. return q*x;
  32. }
  33.  
  34. il void insert(RG int from,RG int to){
  35. g[++num]=(edge){head[from],to},head[from]=num; return;
  36. }
  37.  
  38. il void dfs1(RG int x,RG int p){
  39. sz[x]=; RG int v;
  40. for (RG int i=head[x];i;i=g[i].nt){
  41. v=g[i].to; if (v==p) continue;
  42. dfs1(v,x),sz[x]+=sz[v];
  43. if (sz[son[x]]<=sz[v]) son[x]=v;
  44. }
  45. return;
  46. }
  47.  
  48. il void add(RG int x,RG int p,RG int val){
  49. col[c[x]]+=val; RG int v;
  50. if (Mx<col[c[x]]) Mx=col[c[x]],Sum=c[x];
  51. else if (Mx==col[c[x]]) Sum+=c[x];
  52. for (RG int i=head[x];i;i=g[i].nt){
  53. v=g[i].to; if (v==p || v==flag) continue;
  54. add(v,x,val);
  55. }
  56. return;
  57. }
  58.  
  59. il void dfs2(RG int x,RG int p,RG int fg){
  60. RG int v;
  61. for (RG int i=head[x];i;i=g[i].nt){
  62. v=g[i].to; if (v!=p && v!=son[x]) dfs2(v,x,);
  63. }
  64. if (son[x]) dfs2(son[x],x,),flag=son[x];
  65. add(x,p,),flag=,ans[x]=Sum;
  66. if (fg) add(x,p,-),Mx=Sum=; return;
  67. }
  68.  
  69. int main(){
  70. #ifndef ONLINE_JUDGE
  71. freopen("600E.in","r",stdin);
  72. freopen("600E.out","w",stdout);
  73. #endif
  74. n=gi();
  75. for (RG int i=;i<=n;++i) c[i]=gi();
  76. for (RG int i=,u,v;i<n;++i)
  77. u=gi(),v=gi(),insert(u,v),insert(v,u);
  78. dfs1(,),dfs2(,,);
  79. for (RG int i=;i<=n;++i) printf("%I64d ",ans[i]);
  80. return ;
  81. }

codeforces 600E Lomsat gelral的更多相关文章

  1. Codeforces 600E - Lomsat gelral(树上启发式合并)

    600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...

  2. Codeforces 600E Lomsat gelral (树上启发式合并)

    题目链接 Lomsat gelral 占坑……等深入理解了再来补题解…… #include <bits/stdc++.h> using namespace std; #define rep ...

  3. Codeforces 600E. Lomsat gelral(Dsu on tree学习)

    题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...

  4. Codeforces 600E Lomsat gelral(dsu on tree)

    dsu on tree板子题.这个trick保证均摊O(nlogn)的复杂度,要求资瓷O(1)将一个元素插入集合,清空集合时每个元素O(1)删除.(当然log的话就变成log^2了) 具体的,每次先遍 ...

  5. codeforces 600E. Lomsat gelral 启发式合并

    题目链接 给一颗树, 每个节点有初始的颜色值. 1为根节点.定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和. 每一个叶子节点是一个map ...

  6. codeforces 600E . Lomsat gelral (线段树合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

  7. Codeforces.600E.Lomsat gelral(dsu on tree)

    题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...

  8. Codeforces 600E - Lomsat gelral 「$Dsu \ on \ tree$模板」

    With $Dsu \ on \ tree$ we can answer queries of this type: How many vertices in the subtree of verte ...

  9. 【Codeforces】600E. Lomsat gelral

    Codeforces 600E. Lomsat gelral 学习了一下dsu on tree 所以为啥是dsu而不是dfs on tree??? 这道题先把这棵树轻重链剖分了,然后先处理轻儿子,处理 ...

随机推荐

  1. 9.JSP进阶

    1.JSP内置对象 JSP容器在_jspService()方法中声明并初始化9个内置对象. 名称 作用 接口/类 out 客户端打开的输出流 javax.servlet.jsp.JspWriter 接 ...

  2. python模块之beautifulSoup

    1. Beautiful Soup的简单介绍 Beautiful Soup是python的一个库,主要的功能是从网页抓取数据,并对数据进行分析.官方解释为:Beautiful Soup提供一些简单的. ...

  3. NETCore 调试

    https://www.cnblogs.com/MingQiu/p/8227644.html https://www.cnblogs.com/shumin/p/9967854.html 前言 core ...

  4. 一文彻底明白linux中的selinux到底是什么

    https://www.phpyuan.com/235739.html 一.前言 安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内 ...

  5. java——简易版build模式

    参考教程:https://blog.csdn.net/fanxudonggreat/article/details/78927773 public class Computer { private S ...

  6. eclipse_project

    转!java web项目 build path 导入jar包,tomcat启动报错 找不到该类 在eclipse集成tomcat开发java web项目时,引入的外部jar包,编译通过,但启动tomc ...

  7. input 单选按钮radio 取消选中

    //需要先引入JQ.js <input name="rdo" value="AA" type="radio" tag="0& ...

  8. PlayMaker 对 PlayMakerFSM 里变量的操作

    HutongGames.PlayMaker; //需要引用这个命名空间 红色的字体是对变量的操作,其他的没啥关系. #region 判断为 PlayMakerFSM 组件时 if (behaviour ...

  9. Unity [SerializeField]

    在Unity3d中Unity3D 中提供了非常方便的功能可以帮助用户将 成员变量 在Inspector中显示,并且定义Serialize关系. 也就是说凡是显示在Inspector 中的属性都同时具有 ...

  10. LeetCode 887.鸡蛋掉落(C++)

    每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去. 你知道存在楼层 F ,满足 0 <= F <= N 任何从高于 F 的楼层落下的鸡蛋都会碎,从 F 楼层或比它低的楼层落下的 ...