「CF600E」Lomsat gelral
传送门
Luogu
解题思路
线段树合并板子题(也可以 dsu on the tree)
好像没什么好讲的,就是要注意开 long long 。
细节注意事项
- 咕咕咕
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
typedef long long LL;
const int _ = 100010;
int tot, head[_], nxt[_ << 1], ver[_ << 1];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
int n, c[_];
int cnt, rt[_], lc[_ << 5], rc[_ << 5];
LL mx[_ << 5], col[_ << 5], ans[_];
inline void pushup(int p) {
if (mx[lc[p]] > mx[rc[p]]) mx[p] = mx[lc[p]], col[p] = col[lc[p]];
if (mx[lc[p]] < mx[rc[p]]) mx[p] = mx[rc[p]], col[p] = col[rc[p]];
if (mx[lc[p]] == mx[rc[p]]) mx[p] = mx[lc[p]], col[p] = col[lc[p]] + col[rc[p]];
}
inline void update(int& p, int x, int l = 1, int r = n) {
if (!p) p = ++cnt;
if (l == r) { ++mx[p], col[p] = l; return; }
int mid = (l + r) >> 1;
if (x <= mid) update(lc[p], x, l, mid);
else update(rc[p], x, mid + 1, r);
pushup(p);
}
inline int merge(int x, int y, int l = 1, int r = n) {
if (!x || !y) return x + y;
if (l == r)
return mx[x] += mx[y], col[x] = l, x;
int mid = (l + r) >> 1;
lc[x] = merge(lc[x], lc[y], l, mid);
rc[x] = merge(rc[x], rc[y], mid + 1, r);
return pushup(x), x;
}
inline void dfs(int u, int f) {
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
dfs(v, u), rt[u] = merge(rt[u], rt[v]);
}
update(rt[u], c[u]), ans[u] = col[rt[u]];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int i = 1; i <= n; ++i) read(c[i]), rt[++cnt] = i;
for (rg int u, v, i = 1; i < n; ++i)
read(u), read(v), Add_edge(u, v), Add_edge(v, u);
dfs(1, 0);
for (rg int i = 1; i <= n; ++i) printf("%lld ", ans[i]);
return 0;
}
完结撒花 \(qwq\)
「CF600E」Lomsat gelral的更多相关文章
- 【CF600E】Lomsat gelral(dsu on tree)
[CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...
- 【CF600E】 Lomsat gelral
CF600E Lomsat gelral Solution 考虑一下子树的问题,我们可以把一棵树的dfn序搞出来,那么子树就是序列上的一段连续的区间. 然后就可以莫队飞速求解了. 但是这题还有\(\T ...
- 「CF 600E」 Lomsat gelral
题目链接 戳我 \(Describe\) 给出一棵树,每个节点有一个颜色,求每个节点的子树中颜色数目最多的颜色的和. \(Solution\) 这道题为什么好多人都写的是启发式合并,表示我不会啊. 这 ...
- 【CF600E】Lomsat gelral
题目大意:给定一棵 N 个节点的有根树,1 号节点是树的根节点,每个节点有一个颜色.求对于每个节点来说,能够支配整棵子树的颜色之和是多少.支配的定义为对于以 i 为根的子树,该颜色出现的次数不小于任何 ...
- 题解 CF600E 【Lomsat gelral】
没有多少人用莫队做吗? 蒟蒻水一波莫队 这是一道树上莫队好题. 时间复杂度(\(n\sqrt{n}logn\)) 蒟蒻过菜,不会去掉logn的做法qaq 思路很简单: 1.dfs跑一下树上点的dfs序 ...
- 【CF600E】Lomsat gelral——树上启发式合并
(题面来自luogu) 题意翻译 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. ci <= n <= 1e5 裸题.统计时先扫一遍得到出 ...
- CF600E Lomsat gelral 和 CF741D Dokhtar-kosh paths
Lomsat gelral 一棵以\(1\)为根的树有\(n\)个结点,每个结点都有一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号(若有数量一样的,则求编号和). \(n \le 10^ ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
随机推荐
- WordPress 网站迁移
最近想把本地的WordPress迁移到我的Linux虚拟机里面,是不是很无聊,哈哈哈,接下来就是一过程了,其实这个和迁移到线上是一样的, 1.首先将本地的文件WordPress通过FTP传到虚拟机上: ...
- 28 JavaScript语言类型&运算符
语言类型: 弱类型:可以改变变量值和对象类型 强类型:可以改变变量值不能改变对象类型 解释型:边编译边执行,速度慢.解释型一般是弱类型 编译型:先编译再执行(C++\Java),速度快.编译型一般是强 ...
- ES5-bind用法及与以前的apply和call
当我们调用一个函数的时候,函数中的this一般是指向调用者的.但是我们其实可以在调用函数的时候,传入一个对象,让函数中的this指向我们传入的对象,而不是调用者本身. apply,call,bind都 ...
- N3K license安装
1.获取设备SN和PAK SN获取: Switch#show license host-id 注意:IOS设备中为:show license udi PAK获取: PAK是单独购买license后,c ...
- 中国5G,如何避免重复投资?
前不久,工信部正式向中国移动.中国联通.中国电信发放5G商用牌照,此举意味着中国提前启动5G商用计划,随之而来的,将会是运营商.设备商大规模的投资.相关数据机构预测,三大运营商2019年预计会投入30 ...
- php学习 打星星
<?php // 输入几行 $k=9; // 打孔三角 for($i=1;$i<=$k;$i++){ if($i==$k){ for($n=1;$n<=$k*2-1;$n++){ e ...
- [运维] 如何访问虚拟机上的 Tomcat ?
环境: 虚拟机: VMware 15 pro 操作系统 Linux CentOS 7 64 物理机: Windows 7 事先准备: 1: 下载 Tomcat 的压缩包 apache-t ...
- 学习 Python,怎能不懂点PEP 呢?
或许你是一个初入门 Python 的小白,完全不知道 PEP 是什么.又或许你是个学会了 Python 的熟手,见过几个 PEP,却不知道这玩意背后是什么.那正好,本文将系统性地介绍一下 PEP,与大 ...
- ios 物流时间轴,自动匹配电话号码,可点击拨打
http://www.code4app.com/thread-27587-1-1.html 资讯时间轴(折叠/展开) http://www.code4app.com/thread-32358-1-1. ...
- 后台框架 FastAdmin V1.0.0.20200228 发布,为疫情防控作贡献
后台框架 FastAdmin V1.0.0.20200228 发布,为疫情防控作贡献 https://www.oschina.net/news/113694/fastadmin-1-20200228- ...