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 ...
随机推荐
- <十>面向对象分析之UML核心元素之关系
关系 --->在UML中关系是非常重要的语义,它抽象出对象之间的联系,让对象构成特定的结构. 一,关联关系(association)
- 【<td>】使<td>标签内容居上
<td>有一个叫valign的属性,规定单元格内容的垂直排列方式.有top.middle.bottom.baseline这四个值. 所以,让TD中的内容都居上的实现方法是: <td ...
- store / cache 系列
### golang go-cache An in-memory key:value store/cache (similar to Memcached) library for Go, suitab ...
- winfrom dataGridView 自定义分页实现
Winfrom 基本处于忘光的阶段.先需要做个winfrom 的软件.然后自己扩展了DataGridView带分页的控件.废话不多说 上图先 现在一步步实现其效果. 1.添加用户控件 上图即可知道 ...
- IOS 使用CoreText实现表情文本URL等混合显示控件
实现了一个富文本视图控件.主要针对表情图片,文本字符,URL,等这种类型的文本进行显示. 源码地址 https://github.com/TinyQ/TQRichTextView 实现的效果如下图. ...
- map用法详解
转自:http://www.kuqin.com/cpluspluslib/20071231/3265.html Map是 STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在 ...
- Android selector item 属性大全(按钮按下不同效果)
<selector> 必须.必须是根元素.包含一个或多个<item>元素. Attributes: xmlns:and ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- HDU-4882 ZCC Loves Codefires
http://acm.hdu.edu.cn/showproblem.php?pid=4882 ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Ot ...
- C++调用matlab实例
这段代码是C++调用matab引擎的过程,代码的目的很简单,在C++中创建一个vector数组,然后将这个vector数组单位化.写这个代码的目的是学些C++与matlab之间的数据交互,以供日后参考 ...