ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1027 Tree Decoration dfs+树的遍历

2022-07-04 01:02:51  阅读:133  来源: 互联网

标签:1027 num min int Tree Decoration cost ornaments 节点


链接:https://ac.nowcoder.com/acm/contest/23156/1027
来源:牛客网

题目描述

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 <= PeP_ePe​ <= 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 CiC_iCi​ (0 <= CiC_iCi​ <= 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*TiT_iTi​ to place K ornaments at element i (1 <= TiT_iTi​ <= 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

题意:

一棵树,给每个节点三个参数:它的父节点,它和它的子节点至少需要的物件数量,它买这种物件要花多少钱

想办法让所有节点满足它的最小物件数量,并使花钱量最小;

分析

先让每个叶子节点满足条件

然后肯定是叶子节点往上满足条件

如果当前节点及其子节点的物件数量小于它的要求数量,就让它或者它的叶子节点中的最小花费来买这么多数量的物件

这题其实关键在于我不知道怎么表示每个节点已经有多少数量的物件,还有它的叶子节点中的最小花费

然后看了题解,发现直接给每个节点开几个数组表示这个节点的最小花费和子节点的总数量就可以了



到最后发现自己一堆变量名写错,然后ll忘记了。。
但学会了一颗树怎么从叶子节点往上传递参数,
怎么从根节点往下传递参数。


//-------------------------代码----------------------------

#define int LL
const int N = 2e5+10;
int n,m;
V<int> edge[N];
int cost[N],num[N];
int cnt[N],min_cost[N];
int sum;


void dfs(int u) {
    min_cost[u] = cost[u];
    for(auto t:edge[u]) {
        dfs(t);
        cnt[u] += cnt[t];
        min_cost[u] = min(min_cost[u],min_cost[t]);
    }
    if(cnt[u] < num[u]) {
        sum += (num[u] - cnt[u]) * min_cost[u];
        cnt[u] = num[u];
    }
}

void solve() {
    cin>>n;
    fo(i,1,n) {
        int p;cin>>p;
        if(p == -1) {cin>>num[i]>>cost[i];}
        else {
            edge[p].pb(i);cin>>num[i]>>cost[i];
        }
    }
    dfs(1);
    cout<<sum<<endl;
}

signed main(){
    clapping();TLE;
    
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

标签:1027,num,min,int,Tree,Decoration,cost,ornaments,节点
来源: https://www.cnblogs.com/er007/p/16441438.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有