Imagine you're navigating through a network of interconnected cities, where each road has a specific travel cost. Your task is to find the weighted median node along any path between two cities.
You are given an integer n representing the number of nodes (0 to n-1) in an undirected, weighted tree rooted at node 0. The tree structure is defined by a 2D array edges where edges[i] = [u_i, v_i, w_i] represents a bidirectional edge between nodes u_i and v_i with weight w_i.
The weighted median node is the first node x on the path from node u to node v where the cumulative sum of edge weights from u to x is greater than or equal to half of the total path weight.
For each query [u_j, v_j], determine the weighted median node along the path from u_j to v_j. Return an array where each element corresponds to the weighted median node for the respective query.
Input & Output
Constraints
- 2 โค n โค 104
- edges.length == n - 1
- 0 โค ui, vi < n
- 1 โค wi โค 106
- 1 โค queries.length โค 104
- The graph forms a valid tree (connected and acyclic)