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

题解:题意就是定义一个节点的优势点为他的子树中出现次数最多的数字,(可能会有多个优势点),让你求每一个点的优势点的和;
可以用map记录每一个点的各个优势点,记录每点的子树中每个数字出现的次数,然后dfs。
参考代码:
 #include<bits/stdc++.h>
using namespace std;
#define clr(a,val) memset(a,val,sizeof(a))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int maxn=1e5+;
int n,cnt;
int col[maxn],head[maxn],dy[maxn];
ll ans[maxn];
struct Edge{
int to,nxt;
} edge[maxn<<];
map<int,int> mp[maxn];
map<int,ll> sum[maxn];
map<int,int>::iterator it1,it2;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>='' && ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} inline void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].nxt=head[u];
head[u]=cnt++;
} inline void dfs(int u,int fa)
{
for(int e=head[u];~e;e=edge[e].nxt)
{
int v=edge[e].to;
if(v==fa) continue;
dfs(v,u);
if(mp[dy[u]].size()<mp[dy[v]].size()) swap(dy[u],dy[v]);
for(it1=mp[dy[v]].begin();it1!=mp[dy[v]].end();it1++)
{
sum[dy[u]][mp[dy[u]][(*it1).fi]]-=(*it1).fi;
mp[dy[u]][(*it1).fi]+=(*it1).se;
sum[dy[u]][mp[dy[u]][(*it1).fi]]+=(*it1).fi;
}
}
ans[u]=(*sum[dy[u]].rbegin()).se;
} int main()
{
n=read();
for(int i=;i<=n;++i) col[i]=read(),dy[i]=i,mp[i][col[i]]=,sum[i][]=col[i];
int u,v;cnt=;clr(head,-);
for(int i=;i<n;++i)
{
u=read();v=read();
addedge(u,v);addedge(v,u);
}
dfs(,);
for(int i=;i<=n;++i) printf("%lld%c",ans[i],i==n? '\n':' '); return ;
}

CF600E Lomsat gelral (启发式合并)的更多相关文章

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

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

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

  3. 【学习笔记/题解】树上启发式合并/CF600E Lomsat gelral

    题目戳我 \(\text{Solution:}\) 树上启发式合并,是对普通暴力的一种优化. 考虑本题,最暴力的做法显然是暴力统计每一次的子树,为了避免其他子树影响,每次统计完子树都需要清空其信息. ...

  4. CF600E Lomsat gelral——线段树合并/dsu on tree

    题目描述 一棵树有$n$个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. 这个题意是真的窒息...具体意思是说,每个节点有一个颜色,你要找的是每个子树中颜色的众数 ...

  5. dsu on tree(CF600E Lomsat gelral)

    题意 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. dsu on tree 用来解决子树问题 好像不能带修改?? 暴力做这个题,就是每次扫一遍子树统 ...

  6. CF600E Lomsat gelral 和 CF741D Dokhtar-kosh paths

    Lomsat gelral 一棵以\(1\)为根的树有\(n\)个结点,每个结点都有一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号(若有数量一样的,则求编号和). \(n \le 10^ ...

  7. CF600E Lomsat gelral 树上启发式合并

    题目描述 有一棵 \(n\) 个结点的以 \(1\) 号结点为根的有根树. 每个结点都有一个颜色,颜色是以编号表示的, \(i\) 号结点的颜色编号为 \(c_i\)​. 如果一种颜色在以 \(x\) ...

  8. CF600E Lomsat gelral 【线段树合并】

    题目链接 CF600E 题解 容易想到就是线段树合并,维护每个权值区间出现的最大值以及最大值位置之和即可 对于每个节点合并一下两个子节点的信息 要注意叶子节点信息的合并和非叶节点信息的合并是不一样的 ...

  9. CF600E:Lomsat gelral(线段树合并)

    Description 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. Input 第一行一个$n$.第二行$n$个数字是$c[i]$.后面$n-1$ ...

随机推荐

  1. Python 基础 面向对象之二 三大特性

    Python 基础 面向对象之二 三大特性 上一篇主要介绍了Python中,面向对象的类和对象的定义及实例的简单应用,本篇继续接着上篇来谈,在这一篇中我们重点要谈及的内容有:Python 类的成员.成 ...

  2. js调用浏览器“打印”与“打印预览”

    用到html <object>标签,具体做法如下: 1.在html文档任意位置添加<object>标签: <div style="border: 1px sol ...

  3. nyoj 457-大小写互换

    457-大小写互换 内存限制:64MB 时间限制:1000ms 特判: No 通过数:18 提交数:21 难度:0 题目描述:       现在给出了一个只包含大小写字母的字符串,不含空格和换行,要求 ...

  4. ArcGIS API For Javascript :如何动态生成 token 加载权限分配的地图服务?

    一.需求 项目中我们通常会遇到为外协团队.合作友商提供地图服务的需求,因此对地图服务的权限需要做出分配. 二.现状 主流的办法是用用户和角色来控制,通常使用代理方式和用户名密码的方式来实现. 三.思路 ...

  5. epoll介绍及使用

    小程序功能:简单的父子进程之间的通讯,子进程负责每隔1s不断发送"message"给父进程,不需要跑多个应用实例,不需要用户输入. 首先上代码 #include<assert ...

  6. Linux安装redis数据库

    这几天在搞redis数据库,花了好大功夫,才成功安装在Linux上,这里将自己的安装步骤分享出来,同时也做个记录,备忘. 新人一枚,不对之处,请多指教! 首先登陆Linux服务器 Linux里,我习惯 ...

  7. 菜鸟系列Fabric源码学习 — peer节点启动

    Fabric 1.4 源码分析peer节点启动 peer模块采用cobra库来实现cli命令. Cobra提供简单的接口来创建强大的现代化CLI接口,比如git与go工具.Cobra同时也是一个程序, ...

  8. python 正确字符串处理(自己踩过的坑)

    不管是谁,只要处理过由用户提交的调查数据,就能明白这种乱七八糟的数据是怎么一回事.为了得到一组能用于分析工作的格式统一的字符串,需要做很多事情:去除空白符.删除各种标点符号.正确的大写格式等.做法之一 ...

  9. 修改json源码支持datetime序列化

    修改json源码支持datetime序列化 import json import datetime now = datetime.datetime.today() json.dumps(now) 抛出 ...

  10. Spring Cloud Gateway - 路由法则

    1. After Route Predicate Factory 输入一个参数:时间,匹配该时间之后的请求,示例配置: spring: cloud: gateway: routes: - id: af ...