启发式合并 Codeforces600E

E. Lomsat gelral

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

4
1 2 3 4
1 2
2 3
2 4

output

Copy

10 9 3 4

input

Copy

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

Copy

6 5 4 3 2 3 3 1 1 3 2 2 1 2 3

 

题目大意:

有一棵以1为根的树,每个节点都有颜色,颜色由数字表示,求以每个节点为根的子树占领子树的颜色和,即子树中最多的颜色的数字表示相加(可能有多种最多颜色)

(我觉得再怎么写博客也挽救不了我的语言表达能力了…

 

启发式合并真是个玄妙的东西。

思路:

首先考虑下每个节点需要记录,并合并给父节点的东西。对于每个节点记录,每个颜色及其个数统计cnt,每个个数所包括的是该个数的颜色数字和sum。从根节点1dfs遍历,先计算出子树的答案,再合并至父亲,合并时依据小的合并到大的的原则,判断每个子节点子树所包含的颜色个数,根据这个判断大小,父亲比儿子小的话就swap一下继续记录,得到的最终结果仍是父亲的。

特别要注意,应该建立双向边,因为题目中给边的时候并没有告诉我们哪个是父亲哪个是儿子;

并且建立双向边时,前向星写法的to与nxt数组的大小要<<1!要<<1!要<<1!

 

上代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

const int maxn=1e5+10;

int n;
int c[maxn];

int ecnt;
int head[maxn],nxt[maxn<<1],to[maxn<<1];//双向边啊啊啊啊啊!

map <int,int> cnt[maxn];
map <int,long long> sum[maxn];
long long ans[maxn];

inline void addedge(int u,int v)
{
	to[++ecnt]=v;
	nxt[ecnt]=head[u];
	head[u]=ecnt;
}

void dfs(int u,int pre)
{
	cnt[u][c[u]]=1;//初始化
	sum[u][1]=c[u];//初始化 出现一次的颜色之和为c[u]
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];
		if(v==pre) continue;
		dfs(v,u);
		//小的合并到大的
		if(cnt[u].size()<cnt[v].size()){
			swap(cnt[u],cnt[v]);
			swap(sum[u],sum[v]);
		}
		for(map<int,int>::iterator it=cnt[v].begin();it!=cnt[v].end();++it){
			//sum[u][cnt[u][it->first]]-=it->first;
			cnt[u][it->first]+=it->second;
			sum[u][cnt[u][it->first]]+=it->first;
		}
		//清空!
		cnt[v].clear();
		sum[v].clear();
	}
	ans[u]=sum[u].rbegin()->second;
}

int main()
{
	while(~scanf("%d",&n)){
		//初始化
		ecnt=0;
		cnt[1].clear();sum[1].clear();
		memset(head,0,sizeof(head));
		//memset(to,0,sizeof(to));
		//memset(nxt,0,sizeof(nxt));
		for(int i=1;i<=n;++i){
			scanf("%d",&c[i]);
		}
		for(int i=1;i<n;++i){
			int x,y;
			scanf("%d%d",&x,&y);
			//因为题目给的数据是边,但并未指明哪个是儿子,难以确定上下,所以要建双向边
			addedge(x,y);
			addedge(y,x);
		}
		dfs(1,-1);
		for(int i=1;i<=n;++i){
			printf("%lld%c",ans[i],i==n?'\n':' ');	//妙啊
		}
	}

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值