1027 Tree Decoration dfs+树的遍历

链接: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;
}

/*样例区


*/

//------------------------------------------------------------
posted @ 2022-07-04 00:34  er007  阅读(23)  评论(0编辑  收藏  举报