#K1226. Count of Subordinates

    ID: 23651 Type: Default 1000ms 256MiB

Count of Subordinates

Count of Subordinates

You are given an organization with N employees, structured as a tree where every employee (except the CEO) has a direct supervisor. Each relationship is provided as a pair (employee, supervisor). Your task is to compute the total number of subordinates (both direct and indirect) for each employee. This count for employee i, denoted by (s_i), is given by:

[ s_i = \sum_{j \in children(i)} (1 + s_j) ]

where (children(i)) represents the direct subordinates of employee i. The CEO is the unique employee who is not supervised by anyone. The answer should list subordinate counts for employees with IDs from 1 to N in increasing order.

inputFormat

Input is read from standard input (stdin). The first line contains an integer (N) representing the number of employees. Each of the next (N-1) lines contains two integers: the first is the employee's ID and the second is the supervisor's ID. It is guaranteed that the relationships form a valid tree with a single CEO.

outputFormat

Output the subordinate counts for each employee (from ID 1 to (N)) to standard output (stdout) on a single line, with counts separated by a single space.## sample

5
2 1
3 1
4 2
5 2
4 2 0 0 0