


You are given ‘manager’ = [-1, 0, 0, 1, 1], ‘timeToInform’ = [1, 1, 0, 0, 0], ‘headId’ = [0],

We can see employee ‘0’ will take 1 time to inform their subordinates, that are employee 1, and employee 2, and employee 1 will take 1 time to inform their subordinates that are employee 3 and employee 4. The total time taken is 2. Hence the answer is 2.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘headId’ representing the number of employees and the employee id of the head of the company.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the ‘manager’ array.
The third line of each test case contains ‘N’ space-separated integers representing the ‘timeToInform’ array.
For each test case, print a single integer representing the time it takes to inform all the employees
Print a separate line for each test case.
1 <= T <= 10
1 <= N <= 10^5
0 <= headId, manager[i] < N
0 <= timeToInform[i] <= 10^4
manager[headId] = -1
Time Limit = 1 sec
In this approach, we can create a tree out of the given information and traverse it. For any node, the time taken is the maximum time taken by its subordinates and the timeToInform of that node.
We will create a dfs(employeeId, subordinates, timeToInform), where employeeId is the id of the employee we are currently looking, ‘subordinates’ is the map containing the list of all subordinates of any employee, and ‘timeToInform is the array given in the problem.
Algorithm:
In this approach, we will traverse the graph, but since we are doing it in a breadth-first search, we will have to maintain an array ‘empTime’ which will keep track of overall time taken by an employee, for each employee we will add the overall time taken by their direct manager.
We will just return the maximum value in ‘empTime’.