Cave Escape

View as PDF

Points: 17
Time limit: 1.0s
Memory limit: 256M

Authors:
Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Rico and Reg are running from the authorities as they descend into the first floor of the abyss. The authorities are trying to catch them, and asked you for help!

The first floor of the abyss is an interconnected network of caves and tunnels. The entrance is located at cave , which the authorities wish to force Rico and Reg to. Their method of forcing the adventurers out from the caves is a series of traps, one in each cave, that when activated, force any adventurers in that cave to move to a connected cave that is closer to the entrance.

The authorities try to catch Rico and Reg over a series of days. At the start of each day, traps can be turned on or off. Each trap is initially off and can be turned on exactly once. Traps that are on can also be turned off, but cannot be turned on again afterwards. Then, Rico and Reg can repeatedly travel from the node that they occupy to another cave that it is connected to(but if they travel to a node with a trap that is on, or a trap is activated in their current node, they are immediately forced to move, however, if the are moved by another trap, they will not be forced to move again). Note that they cannot "skip past" caves that have activated traps. (Example: if caves , , , , are connected in that order, and cave has an activated trap, it is impossible to move from cave , since upon moving to , they would immediately be sent back to ). Also note that Rico and Reg do not move while traps are being switched on or off.

Let the power be the maximum number of traps that must be on during a single day, assuming the authorities try to minimize this values. Find the minimum power required to force the adventurers back to cave , if both parties move optimally.

Input Format

Line : A single integer

Next lines: An integer , representing an edge from parent to .

Output Format

A single integer, the minimum power required.

Sample Input

10
0
0
0
2
2
1
2
3
0

Sample Output

4