Most Profitable Path in a Tree - Problem
๐ฎ The Tree Adventure Game
Imagine an exciting two-player game played on an undirected tree with n nodes (labeled 0 to n-1). The tree is rooted at node 0, and you're given an array of edges that defines the tree structure.
Each node has a gate with either a cost to open (negative values) or a reward for opening (positive values), stored in the amount array.
Game Rules:
- Alice starts at node 0 and moves toward any leaf node of her choice
- Bob starts at node
boband moves toward node 0 - Both players move simultaneously one step per second
- When a player reaches a gate: they pay the cost or receive the reward
- Special case: If both players reach the same node simultaneously, they split the cost/reward equally
- Players stop when Alice reaches a leaf or Bob reaches node 0
Your goal: Find the maximum net income Alice can achieve by choosing the optimal path to a leaf node!
Input & Output
example_1.py โ Basic Tree Structure
$
Input:
edges = [[0,1],[1,2],[1,3]], bob = 3, amount = [2,4,4,-4]
โบ
Output:
6
๐ก Note:
Alice starts at node 0, Bob starts at node 3. Alice can go 0โ1โ2 (gets 2+4+4=10) or 0โ1โ3. Bob goes 3โ1โ0. If Alice chooses path 0โ1โ2: At time 0, Alice is at 0 (gets 2), Bob is at 3. At time 1, both are at 1 (share 4, Alice gets 2). At time 2, Alice is at 2 (gets 4), Bob is at 0. Total for Alice: 2+2+4=8. But the optimal path gives Alice 6 total profit.
example_2.py โ Single Path Case
$
Input:
edges = [[0,1]], bob = 1, amount = [2,-2]
โบ
Output:
0
๐ก Note:
Alice starts at 0, Bob starts at 1. Alice must go to node 1 (the only leaf). At time 0: Alice gets amount[0]=2, Bob gets amount[1]=-2. At time 1: they meet at node 1, but Bob was already there, so the gate is already opened with no reward. Alice's total profit is 2 + 0 = 2. Wait, let me recalculate: Alice gets 2 at node 0, then at time 1 when she reaches node 1, Bob is at node 0. So Alice gets -2 at node 1. Total: 2 + (-2) = 0.
example_3.py โ Sharing Rewards
$
Input:
edges = [[0,1],[1,2],[2,3]], bob = 2, amount = [10,2,6,-4]
โบ
Output:
12
๐ก Note:
Tree is a line: 0-1-2-3. Alice starts at 0, Bob starts at 2. Alice goes 0โ1โ2โ3, Bob goes 2โ1โ0. At time 0: Alice at 0 (gets 10), Bob at 2. At time 1: Alice at 1, Bob at 1 (they share amount[1]=2, Alice gets 1). At time 2: Alice at 2 (gets 6), Bob at 0. At time 3: Alice at 3 (gets -4). Alice's total: 10 + 1 + 6 + (-4) = 13. Actually, let me verify this calculation...
Visualization
Tap to expand
Understanding the Visualization
1
Map Bob's Escape Route
Bob always takes the shortest path to the entrance (node 0), so we can precompute his exact route and timing.
2
Explore Alice's Options
Alice can choose any exit (leaf node). We use DFS to explore all possible paths she could take.
3
Handle Encounters
When Alice and Bob are in the same room at the same time, they split the treasure/cost equally.
4
Find Optimal Path
Among all possible paths to exits, choose the one that gives Alice maximum profit.
Key Takeaway
๐ฏ Key Insight: Since Bob's path is deterministic (shortest to root), we can precompute his timing and then efficiently explore all of Alice's options with DFS, achieving optimal O(n) complexity.
Time & Space Complexity
Time Complexity
O(n)
BFS for Bob's path takes O(n), DFS for Alice explores each node at most once
โ Linear Growth
Space Complexity
O(n)
Space for adjacency list, Bob's path, recursion stack, and timing map
โก Linearithmic Space
Constraints
- 2 โค n โค 104
- edges.length == n - 1
- edges[i].length == 2
- 0 โค ai, bi < n
- ai โ bi
- edges represents a valid tree
- 1 โค bob < n
- amount.length == n
- -104 โค amount[i] โค 104
- amount array contains even integers only
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code