Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map
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的更多相关文章
- Educational Codeforces Round 2 E - Lomsat gelral
题意:每个节点有个值,求每个节点子树众数和 题解:可线段树合并,维护每个数出现次数和最大出现次数,以及最大出现次数的数的和 //#pragma GCC optimize(2) //#pragma GC ...
- Educational Codeforces Round 2 E. Lomsat gelral(dsu)
题目链接 题意:给你一棵以1为根n个点的树,问你以i为根的子树的众数和是多少 思路:dsu是一种优化暴力的手段 首先进行轻重链剖分 然后只记录重链的信息 轻链的信息就直接暴力查找 经过证明这样复杂度可 ...
- codeforces 600E. Lomsat gelral 启发式合并
题目链接 给一颗树, 每个节点有初始的颜色值. 1为根节点.定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和. 每一个叶子节点是一个map ...
- CF600E Lomsat gelral (启发式合并)
You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- Educational Codeforces Round 64(ECR64)
Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
随机推荐
- liux之我用过的zip解压命令
用途说明 zip文件是一种常用的压缩文件格式,WinZip.WinRar等压缩软件都支持zip文件格式,就连java的jar包也是zip格式 的,Firefox插件xpi文件也是zip格式的.Linu ...
- 【转】第一次使用Android Studio时你应该知道的一切配置
原文网址:http://www.cnblogs.com/smyhvae/p/4390905.html [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.c ...
- Android开发 |常见的内存泄漏问题及解决办法
在Android开发中,内存泄漏是比较常见的问题,有过一些Android编程经历的童鞋应该都遇到过,但为什么会出现内存泄漏呢?内存泄漏又有什么影响呢? 在Android程序开发中,当一个对象已经不需要 ...
- Zend Framework 入门(4)—页面布局
Zend Framework 的页面布局模块——Zend_Layout——既可以跟 MVC 一起使用,也可以单独使用.本文只讨论与 MVC 一起使用的情况. 1. 布局脚本 在 application ...
- POJ 2136 Vertical Histogram
题意:按样例那样模拟…… 解法:模拟…… 代码: #include<stdio.h> #include<iostream> #include<algorithm> ...
- 大数据性能调优之HBase的RowKey设计
1 概述 HBase是一个分布式的.面向列的数据库,它和一般关系型数据库的最大区别是:HBase很适合于存储非结构化的数据,还有就是它基于列的而不是基于行的模式. 既然HBase是采用KeyValue ...
- C#获取汉字拼音
C#获取汉字拼音 using System; using System.Collections.Generic; using System.Text; using System.Text.Reg ...
- QT4.7.4-vs2008和vs2010的安装并编写测试程序
QT的安装着实费了我好大的劲,之前试过QT5.1.0与VS2010的安装,但是因为设置的地方太多,程序运行总是不成功,所以最后选用QT4.7.4和VS2008与VS2010来编写程序.写这篇文章来总结 ...
- 【随便走走】Vietnam
从来没有一个地方让我如此留念过. 初到越南印象就是乱,满街轰轰轰的摩托车,狭窄的街道,各种小酒店小商店.从机场出来的路上还看到了不少中国品牌如豪爵摩托等等. 落地办理了落地签,从大陆是不能办的.越 ...
- ExcelUtils 导表实例
@RequestMapping("/dealer/chargebook/exportv.htm") public void getChargeBookList(int epm ...