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 ...
随机推荐
- jquery生成二维码
下载Demo js下载: jquery-1.8.3.js .qrcode.js.jquery.qrcode.js <!DOCTYPE html> <html> <head ...
- jQuery点击div其他地方隐藏div
$(document).bind("click",function(e){ var target = $(e.target); ){ $("#regionlist&quo ...
- Android下载速度计算
long startTime = System.currentTimeMillis(); // 开始下载时获取开始时间 long curTime = System.currentTimeMillis( ...
- duilib List控件,横向滚动时列表项不移动或者移动错位的bug的修复
转载请说明出处,谢谢~~ 这篇博客已经作废,只是留作记录,新的bug修复博客地址:http://blog.csdn.net/zhuhongshu/article/details/42264673 之前 ...
- PHP 正则表达式总结
可以用字符作为一个通配符来代替除换行符(\n)之外的任一个字符.例如,正则表达式:.at可以与"cat"."sat"."#at"和" ...
- springMVC(注解版笔记)
springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...
- SqlHelper 带详细中文注释
using System; using System.Collections.Generic; using System.Linq; using System.Text; //对数据库进行操作引入命名 ...
- 多元线性回归(Linear Regression with multiple variables)与最小二乘(least squat)
1.线性回归介绍 X指训练数据的feature,beta指待估计得参数. 详细见http://zh.wikipedia.org/wiki/%E4%B8%80%E8%88%AC%E7%BA%BF%E6% ...
- Tkinter教程之Text篇(3)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811348 '''Tkinter教程之Text篇(3)''''''14.自定义tag的两个内置 ...
- [Hive - LanguageManual ] ]SQL Standard Based Hive Authorization
Status of Hive Authorization before Hive 0.13 SQL Standards Based Hive Authorization (New in Hive 0. ...