This problem can be solved with BIT or Merge Sort Tree after constructing the required array using DFS (topological sort). The main observation is that for a node it's range for the given query is . and you can get the exact node's order by doing a topological sort in the tree.
Then you can do basic merge sort tree to find the answer where the time complexity is or you can do it with BIT where the time complexity is .