#K95057. Manager Subtree Rating Average

    ID: 38778 Type: Default 1000ms 256MiB

Manager Subtree Rating Average

Manager Subtree Rating Average

In this problem, you are given the organizational hierarchy of a company. Each employee is represented by a unique identifier, a manager identifier, and a performance rating. The manager identifier is 0 if the employee has no manager. Given an integer (n) representing the number of employees followed by (n) lines describing each employee, and an integer (m) representing a manager's id, you are asked to compute the average performance rating of all employees in the subtree of the manager (m). The subtree of a manager includes the manager themselves and all employees who report directly or indirectly to them. The average is defined as [ \text{average} = \frac{\sum_{i \in S} rating_i}{|S|} ] where (S) is the set of employee ids in the subtree. If the manager with id (m) does not exist, output -1. The final answer should be rounded to two decimal places.

inputFormat

The input is read from standard input (stdin). The first line contains an integer (n), which is the number of employees. Each of the next (n) lines contains three space-separated integers representing an employee's id, their manager id (0 indicates no manager), and their performance rating. The last line contains an integer (m), the id of the manager for whom the average rating is to be calculated.

outputFormat

Output a single number to standard output (stdout): the average performance rating of the manager's subtree, rounded to two decimal places. If the manager does not exist, output -1.## sample

6
1 0 5
2 1 3
3 1 4
4 2 2
5 2 3
6 3 4
1
3.50