Mobile coding works. A laptop is faster for long sessions.

Problem

A tree with n nodes numbered from 1 to n is represented by

the parent array t, where t[i] is the parent of node i, and

t[i] = 0 means that node i is the root of the tree.

The depth of a node is the number of edges on the path from that

node to the root. The root has depth 0, its children have depth 1,

and so on. The height of the tree is the maximum depth found in

the tree.

Write a program that reads n and the values of the parent array, and

prints the height of the tree.

Input format

Input input.txt

The first line contains the natural number `n`. The second line contains the `n` values of the parent array, separated by spaces. - `1 <= n <= 100` - `0 <= t[i] <= n` for every `i` in `1..n` - the array describes a valid tree - if the tree has a single node, its height is `0`

Output format

Output output.txt

The program prints a single natural number: the height of the tree.

Example

input
7

2 4 4 0 4 5 5
output
2

Ready to solve this challenge?

Create a free account to write code, submit solutions, and track your progress.

⌨️ Keyboard Shortcuts

Code Editor
Run Code
Ctrl Enter
Submit Code
Ctrl Shift Enter
Format Code
Shift Alt F
Toggle Comment
Ctrl /
Undo
Ctrl Z
Redo
Ctrl Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc

🔍 Interactive Debugger

0 / 0

Analyzing your code...

📦 Variables

No variables yet

📚 Call Stack
main() line 1
📤 Output