#HK4105. 「BalkanOI 2023 Day1」Car Race
「BalkanOI 2023 Day1」Car Race
Description
To attract more visitors and money to the once proud but now more or less abandoned industrial area of Maribor, the city built a race track on the site of the former Metalna factory (one of multiple large Maribor businesses that were forced to shut down in the early 1990s). The track is constructed in the form of a rooted tree of vertices. The vertices of the tree are numbered with integer numbers , with the root having the number .
Let the race begin! Initially, there are cars at some vertices of the tree. Every second, each car moves to the adjacent vertex in the direction towards the root. At any moment, if two or more cars happen to be simultaneously at the same vertex with a number greater than , they collide and cannot participate in the race any more. For vertex (the root), this rule does not hold; the root may hold any number of cars at any moment.
For each vertex , output the integer , which is defined as follows:
- If there was no car at vertex at the beginning of the race, is .
- Otherwise, if the car that started at vertex collides on its way to the root, then is .
- Otherwise, is the time when the car that started at vertex reaches the root.
Input format
The first line contains an integer , which represents the number of vertices in the tree.
The second line contains integers, namely . For each , denotes the parent of vertex ; it holds that .
The third line contains integers, namely . For each , is either or . If there is a car at vertex at the beginning of the race, then ; otherwise, .
Output format
Print the integers on a single line, separated by a single space.
Input bounds
- .
Subtasks
- (3 points) .
- (5 points) for each .
- (8 points) .
- (9 points) .
- (10 points) .
- (9 points) .
- (14 points) .
- (19 points) Each vertex has at most 3 neighbors (i.e., the root has at most 3 children, and all other vertices have at most 2 children).
- (23 points) No additional constraints.
5
0 1 1 3
0 1 1 1 1
-1 1 -1 -1 3
Vertex (the root) contained no car at the beginning of the race. It takes second for the car starting from vertex to arrive to the root, and seconds for the car starting from vertex to do the same. The cars starting from vertices 2 and collide on their way to the root (this happens at node ).