#K1161. Employee Hierarchy Depths

    ID: 23507 Type: Default 1000ms 256MiB

Employee Hierarchy Depths

Employee Hierarchy Depths

You are given a company hierarchy consisting of n employees with unique IDs from 1 to n. Employee with ID 1 is the CEO and has no manager (its depth is 0). For employees with IDs from 2 to n, you are given their direct manager's ID. Your task is to compute the depth of each employee in the hierarchy, where the depth of an employee is defined as the number of managers between that employee and the CEO.

Formally, let \(d_i\) be the depth of employee \(i\). Then \(d_1 = 0\) (for the CEO), and for any employee \(i\) (with \(2 \le i \le n\)), if the direct manager of employee \(i\) is \(m\), then \(d_i = d_m + 1\).

You are to read the input from stdin and write the output to stdout.

inputFormat

The input is given in two lines:

  • The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) representing the number of employees.
  • If \(n > 1\), the second line contains \(n-1\) space-separated integers. The \(i\)-th integer in this line is the manager's ID of the employee with ID \(i+1\).

If \(n = 1\), the input contains only the first line.

outputFormat

Output a single line with \(n\) space-separated integers, where the \(i\)-th integer denotes the depth of the employee with ID \(i\).

## sample
1
0

</p>