classSolution{publicintnumOfMinutes(int n,int headID,int[] manager,int[] informTime){// Manager -> employeesHashMap<Integer,ArrayList<Integer>> map =newHashMap();// Build the relationshipsfor(int i =0; i < n;++i){// Get the list or create a new oneArrayList<Integer> list = map.getOrDefault(manager[i],newArrayList<>());
list.add(i);// Add the element to the list
map.put(manager[i], list);// Put the updated list back in the map}int res =0;// Queue of "id, time"Queue<Pair<Integer,Integer>> q =newLinkedList();// Add the CEO
q.offer(newPair(headID, informTime[headID]));while(!q.isEmpty()){int size = q.size();int id = q.peek().getKey();int time = q.poll().getValue();// Update the result
res =Math.max(res, time);if(map.containsKey(id)){// Add the employees of the current managerfor(int emp : map.get(id)){
q.offer(newPair(emp, time + informTime[emp]));}}}return res;}}