richer
array.answer
with the same length as quiet
and fill it with -1, indicating that we have not computed the answer for that person yet.i
, if answer[i]
is -1, call the dfs
function on that person.dfs
function that takes a person x
and returns the quietest person in the subtree rooted at x
.answer[x]
is not -1, return answer[x]
as it has already been computed.answer[x]
to x
initially, and for each child y
of x
in the graph, update answer[x]
to the quieter person between answer[x]
and dfs(y)
.answer[x]
.answer
array.