CF600E Lomsat gelral (启发式合并)
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
4
1 2 3 4
1 2
2 3
2 4
10 9 3 4
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
6 5 4 3 2 3 3 1 1 3 2 2 1 2 3 题解:题意就是定义一个节点的优势点为他的子树中出现次数最多的数字,(可能会有多个优势点),让你求每一个点的优势点的和;
可以用map记录每一个点的各个优势点,记录每点的子树中每个数字出现的次数,然后dfs。
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define clr(a,val) memset(a,val,sizeof(a))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int maxn=1e5+;
int n,cnt;
int col[maxn],head[maxn],dy[maxn];
ll ans[maxn];
struct Edge{
int to,nxt;
} edge[maxn<<];
map<int,int> mp[maxn];
map<int,ll> sum[maxn];
map<int,int>::iterator it1,it2;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>='' && ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} inline void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].nxt=head[u];
head[u]=cnt++;
} inline void dfs(int u,int fa)
{
for(int e=head[u];~e;e=edge[e].nxt)
{
int v=edge[e].to;
if(v==fa) continue;
dfs(v,u);
if(mp[dy[u]].size()<mp[dy[v]].size()) swap(dy[u],dy[v]);
for(it1=mp[dy[v]].begin();it1!=mp[dy[v]].end();it1++)
{
sum[dy[u]][mp[dy[u]][(*it1).fi]]-=(*it1).fi;
mp[dy[u]][(*it1).fi]+=(*it1).se;
sum[dy[u]][mp[dy[u]][(*it1).fi]]+=(*it1).fi;
}
}
ans[u]=(*sum[dy[u]].rbegin()).se;
} int main()
{
n=read();
for(int i=;i<=n;++i) col[i]=read(),dy[i]=i,mp[i][col[i]]=,sum[i][]=col[i];
int u,v;cnt=;clr(head,-);
for(int i=;i<n;++i)
{
u=read();v=read();
addedge(u,v);addedge(v,u);
}
dfs(,);
for(int i=;i<=n;++i) printf("%lld%c",ans[i],i==n? '\n':' '); return ;
}
CF600E Lomsat gelral (启发式合并)的更多相关文章
- Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map
E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...
- codeforces 600E. Lomsat gelral 启发式合并
题目链接 给一颗树, 每个节点有初始的颜色值. 1为根节点.定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和. 每一个叶子节点是一个map ...
- 【学习笔记/题解】树上启发式合并/CF600E Lomsat gelral
题目戳我 \(\text{Solution:}\) 树上启发式合并,是对普通暴力的一种优化. 考虑本题,最暴力的做法显然是暴力统计每一次的子树,为了避免其他子树影响,每次统计完子树都需要清空其信息. ...
- CF600E Lomsat gelral——线段树合并/dsu on tree
题目描述 一棵树有$n$个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. 这个题意是真的窒息...具体意思是说,每个节点有一个颜色,你要找的是每个子树中颜色的众数 ...
- dsu on tree(CF600E Lomsat gelral)
题意 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. dsu on tree 用来解决子树问题 好像不能带修改?? 暴力做这个题,就是每次扫一遍子树统 ...
- CF600E Lomsat gelral 和 CF741D Dokhtar-kosh paths
Lomsat gelral 一棵以\(1\)为根的树有\(n\)个结点,每个结点都有一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号(若有数量一样的,则求编号和). \(n \le 10^ ...
- CF600E Lomsat gelral 树上启发式合并
题目描述 有一棵 \(n\) 个结点的以 \(1\) 号结点为根的有根树. 每个结点都有一个颜色,颜色是以编号表示的, \(i\) 号结点的颜色编号为 \(c_i\). 如果一种颜色在以 \(x\) ...
- CF600E Lomsat gelral 【线段树合并】
题目链接 CF600E 题解 容易想到就是线段树合并,维护每个权值区间出现的最大值以及最大值位置之和即可 对于每个节点合并一下两个子节点的信息 要注意叶子节点信息的合并和非叶节点信息的合并是不一样的 ...
- CF600E:Lomsat gelral(线段树合并)
Description 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. Input 第一行一个$n$.第二行$n$个数字是$c[i]$.后面$n-1$ ...
随机推荐
- mysql里面的时间获取(格式年月日)
1.当前日期 select DATE_SUB(curdate(),INTERVAL 0 DAY) ; 2.明天日期 select DATE_SUB(curdate(),INTERVAL -1 DAY) ...
- 一个ip, 两个域名, 两个ssl, 访问多个不同的项目
在前面一篇中说过, 入了好几个坑. 后来使用了nginx+tomcat配置的方式. 终于成功了. 因为头一次使用nginx, 不知道具体怎么操作, 于是我在操作的时候, 按照以下几个步骤执行的: 导航 ...
- Maven系列第8篇:你的maven项目构建太慢了,我实在看不下去,带你一起磨刀!!多数使用maven的人都经常想要的一种功能,但是大多数人都不知道如何使用!!!
maven系列目标:从入门开始开始掌握一个高级开发所需要的maven技能. 这是maven系列第8篇. 整个maven系列的内容前后是有依赖的,如果之前没有接触过maven,建议从第一篇看起,本文尾部 ...
- nyoj 1364-治安管理 (INT_MAX)
1364-治安管理 内存限制:128MB 时间限制:3000ms 特判: No 通过数:6 提交数:6 难度:2 题目描述: SZ市是中国改革开放建立的经济特区,是中国改革开放的窗口,已发展为有一定影 ...
- linux文件时间
Linux 查看文件修改时间(精确到秒)(简单) ls --full-time 查看文件时间戳命令:stat test.txt linux 下查看文件修改时间 等(详细) 查看文件时间戳命令:stat ...
- x86汇编分页模式实验 --《ORANGE'S一个操作系统的实现》中 pmtest8.asm解析
序言(废话) : 在看书的过程中发现一开始不是很能理解pmtest8的目的,以及书上说得很抽象..于是在自己阅读过源代码后,将一些自己的心得写在这里. 正文 : 讲解顺序依然按照书上贴代码的顺序来.但 ...
- 面试官:你连RESTful都不知道我怎么敢要你?
目录 01 前言 02 RESTful的来源 03 RESTful6大原则 1. C-S架构 2. 无状态 3.统一的接口 4.一致的数据格式 4.系统分层 5.可缓存 6.按需编码.可定制代码(可选 ...
- 如何使用C#调用C++类虚函数(即动态内存调用)
本文讲解如何使用C#调用只有.h头文件的c++类的虚函数(非实例函数,因为非虚函数不存在于虚函数表,无法通过类对象偏移计算地址,除非用export导出,而gcc默认是全部导出实例函数,这也是为什么 ...
- setBounds方法,与setLayout(null)
首先把相关容器的布局方式设为 setLayout(null); 然后调用组件的 setBounds() 方法 设置button的位置为(100,100) 长宽分别为 60,25 jButton.se ...
- scrapy抓取国家社科基金项目数据库
1.明确任务 目标网站:http://fz.people.com.cn/skygb/sk/index.php/Index/seach 抓取任务:抓取近五年某关键词(例如"能源".” ...