luogu#P9128. [USACO23FEB] Fertilizing Pastures G
[USACO23FEB] Fertilizing Pastures G
题目描述
There are pastures , connected by roads, such that they form a tree. Every road takes second to cross. Each pasture starts out with grass, and the ith pasture's grass grows at a rate of units per second. Farmer John is in pasture at the start, and needs to drive around and fertilize the grass in every pasture. If he visits a pasture with units of grass, it will need amount of fertilizer. A pasture only needs to be fertilized the first time it is visited, and fertilizing a pasture takes time.
The input contains an additional parameter .
- If , Farmer John must end at pasture .
- If , Farmer John may end at any pasture.
Compute the minimum amount of time it will take to fertilize every pasture and the minimum amount of fertilizer needed to finish in that amount of time.
输入格式
The first line contains and .
Then for each from to , there is a line containing and , meaning that there is a road connecting pastures and . It is guaranteed that .
输出格式
The minimum amount of time and the minimum amount of fertilizer, separated by spaces.
题目大意
题目描述
有 个顶点的树,经过节点之间的每一条边都需要 。每个顶点一开始的权值均为 ,第 个点的权值的增长速率为 。FJ 从 号顶点出发遍历整棵树。当 FJ 走到某个节点时,若该节点的权值为 ,则需要支出大小为 的费用。(当然,只需在第一次经过该节点时需要支出。)
给出一个参数 :
-
若 ,FJ 必须回到 号节点。
-
若 ,FJ 可以在任意节点结束他的遍历。
求遍历所有节点的最小时间和此时需要付出的最小的费用。
输入格式
第一行包括 和 。
第 行到第 行,包含两个整数 和 , 的含义见上文。 则表示节点 和 之间有一条边相连。
输出格式
两个整数:遍历所有节点的最小时间和此时需要付出的最小的费用。
$2 \le N \le 2 \times 10^5,T \in \{0,1\},1 \le a_i \le 10^8, 1 \le p_i < i$。
5 0
1 1
1 2
3 1
3 4
8 21
5 1
1 1
1 2
3 1
3 4
6 29
提示
Explanation for Sample 1
The optimal route for Farmer John is as follows:
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move back to node , which we already fertilized and so don't need to fertilize again.
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move back to node , which we already fertilized.
- At time , move back to node .
- At time , move to node , which now has grass and so needs fertilizer.
- At time , return to node .
This route takes time and uses fertilizer. It can be shown that is the least possible amount of time for any route that returns to node at the end and is the least possible fertilizer used for any route that returns to node and takes time.
Explanation for Sample 2
The optimal route for Farmer John is as follows:
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move back to node .
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move to node , which now has grass and so needs fertilizer.
- At time , move back to node , which we already fertilized and so don't need to fertilize again.
- At time , move to node , which now has grass and so needs fertilizer.
This route takes time and uses fertilizer. It can be shown that is the least possible amount of time for any route and is the least possible fertilizer used for any route that takes time.
SCORING
- Inputs :
- Inputs :
- Inputs and : No pasture is adjacent to more than three roads.