E. Lomsat gelral

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/600/problem/E

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.

Sample Input

4
1 2 3 4
1 2
2 3
2 4

Sample Output

10 9 3 4

HINT

题意

给你一棵树,告诉你每个节点的颜色,然后让你输出对于这个节点的子树中,出现次数最多的颜色的权值和是多少

题解:

启发式合并map

对于每一个子树,我们都维护一个map,然后从小的合并到大的中

均摊下来复杂度不会很高(雾

代码:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
using namespace std;
#define maxn 800005 map<int,int> H[maxn];
map<int,int>::iterator it;
long long ans[maxn];
vector<int> E[maxn];
int c[maxn];
int id[maxn];
long long M[maxn];
long long M1[maxn];
void uni(int &x,int y)
{
if(H[x].size()<H[y].size())swap(x,y);
for(it = H[y].begin();it!=H[y].end();it++)
{
H[x][it->first]+=it->second;
if(M1[x]==H[x][it->first])
M[x]+=it->first;
if(M1[x]<H[x][it->first])
{
M1[x]=H[x][it->first];
M[x]=it->first;
}
}
}
void solve(int x,int fa)
{
H[x][c[x]]=;
M1[x]=,M[x]=c[x];
for(int i=;i<E[x].size();i++)
{
if(E[x][i]==fa)continue;
solve(E[x][i],x);
uni(id[x],id[E[x][i]]);
}
ans[x]=M[id[x]];
}
long long flag = ;
int main()
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++)
{
id[i]=i;
scanf("%d",&c[i]);
}
for(int i=;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
solve(,-);
for(int i=;i<=n;i++)
printf("%lld ",ans[i]);
printf("\n");
return ;
}

Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map的更多相关文章

  1. Educational Codeforces Round 2 E - Lomsat gelral

    题意:每个节点有个值,求每个节点子树众数和 题解:可线段树合并,维护每个数出现次数和最大出现次数,以及最大出现次数的数的和 //#pragma GCC optimize(2) //#pragma GC ...

  2. Educational Codeforces Round 2 E. Lomsat gelral(dsu)

    题目链接 题意:给你一棵以1为根n个点的树,问你以i为根的子树的众数和是多少 思路:dsu是一种优化暴力的手段 首先进行轻重链剖分 然后只记录重链的信息 轻链的信息就直接暴力查找 经过证明这样复杂度可 ...

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

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

  4. CF600E Lomsat gelral (启发式合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

  5. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  6. Educational Codeforces Round 64 部分题解

    Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...

  7. Educational Codeforces Round 64(ECR64)

    Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...

  8. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  9. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

随机推荐

  1. 【WEB小工具】EncodingFilter—设置全局编码

    1.我们知道,如果是POST请求,我们需要调用request.setCharacterEncoding("utf-8") 方法来设计编码. public void doGet(Ht ...

  2. Java-泛型编程-使用通配符? extends 和 ? super

    原文地址:http://blog.csdn.net/fw0124/article/details/42296283 泛型中使用通配符有两种形式:子类型限定<? extends xxx>和超 ...

  3. [转]python类方法

    Python定义类-方法 公有方法.私有方法.类方法.静态方法

  4. Canvas处理头像上传

    未分类 最近社区系统需要支持移动端,其中涉及到用户头像上传,头像有大中小三种尺寸,在PC端,社区用Flash来处理头像编辑和生成,但该Flash控件的界面不友好而且移动端对Flash的支持不好,考虑到 ...

  5. [Everyday Mathematics]20150107

    设 $f\in C^1[a,b]$, $f(a)=0$, 且存在 $\lm>0$, 使得 $$\bex |f'(x)|\leq \lm |f(x)|,\quad \forall\ x\in [a ...

  6. WebView介绍

    本文主要对WebView进行介绍,包括webView 4个可以定制的点.设置WebView back键响应.控制网页的链接仍在webView中跳转.显示页面加载进度.处理https请求.利用addJa ...

  7. IOS 类别

    在编写面向对象的程序时,你经常希望向现有的类添加一些新的行为:你总是能够为对象提供使用这些新方法的新手段.当希望为现有的类增加新行为时,我们通常会创建子类,但是有时候子类并不方便.例如,你可能会希望为 ...

  8. 8个很有用的PHP安全函数,你知道几个?

    原文:Useful functions to provide secure PHP application 译文:有用的PHP安全函数 译者:dwqs 安 全是编程非常重要的一个方面.在任何一种编程语 ...

  9. java计时代码

    public class Test{ public static void main(String[] args) { long startMili=System.currentTimeMillis( ...

  10. centos 安装git 服务端

    // 在服务端安装好git后,开始安装gitosis 3.在服务器安装gitosis sudo yum install python python-setuptools cd /usr/local/s ...