题目链接

题目

题目描述

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的更多相关文章

  1. 洛谷——P3018 [USACO11MAR]树装饰Tree Decoration

    P3018 [USACO11MAR]树装饰Tree Decoration 比较水的一道树上模拟水题,更新每个点的价值为以这个点为根的子树中的价值最小值,同时更新以每个节点为根的$sum$值,即以这个节 ...

  2. 洛谷P3018 [USACO11MAR]树装饰Tree Decoration

    洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...

  3. bzoj usaco 金组水题题解(2.5)

    bzoj 2197: [Usaco2011 Mar]Tree Decoration 树形dp..f[i]表示处理完以i为根的子树的最小时间. 因为一个点上可以挂无数个,所以在点i上挂东西的单位花费就是 ...

  4. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  5. bzoj Usaco补完计划(优先级 Gold>Silver>资格赛)

    听说KPM初二暑假就补完了啊%%% 先刷Gold再刷Silver(因为目测没那么多时间刷Silver,方便以后TJ2333(雾 按AC数降序刷 ---------------------------- ...

  6. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  7. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  8. DP---Mahjong tree

    HDU  5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenl ...

  9. HDU 5379 Mahjong tree(dfs)

    题目链接:pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little su ...

  10. HDU 5379——Mahjong tree——————【搜索】

    Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

随机推荐

  1. mvn 编译异常:Fatal error compiling: 无效的标记: -parameters

    错误信息: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin::compile (defaul ...

  2. 08-避免Latch的产生

    1.Latch简介 Latch就是锁存器,是一种在异步电路系统中,对输入信号电平敏感的单元,用来存储信息 锁存器在数据未锁存时,输出端的信号随输入信号变化,就像信号通过一个缓冲器,一旦锁存信号有效,数 ...

  3. airsim+px4无人机仿真平台

    0. 架构图 1. 主机列表 对应的ip地址与选择的系统,根据实际情况进行修改 主机IP 组件 系统 192.168.0.28 mavporxy linux-centos7.6 192.168.0.2 ...

  4. 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.12.14)

    一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...

  5. [转帖]如何不耍流氓的做运维之-SHELL脚本

    https://www.cnblogs.com/luoahong/articles/8504691.html 前言 大家都是文明人,尤其是做运维的,那叫一个斯文啊.怎么能耍流氓呢?赶紧看看,编写SHE ...

  6. [转帖]TiUP 命令概览

    https://docs.pingcap.com/zh/tidb/stable/tiup-reference TiUP 在 TiDB 生态中承担包管理器的功能,管理着 TiDB 生态下众多的组件,如 ...

  7. SPECJVM2008 再学习

    SPECJVM2008 再学习 摘要 昨天的太水了 感觉今天有必要再水一点.. 存在的问题 默认进行启动 sunflow 必定过不去. 一般的解决办法要求进行重新编译 但是我不知道怎么下载源码... ...

  8. 最小化安装的CentOS7 上面安装Oracle12C的简单过程

    首先声明自己对静默安装不熟,也害怕初问题,所以不使用静默安装的方式. 因为是最小化安装,所以必须安装GUI界面才可以,以下是过程(早上回忆的,全文字,无截图) 1. 安装GUI界面 yum group ...

  9. [转贴]BLOCKED,WAITING,TIMED_WAITING有什么区别?-用生活的例子解释

    BLOCKED,WAITING,TIMED_WAITING有什么区别?-用生活的例子解释 https://www.jianshu.com/p/0976b2f23db1 https://dzone.co ...

  10. 【DP】DMOPC '21 Contest 8 P5 - Tree Building

    Problem Link 给定 \(n,m\) 和一个长为 \(m\) 的代价序列,对于一棵 \(n\) 个节点,每个节点度数不超过 \(m\) 的树,定义它的代价为 \(\sum\limits_{i ...