#C3013. Employee Hierarchy Report

    ID: 46394 Type: Default 1000ms 256MiB

Employee Hierarchy Report

Employee Hierarchy Report

You are given a list of employees and their reporting relationships. Each employee is represented by three values: an emp_id, an emp_name, and a manager_id. A manager_id of -1 indicates that the employee has no manager.

Your task is to identify all employees who directly or indirectly report to a specified manager. For each such employee, also output the level of reporting, where level 1 indicates a direct report, level 2 indicates a report of a direct report, and so on.

The output must be sorted by the reporting level first, and then by the employee name in alphabetical order within the same level.

The formula for the level of any employee in the reporting hierarchy with respect to the queried manager is given by:

\( level = 1 + \text{distance from the manager in the hierarchy} \)

inputFormat

The input is read from standard input. The format is as follows:

  1. An integer N representing the number of employees.
  2. N lines follow, each containing three values separated by spaces: emp_id (integer), emp_name (string without spaces), and manager_id (integer). If an employee has no manager, manager_id will be given as -1.
  3. An integer that represents the manager_emp_id of the manager whose direct and indirect reports are to be found.

outputFormat

The output should be written to standard output. For each employee that reports (directly or indirectly) to the specified manager, print a line with the employee's name and the level of reporting separated by a space. The lines must be ordered first by the level (in ascending order) and then by the employee name (in alphabetical order). If no employee reports to the specified manager, output nothing.

## sample
3
1 John -1
2 Sarah 1
3 Alice 1
1
Alice 1

Sarah 1

</p>