启发式合并最重要的思想就是指的是每次将小集合拷贝合并至大集合。
考虑每个元素的合并开销。对于合并次数最多的那个元素来说,它每合并一次,所在集合的规模扩大两倍,最多只会合并 logN 次,因而对于所有元素,至多插入set中NlogN次,时间复杂度就有上限 O(N∗logN∗logN)

现在主要是dsu on tree,它用来解决这样一类问题:统计树上一个节点的子树中具有某种特征的节点数。 基本上就是树上子树的统计问题。

但是dsu on tree也有一定的局限性:1.只能支持子树查询;2.不支持修改修改。

看例题来说:

D - Lomsat gelralCodeForces - 600E

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

Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.

The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.

For each vertex v find the sum of all dominating colours in the subtree of vertex v.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of vertices in the tree.

The second line contains n integers ci (1 ≤ ci ≤ n), ci — the colour of the i-th vertex.

Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the edge of the tree. The first vertex is the root of the tree.

Output

Print n integers — the sums of dominating colours for each vertex.

Examples

Input
  1. 4
    1 2 3 4
    1 2
    2 3
    2 4
Output
  1. 10 9 3 4
Input
  1. 15
    1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
    1 2
    1 3
    1 4
    1 14
    1 15
    2 5
    2 6
    2 7
    3 8
    3 9
    3 10
    4 11
    4 12
    4 13
Output
  1. 6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
  2.  
  3. 题意:给定一个 n 个节点的树,对于每个子树,输出子树中出现次数最多的节点编号之和。(次数最多的编号有多个节点都要统计进去)。 
    想法:最初的想法是对于每个点都跑一遍,暴力跑每个点的子树,然后记录下来答案,然后清空数组跑下一次,但是这样的话就时间复杂度就非常的高。
    所以要优化一下,我们都知道每个点有子树的话,会有一个重子树(就是点相对最多的那颗子树)。所以我们先预处理一下找到所有节点重子树。
    然后对于每科子树,我们先跑每颗子树的轻子树,跑完之后再跑重子树。在计算结果的时候,假设某一个点的儿子都已经被dfs过,统计这个点的答案。统计答案的过程中要calc当前这个点的子树,但是只calc它的轻链,重链不做。 
    这样的话,就需要在dfs的过程中,如果当前点是它父亲的轻儿子,做完这个点之后就将影响消除;而如果这个点是它父亲的重儿子,则将这个点的影响保留。 
    我们看下代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <vector>
  7. using namespace std;
  8. typedef long long LL;
  9. const int maxn=1e5+;
  10. int n,m;
  11. int mx,big;
  12. LL sum=;
  13. LL ans[maxn];
  14. int col[maxn],si[maxn],hson[maxn],cnt[maxn];
  15. vector<int>G[maxn];
  16.  
  17. void findhson(int x,int fa)//找到所有的重儿子
  18. {
  19. si[x]=;
  20. int len=G[x].size();
  21. for(int i=;i<len;i++)
  22. {
  23. int t=G[x][i];
  24. if(t!=fa)
  25. {
  26. findhson(t,x);
  27. si[x]+=si[t];
  28. if(si[t]>si[hson[x]])
  29. hson[x]=t;
  30. }
  31. }
  32. }
  33. void cal(int x,int fa,int val)//计算的过程,就是不断递归求颜色的数量
  34. {
  35. cnt[col[x]]+=val;
  36. if(cnt[col[x]]>mx)
  37. {
  38. sum=col[x];
  39. mx=cnt[col[x]];
  40. }
  41. else if(cnt[col[x]]==mx)
  42. sum+=col[x];
  43. int len=G[x].size();
  44. for(int i=;i<len;i++)
  45. {
  46. int t=G[x][i];
  47. if(t!=fa && t!=big)
  48. cal(t,x,val);
  49. }
  50. }
  51. void dfs(int x,int fa,int flag)//flag作为标记,看是轻子树还是重子树
  52. {
  53. int len=G[x].size();
  54. for(int i=;i<len;i++)//先跑轻子树
  55. {
  56. int t=G[x][i];
  57. if(t!=fa && t!=hson[x])
  58. dfs(t,x,);
  59. }
  60. if(hson[x])//再跑重子树
  61. {
  62. dfs(hson[x],x,);
  63. big=hson[x];
  64. }
  65. cal(x,fa,);
  66. big=;
  67. ans[x]=sum;
  68. if(!flag)//如果是轻子树的话,消除影响
  69. {
  70. cal(x,fa,-);
  71. mx=;sum=;
  72. }
  73. }
  74. int main()
  75. {
  76. sum=;big=;mx=;
  77. scanf("%d",&n);
  78. for(int i=;i<=n;i++)
  79. scanf("%d",&col[i]);
  80. int x,y;
  81. for(int i=;i<n;i++)//双向边
  82. {
  83. scanf("%d %d",&x,&y);
  84. G[x].push_back(y);
  85. G[y].push_back(x);
  86. }
  87. findhson(,);
  88. dfs(,,);
  89. for(int i=;i<=n;i++)
  90. {
  91. if(i==)
  92. printf("%lld",ans[i]);
  93. else
  94. printf(" %lld",ans[i]);
  95. }
  96. printf("\n");
  97. return ;
  98. }
 
  1.  

启发式合并 CodeForces - 600E的更多相关文章

  1. 启发式合并CodeForces - 1009F

    E - Dominant Indices CodeForces - 1009F You are given a rooted undirected tree consisting of nn vert ...

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

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

  3. Codeforces - 600E 树上启发式合并

    题意:求每一个子树存在最多颜色的颜色代号和(可重复) 本题是离线统计操作,因此可以直接合并重儿子已达到\(O(nlogn)\)的复杂度 PS.不知道什么是启发式合并的可以这样感受一下:进行树链剖分,分 ...

  4. Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map

    E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...

  5. CodeForces 958F3 Lightsabers (hard) 启发式合并/分治 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8835443.html 题目传送门 - CodeForces 958F3 题意 有$n$个球,球有$m$种颜色,分 ...

  6. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  7. codeforces#1166F. Vicky's Delivery (Service并查集+启发式合并)

    题目链接: https://codeforces.com/contest/1166/problem/F 题意: 给出节点数为$n$,边数为$m$的图,保证每个点对都是互连的 定义彩虹路:这条路经过$k ...

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

    codeforces 600E E. Lomsat gelral 传送门:https://codeforces.com/contest/600/problem/E 题意: 给你一颗n个节点的树,树上的 ...

  9. Codeforces 1455G - Forbidden Value(map 启发式合并+DP)

    Codeforces 题面传送门 & 洛谷题面传送门 首先这个 if 与 end 配对的结构显然形成一个树形结构,考虑把这棵树建出来,于是这个程序的结构就变为,对树进行一遍 DFS,到达某个节 ...

随机推荐

  1. 警告框在asp.net mvc中应用

    Bootstrap与asp.net MVC框架结合,产生警告框.主要是利用控制器的TempData 字典对象 生成临时数据. ASP.NET MVC的TempData用于传输一些临时的数据,例如在各个 ...

  2. 2 socket相关概念

    嘿嘿 这只是学习过程中的笔记积累,百度也是一代吧,大神就勿喷勒..... 1 为什么把网络编程接口叫做套接字 socket字面意思为插座 插孔,让人联想到电话,这种简单的设备给人类太大的方便 2 根据 ...

  3. 洛谷 P2761 软件补丁问题 【spfa】

    -为什么最短路的题会出现在网络流24里?? 因为范围是15所以直接把每个状态作为一个点,向它能转移到的点连有向边即可.可以不用建图(据说建图存不下?),直接枚举m个转移方案.位运算比较麻烦注意不要写错 ...

  4. bzoj2131: 免费的馅饼(树状数组)

    Description Input 第一行是用空格隔开的二个正整数,分别给出了舞台的宽度W(1到10^8之间)和馅饼的个数n(1到10^5). 接下来n行,每一行给出了一块馅饼的信息.由三个正整数组成 ...

  5. Java标准输入流system.in报错: java.util.NoSuchElementException解决方法

    我的程序大概是这样的: main()主函数里面,调用两个自定义的方法,这里我们称之为方法a和方法b: 主函数main()里有一个:Scanner scanner = new Scanner(Syste ...

  6. ORA-01144_表空间数据文件超出最大限制

    Oracle11gR2扩展表空间报ORA-01144错误. 数据块大小为8K的数据库,单个数据文件大小限制在32GB内. 解决办法: 1.增加表空间数据文件的方式: 2.创建BIGFILE表空间:

  7. pycharm的使用小技巧111

    如果你想快速敲出if __name__ == '__main__':只需你敲个main 然后回车就ok了 import和from xx模块 import *的区别是前者使用时要加模块名加点,后者可以直 ...

  8. fullpagejs实现的拥有header和foooter的全屏滚动demo/fullpage footer

    fullpagejs实现的拥有header和foooter的全屏滚动, 技术要点:给section元素加fp-auto-height类, <!DOCTYPE html> <html ...

  9. 如需在APP中使用腾讯QQ登陆,需提前申请获取腾讯QQ的APPKEY和APPSecret。

    如需在APP中使用腾讯QQ登陆,需提前申请获取腾讯QQ的APPKEY和APPSecret. 申请流程如下: 步骤1:登陆腾讯开放平台.链接地址: http://open.qq.com/ . 步骤2:填 ...

  10. C# 方法 虚方法的调用浅谈 引用kdalan的博文

    我们在面试中经常碰到有关多态的问题,之前我也一直被此类问题所困扰,闹不清到底执行哪个方法. 先给出一道简单的面试题,大家猜猜看,输出是?     public class A    {         ...