#C7657. Organizational Hierarchy Levels
Organizational Hierarchy Levels
Organizational Hierarchy Levels
Given the reporting structure of an organization, determine the number of levels in its hierarchy. The structure is provided as a list of reporting relationships, where each relationship is represented by a pair of integers (a, b) indicating that employee b directly reports to employee a. The organizational chart is structured as a tree with a single CEO, who does not report to anyone.
The number of levels L in the hierarchy can be thought of as the maximum distance from the CEO to any employee, where the CEO is considered to be at level 1. In mathematical terms, if the distance from the CEO to the furthest employee is d, then L = d + 1.
If the input does not represent a valid hierarchy (i.e. if there is not exactly one CEO), output Invalid hierarchy
.
inputFormat
The input is read from standard input (stdin
). The first line contains an integer n representing the number of reporting relationships. Each of the next n lines contains two space‐separated integers a and b, indicating that employee b directly reports to employee a.
outputFormat
Output a single line to standard output (stdout
) with an integer representing the number of levels in the organizational hierarchy. If the hierarchy is invalid (i.e. if there is not exactly one CEO), print Invalid hierarchy
.
5
1 2
1 3
2 4
3 5
4 6
4