DSU on tree
Description
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
题意
有一棵有根树,根是1,每个点有颜色,求出以每个点为根的子树中,出现次数最多的颜色的编号和。
比方说我的子树里面有5个1色,5个2色,5个3色,4个4色,2个5色,1个6色,1个7色,那我的答案就是1+2+3。
分析
并不复杂的DSU on tree,也许树莫队啊LCT啊线段树合并啊主席树合并啊都可以过(也许吧没试过)。
何谓DSU on tree呢?
就这道题而言。
int mxcnt=0,sum=0,cnt[]={0};
//mxcnt代表出现最多次的颜色的数量,sum代表下标和,也就是答案,cnt是个桶,记录各个颜色它出现的次数
void update(int pos,int flg,int bad)//flg为1或-1,为1代表统计,即往桶里塞东西;为-1代表取东西。bad代表是否规避某个点。
{
cnt[col[pos]]+=flg;
if(flg>0 && cnt[col[pos]]==mxcnt) sum+=col[pos];
if(flg>0 && cnt[col[pos]]>mxcnt) mxcnt=cnt[col[pos]],sum=col[pos];
for(pos的所有儿子i) if(i!=bad) update(i,flg,bad);
}
void dfs(int pos,int flg)//flg代表是否要清除这个点的影响
{
for(all pos的轻儿子i) dfs(i,1);
if(pos有重儿子) dfs(pos的重儿子,0);
update(pos,1,pos的重儿子); //只统计轻儿子的答案
ans[pos]=sum;
if(!flg) update(pos,-1,0),sum=0,mxcnt=0;//如果是作为轻儿子被访问,将记录清零,当然此时谁也不需要规避
}
这个算法在干什么?
如果我们可以把以每个节点为根的子树的答案都分别存起来,这样我们就可以非常舒服的想怎么求答案就怎么求,可是空间开不下,我们只有一个答案数组,所以只能跑一次存一次清空一次。
有人提出:不用每次都清零!你只需要——
对于每个结点,先递归它的轻儿子,然后清空答案数组,然后递归它的重儿子,但不清空答案数组。所以现在我们回到这个结点的时候,就有了重儿子的信息。然后我们就再暴力统计轻儿子的信息,然后求得这个结点的答案。如果这个结点是重儿子的话,就什么也不做,如果是轻儿子的话,就清空答案数组。
什么是暴力统计?就是你把整颗子树都dfs遍历一遍,会做吧?
这里用撤回更合适,因为你再遍历一遍子树顶多是 的,而且还能均摊;如果你memset,时间复杂度总是 的
为什么这么做是可行的?
……?
为什么这样做的效率有 呢?
因为我们处理每个点的时候,都知道了重儿子的贡献,我们暴力处理轻儿子,因为轻儿子比较轻,所以时间复杂度就比较少,我不会证明重链剖分的时间复杂度(太懒了),我觉得这个时间复杂度的证明和重链剖分的时间复杂度的证明是一样的,所以请各位自行证明(臭不要脸)。
以下是本题代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
inline void Read(int &p)
{
p=0;
char c=getchar();
while(c<'0' || c>'9') c=getchar();
while(c>='0' && c<='9')
p=p*10+c-'0',c=getchar();
}
const int MAXN=102030,MAXM=202030;
bool vis[MAXN];
int mx,col[MAXN];
long long sum,all[MAXN],ans[MAXN];
int n,u,v,cnt,head[MAXN],siz[MAXN],son[MAXN],to[MAXM],nxt[MAXM];
inline void addedge(int u,int v)
{
nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v;
}
void build(int pos)
{
vis[pos]=1,siz[pos]=1;
for(int i=head[pos];i;i=nxt[i])
if(!vis[to[i]])
{
build(to[i]),siz[pos]+=siz[to[i]];
if(siz[to[i]]>siz[son[pos]]) son[pos]=to[i];
}
}
void update(int pos,int flg,int rid)
{
all[col[pos]]+=flg;
if(flg>0)
{
if(all[col[pos]]==mx) sum+=col[pos];
if(all[col[pos]]>mx) sum=col[pos],mx=all[col[pos]];
}
for(int i=head[pos];i;i=nxt[i])
if(to[i]!=rid && siz[pos]>siz[to[i]]) update(to[i],flg,rid);
}
void dfs(int pos,int flg)
{
for(int i=head[pos];i;i=nxt[i])
if(to[i]!=son[pos] && siz[pos]>siz[to[i]]) dfs(to[i],1);
if(son[pos]) dfs(son[pos],0);
update(pos,1,son[pos]),ans[pos]=sum;
if(flg) update(pos,-1,0),sum=0,mx=0;
}
int main()
{
Read(n);
for(int i=1;i<=n;i++) Read(col[i]);
for(int i=2;i<=n;i++) Read(u),Read(v),addedge(u,v),addedge(v,u);
build(1),dfs(1,1);
for(int i=1;i<=n;i++) printf("%lld%s",ans[i],i==n?"\n":" ");
}
转载:https://blog.csdn.net/c20182030/article/details/102326029