有这么一类问题,要求统计一棵树上与子树相关的某些信息,比如:在一棵所有节点被染色的树上,统计每棵子树上出现次数最多的颜色编号之和。

很自然的可以想到用DFS序+主席树去求解,但是编码复杂度很高;

然后我们可以想到DFS序+莫队解决,然而$O(n\sqrt{n})$的时间复杂度在数据较大的时候容易TLE;

有没有更优美一点的解法呢?DSU On Tree(据说叫树上启发式合并)可以以较小的编码复杂度在$O(n\log n)$的时间复杂度完成对于所有子树信息的统计。

先上模板

 void dsu(LL k,LL f,LL x){
fore(i,k,v)
if(v!=f&&v!=son[k])dsu(v,k,);//统计轻儿子所在的子树,不保留信息
if(son[k])
dsu(son[k],k,),hs=son[k];//统计重儿子所在子树,保留信息
calc(k,f,);//统计所需信息
hs=;ans[k]=sum;
if(!x)calc(k,f,-),mx=sum=;//如果是轻儿子,则清除信息
}

看上去除了每次保留了重儿子的信息和暴力也没啥区别。。。

但是实际上,只有dfs到轻边时,才会将轻边的子树中合并到上一级的重链,树剖后的每条链上至多有$log n$条轻边,所以每一个节点最多向上合并$log n$次,每次统计的复杂度是$O(n)$,整体复杂度为$O(nlog n)$

在竞赛中,DSU On Tree 是一个不错的trick,可以有效减少代码复杂度。然而其缺陷也是明显的,这种算法的适用范围非常狭窄,仅适用于对于子树信息的统计,并且不滋磁修改操作。


来看一道模板题:传送门

E. Lomsat gelral

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
4
1 2 3 4
1 2
2 3
2 4
output
10 9 3 4
input
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
6 5 4 3 2 3 3 1 1 3 2 2 1 2 3

题目大意:给出一棵树,每一个节点有一个颜色,统计以每一个节点为根的子树中出现次数最多的颜色(可能不止一种)的编号和。

套上面的模板就好啦。

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define foru(i,x,y) for(LL i=x;i<=y;i++)
#define fore(i,x,v) for(LL i=head[x],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
using namespace std;
typedef long long LL;
const LL N=2e5+;
struct edge{LL to,nxt;}e[N*];
LL head[N],siz[N],son[N],ans[N],cnt[N],c[N],n,sum,hs,mx,ne;
void add(LL a,LL b){e[++ne]=(edge){b,head[a]};head[a]=ne;} void dfs(LL k,LL f){
siz[k]=;son[k]=;
fore(i,k,v){
if(v==f)continue;
dfs(v,k);
siz[k]+=siz[v];
if(siz[v]>siz[son[k]])son[k]=v;
}//剖分轻重儿子
} void calc(LL k,LL f,LL x){
cnt[c[k]]+=x;
if(cnt[c[k]]>mx)sum=c[k],mx=cnt[c[k]];
else if(cnt[c[k]]==mx)sum+=c[k];
fore(i,k,v)
if(v!=f&&v!=hs)calc(v,k,x);
} void dsu(LL k,LL f,LL x){
fore(i,k,v)
if(v!=f&&v!=son[k])dsu(v,k,);//统计轻儿子所在的子树,不保留信息
if(son[k])
dsu(son[k],k,),hs=son[k];//统计重儿子所在子树,保留信息
calc(k,f,);//统计所需信息
hs=;ans[k]=sum;
if(!x)calc(k,f,-),mx=sum=;//如果是轻儿子,则清除信息
} int main(){
LL u,v;
scanf("%I64d",&n);
foru(i,,n)scanf("%I64d",&c[i]);
foru(i,,n-){
scanf("%I64d%I64d",&u,&v);
add(u,v);add(v,u);
}
dfs(,);
dsu(,,);
foru(i,,n)printf("%I64d ",ans[i]);printf("\n");
}

DSU On Tree——Codeforces 600E(E. Lomsat gelral)的更多相关文章

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

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

  2. Codeforces 600 E - Lomsat gelral

    E - Lomsat gelral 思路1: 树上启发式合并 代码: #include<bits/stdc++.h> using namespace std; #define fi fir ...

  3. Codeforces 600 E. Lomsat gelral (dfs启发式合并map)

    题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...

  4. 「CF 600E」 Lomsat gelral

    题目链接 戳我 \(Describe\) 给出一棵树,每个节点有一个颜色,求每个节点的子树中颜色数目最多的颜色的和. \(Solution\) 这道题为什么好多人都写的是启发式合并,表示我不会啊. 这 ...

  5. 树上启发式合并(dsu on tree)学习笔记

    有丶难,学到自闭 参考的文章: zcysky:[学习笔记]dsu on tree Arpa:[Tutorial] Sack (dsu on tree) 先康一康模板题吧:CF 600E($Lomsat ...

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

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

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

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

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

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

  9. 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 ...

随机推荐

  1. jenkins忘记登录密码解决方法

    第一步:修改配置文件 修改jenkins的配置文件,找到如下几行删除(删除前一定要备份) <useSecurity>true</useSecurity> <authori ...

  2. GaussDB数据dump实现完全同步

    问题背景:搭建服务后端容灾集群,服务正常时容灾DB需要从业务DB完全同步数据,服务异常时,容灾DB停止抽取数据,自动从探针采集业务数据. 解决方案:常用的有两种思路,一是从服务后端定时每天拉取业务DB ...

  3. [Java-基础]反射_Class对象_动态操作

    动态性 动态语言 在程序运行时,可以改变程序结构或变量类型,典型的语言: Python,ruby,javascript 如: function test(){ var s = "var a= ...

  4. 31. docker swarm 通过 service 部署 wordpress

    1. 创建 一个 overlay 的网络 driver docker network create -d overlay demo 查看网络列表 docker network ls 2. 创建mysq ...

  5. 每天一杯C_C89、C99、C11等之C语言标准

    C语言的伟大之处在于C语言还是一个国际标准,这只“无形的手”掌控者其他派生语言和计算机的各个方面.起关于C语言被发明之后,ANSI和ISO相继发布关于C语言的标准.关于C90和C99,C89和C99容 ...

  6. Spring Cloud Alibaba 教程 | Nacos(三)

    使用Nacos作为配置中心 前面我们已经介绍过滤Nacos是一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台.所以它可以作为注册中心和配置中心,作为注册中心Nacos可以让我们灵活配置多 ...

  7. AtCoder Beginner Contest 129

    ABCD 签到(A.B.C过水已隐藏) #include<bits/stdc++.h> using namespace std; ; int n,m,ans,f1[N][N],f2[N][ ...

  8. JavaScript面试题(珍爱生命,远离面试)

    1.使用 typeof bar === "object" 判断 bar 是不是一个对象有神马潜在的弊端?如何避免这种弊端? 使用 typeof 的弊端是显而易见的(这种弊端同使用 ...

  9. c#为什么要用事物

    一.事务的定义 所谓事务,它是一个操作集合,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.典型的例子就像从网上银行系统的帐户A转帐到帐户B,它经过两个阶段:1.从帐户A取出款项.2.把 ...

  10. zabbix3.4--配置微信告警

    1.注册企业微信 https://work.weixin.qq.com/ 2.注册好后登陆,点击“我的企业”,记录企业ID. 3.点击“应用管理”--“创建应用”,创建应用时添加接收告警的用户 4.添 ...