DAY 2 T2 simulation game is not the old dream

[Title] background
Fortunately for people to meet in an instant Che Ming Tong net.
I become invincible courage and fear, split mountain and sea, the sky falling.
If you love torch-wind, blazing and mourning, in which all sorts of taste.
Shaohua chant reverberates, desolate glory, anxious scene withered in difficult soulful total.
- Silver Pro "old dream"
【Problem Description】
Buso a search of the song did not find a single song title can do the background of this question, then put the Fusu favorite
Of an "old dream."
And the second question Day1, like today's second question is still a topic tree.
We define a tree of n nodes is a node n and n 1-edges of undirected FIG.
If we define u T is the root of a tree, then any node v to the root path is to start from the v
Simple path of the point u the elapsed point set point. This can be proved and only a simple path.
Definition of a node x is a child node y, if and only if there is connected edges between x and y and y to x is not the root
Path. If x is y child, then the parent node is defined y x.
If I were the kind of cancer _rqy gods, then, you may ask the number of children for each node k does not exceed the n Festival
Labeled point unrooted trees, a total of how many, but I would not have this problem, so I will not ask you so malignant tumor
problem.
Fusu from No. 1 node of a tree of n nodes starting, running along the sides of the tree. Of course, we agreed to No. 1
This node is the root of the tree. He walks provisions are: u at the point when Buso, Fusu either choose u-olds
Did not reach the point of a nice parallel walked v v, u either choose to return to the parent node.
Now each node a weight w, wherein the weight value of the node number i wi. Buso some stones, he wanted to give
One node of this tree placed stones. We can put a stone in the provisions of Fusu node u if and only if the following
condition:
1, in the current node u Fusu
2, for all the children nodes v u, v nodes are placed on the wv stars stones.
However, Fusu can be retrieved at any time to any stones node.
Now, Fusu would like to ask you for each node, if he wanted to put the pieces of stone on i wi node number, so he
How many stones need to be prepared to start.
[Input Format]
Enter the file name yin.in.
Input file and only one set of data, the first data row n is an integer representative of the number of nodes of the tree.
The second row has n-1 integers, i-integer number representative of the number i + 1 pi parent node of the node.
The third row there are n integers, the i-th integer representing wi.
[Output format]
Output file name yin.out.
Output line n integers, the i represents an integer of stone teeth would like to put on the i wi number of nodes need to be prepared
The number of stones.
 
Sample 1 [O]
in
3
1 2
1 1 1
out
2 2 1
 
Explanation:
           
Sample input 1 as the left, each node needs to put a stone.
If the stone discharge node 1, from the point motion One to two dots, and then moved to Point 3, placed on a 3-point
Stone, return point 2, a stone at the 2 points, while the 3rd point to recover the stone, and then return point 1, the place to recover from Point 3 to Point 1 stones. A total need to be two stones.
2,3 node similar scheme put stone. 
 
Sample 2 [O]
in
3
1 1
1 1 1
out
3 1 1
Sample 3 [O]
in
6
1 1 2 3 4
3 14 1 5 12 15
out
21 20 13 20 12 15 
 
3 Sample input picture to the left.
No. 1 starting with node 3 to node movement, and then moves to the node 5, 12 placed on the stone sub-node 5, and node 3 returns, a stone is placed on the child node 3, node 12 is retracted V stone promoter, No. 1 node returns.
No. 2 is then moved to node 4 through node moving to node 6, 15 stones down, the node 4 returns down five stones, when the number of trees and some stone 5 (4) + 15 (6) +1 (3) = 21. Node 6 is then retracted stones, node 2 returns, down stone 14, to recover 4-node number, node returns the number 1, 3 is placed on the No. 1 node stone, gravel can be achieved in the discharge of the node No. 1 purpose.
You can verify the maximum cost of 21. The answer other nodes in the same way.
       

 

Guess you like

Origin www.cnblogs.com/lcezych/p/11093298.html