Imagine you're a network architect tasked with optimizing the cost efficiency of a tree-structured network with n nodes (indexed 0 to n-1). You have an unrooted tree defined by edges array, where each edge connects two nodes, and each node has an associated operational price.
The price sum of any path is simply the sum of prices of all nodes along that path. Here's the challenge: you can choose any node as the root of your network. Once you pick a root, you need to consider all possible paths that start from this root node.
The incurred cost for choosing a particular root is the difference between the maximum and minimum price sum among all paths starting from that root. Your goal is to find the maximum possible cost you can achieve by choosing the optimal root.
This problem combines tree traversal, dynamic programming, and optimal substructure - making it a fascinating challenge that tests your ability to think about tree rerooting and path optimization!
Input & Output
Constraints
- 1 โค n โค 2 ร 104
- edges.length == n - 1
- 0 โค ai, bi โค n - 1
- edges represents a valid tree
- 1 โค price[i] โค 105
- The graph is guaranteed to be a connected tree