NC24623 Tree Decoration
题目
题目描述
Farmer John is decorating his Spring Equinox Tree (like a Christmas tree but popular about three months later). It can be modeled as a rooted mathematical tree with N (1 <= N <= 100,000) elements, labeled 1...N, with element 1 as the root of the tree. Each tree element e > 1 has a parent, PeP_ePe (1 <= \(P_e\) <= N). Element 1 has no parent (denoted '-1' in the input), of course, because it is the root of the tree.
Each element i has a corresponding subtree (potentially of size 1) rooted there. FJ would like to make sure that the subtree corresponding to element i has a total of at least \(C_i\) (0 <= \(C_i\) <= 10,000,000) ornaments scattered among its members. He would also like to minimize the total amount of time it takes him to place all the ornaments (it takes time K*\(T_i\) to place K ornaments at element i (1 <= \(T_i\) <= 100)).
Help FJ determine the minimum amount of time it takes to place ornaments that satisfy the constraints. Note that this answer might not fit into a 32-bit integer, but it will fit into a signed 64-bit integer.
For example, consider the tree below where nodes located higher on
the display are parents of connected lower nodes (1 is the root):
1
|
2
|
5
/ \
4 3
Suppose that FJ has the following subtree constraints:
Minimum ornaments the subtree requires
| Time to install an ornament
Subtree | |
root | C_i | T_i
--------+--------+-------
1 | 9 | 3
2 | 2 | 2
3 | 3 | 2
4 | 1 | 4
5 | 3 | 3
Then FJ can place all the ornaments as shown below, for a total
cost of 20:
1 [0/9(0)] legend: element# [ornaments here/ | total ornaments in subtree(node install time)]
2 [3/9(6)]
|
5 [0/6(0)]
/ \
[1/1(4)] 4 3 [5/5(10)]
输入描述
- Line 1: A single integer: N
- Lines 2..N+1: Line i+1 contains three space-separated integers: PiP_iPi, CiC_iCi, and TiT_iTi
输出描述
- Line 1: A single integer: The minimum time to place all the ornaments
示例1
输入
5
-1 9 3
1 2 2
5 3 2
5 1 4
2 3 3
输出
20
题解
知识点:贪心,DFS,树形dp。
每个节点为根的子树都有一个最小要求的装饰数量,显然叶子节点只能全挂上去,随后向上考虑。对于一个子树,肯定把装饰挂在花费最小的节点上,因此可以回溯同时更新子树最小值,同时还需要一个记录已经挂了多少的数组。
注意结果可能超 int 。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct edge {
int to, nxt;
}e[100007];
int h[100007], cnt;
int root, c[100007], t[100007];///某节点的需求装饰;某子树所有节点的最小t;
ll ans, csum[100007]; ///某子树已有装饰
void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = h[u];
h[u] = cnt++;
}
void dfs(int u) {
if (!~h[u]) {
ans += c[u] * t[u];
csum[u] = c[u];
return;
}
for (int i = h[u];~i;i = e[i].nxt) {
int v = e[i].to;
dfs(v);
csum[u] += csum[v];
t[u] = min(t[u], t[v]);
}
ans += max(c[u] - csum[u], 0LL) * t[u];
csum[u] = max(csum[u], 0LL + c[u]);
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(h, -1, sizeof(h));
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
int p;
cin >> p >> c[i] >> t[i];
if (p == -1) root = i;
else add(p, i);
}
dfs(root);
cout << ans << '\n';
return 0;
}
NC24623 Tree Decoration的更多相关文章
- 洛谷——P3018 [USACO11MAR]树装饰Tree Decoration
P3018 [USACO11MAR]树装饰Tree Decoration 比较水的一道树上模拟水题,更新每个点的价值为以这个点为根的子树中的价值最小值,同时更新以每个节点为根的$sum$值,即以这个节 ...
- 洛谷P3018 [USACO11MAR]树装饰Tree Decoration
洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...
- bzoj usaco 金组水题题解(2.5)
bzoj 2197: [Usaco2011 Mar]Tree Decoration 树形dp..f[i]表示处理完以i为根的子树的最小时间. 因为一个点上可以挂无数个,所以在点i上挂东西的单位花费就是 ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- bzoj Usaco补完计划(优先级 Gold>Silver>资格赛)
听说KPM初二暑假就补完了啊%%% 先刷Gold再刷Silver(因为目测没那么多时间刷Silver,方便以后TJ2333(雾 按AC数降序刷 ---------------------------- ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...
- DP---Mahjong tree
HDU 5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenl ...
- HDU 5379 Mahjong tree(dfs)
题目链接:pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little su ...
- HDU 5379——Mahjong tree——————【搜索】
Mahjong tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
随机推荐
- centos7 systemctl配置开机自启动服务
centos7使用systemctl替代原来/etc/init.d,按官方的说法是提高系统服务的运行效率.服务配置更加简单易用,对于一些自定义的服务来配置开机自启动,是真的香! 概念理解 它是服务管理 ...
- HashMap集合遍历随机性问题分析
一.原因分析 1.1 HashMap对象的遍历 HashMap的遍历是通过此类中字段table数组进行顺序遍历,原因如下所示: 1 #HashMap 迭代遍历源码 2 public final boo ...
- VSCode + GCC编译器(MinGW)开发环境中文字符乱码问题踩坑与解决办法
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- ONVIF网络摄像头(IPC)客户端开发—RTSP RTCP RTP加载H264视频流
前言: RTSP,RTCP,RTP一般是一起使用,在FFmpeg和live555这些库中,它们为了更好的适用性,所以实现起来非常复杂,直接查看FFmpeg和Live555源代码来熟悉这些协议非常吃力, ...
- [转帖]深入浅出分析LSM树(日志结构合并树)
https://zhuanlan.zhihu.com/p/415799237 目录 收起 零.前言 一.LSM树数据结构定义 二.插入操作 三.删除操作 四.修改操作 五.查询操作 六.合并操作 ...
- [转帖]学会使用Kafka(八)Kafka基本客户端命令操作
https://www.cnblogs.com/rexcheny/articles/9463811.html 主题管理 创建主题 1 kafka-topics.sh --bootstrap-serve ...
- [转帖]rsar - Extract data from plain-text sar files
sar -A -t -f /tmp/sa11 >/tmp/sar11 https://github.com/ryran/rsar When dealing with sysstat sar da ...
- [转帖]通过架设Cockpit服务 使用Web浏览器监测管理多个Linux服务器
Cockpit是一个易于使用,轻量级和简单但功能强大的工具,通过单个Web浏览器监视和管理多个远程Linux服务器. 如果你管理着一台 Linux 服务器,那么你可能正在寻找一个可靠的管理工具.为了这 ...
- [转帖]十大主流Nehalem服务器横评(多图)
https://server.51cto.com/article/201820.html 作者:佚名2010-05-25 09:52:27 运维服务器运维 除了我们的两台基准服务器之外,本次横向评测活 ...
- [转帖]Cgroups资源限制
https://cloud.tencent.com/developer/article/2108816?areaSource=105001.13&traceId=QzVtWN5jGl8zeYZ ...