题目
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 ≤ 10^5) — 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
思路
题意:给出一棵树,每个结点都有自己的颜色,对于每一个根节点,我们定义颜色c为子树出现次数最多的颜色,如果多种颜色相等都为最大,那么c应该为他们之中任何一个,求每个结点作为根节点时颜色c的总和(就是出现最多的颜色数字之和,多个并列相等应都加上).
解题思路:从题意很明显关键在于维护树的信息,如果暴力来做的话,对于每个点都要求,且清空数据,这样会很浪费复杂度,于是我们用到 树上启发式合并
Dsu on tree: 我们在暴力统计子树答案的时候,先暴力统计轻儿子的答案,最后统计重儿子的答案,在统计完轻儿子的答案后,我们消除轻儿子的影响,而重儿子由于最后统计,不需要消除影响,这样父亲结点就可以使用重儿子保留的信息,不需要再次访问重儿子,由于树链剖分后轻儿子所占结点每次至少除以二,这样复杂度也被优化到nlogn从而用暴力解决树上子树问题。
代码如下(模板)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
#include<cctype>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int mx=1e5+10;
struct node{
int to,next;
}e[mx<<1];
int head[mx],tot;
int son[mx],size[mx];
int n,c[mx],vis[mx];
ll sum,maxcnt,cnt[mx],ans[mx];
void init()
{
tot=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
}
void add(int u,int v)
{
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u,int fa)//最开始的划分轻儿子和重儿子
{
size[u]=1;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(v==fa) continue;
dfs(v,u);
size[u]+=size[v];
if(size[v]>size[son[u]])
son[u]=v;
}
}
void cal(int u,int fa,int k)//k的取值为1和-1,分别对应累加和清除
{
cnt[c[u]]+=k;
if(k>0&&cnt[c[u]]>=maxcnt)
{
if(cnt[c[u]]>maxcnt) sum=0,maxcnt=cnt[c[u]];
sum+=c[u];
}
for(int i=head[u];i!=-1;i=e[i].next)//计算子树
{
int v=e[i].to;
if(v!=fa&&!vis[v])//vis表示已经计算过了~ 重儿子就不需要算
cal(v,u,k);//递归计算子节点
}
}
void dsu(int u,int fa,int keep)//keep为1表示当前结点在重儿子子树中,需要保留答案
{
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(v!=fa&&v!=son[u])//先访问轻儿子
dsu(v,u,0);
}
if(son[u]) dsu(son[u],u,1),vis[son[u]]=1;//再访问重儿子的,记得vis标记
cal(u,fa,1);//暴力统计轻儿子的
ans[u]=sum;//最后轻儿子和重儿子的统计结果
if(son[u]) vis[son[u]]=0;//这个时候清除标记
if(!keep) cal(u,fa,-1),sum=maxcnt=0;//删掉轻儿子的结果
}
int main()
{
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&c[i]);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1,0);
dsu(1,0,1);
for(int i=1;i<=n;i++)
printf("%lld ",ans[i]);
return 0;
}