2467. Most Profitable Path in a Tree #1354
-
Topics: There is an undirected tree with At every node
The game goes on as follows:
Return the maximum net income Alice can have if she travels towards the optimal leaf node. Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the maximum net income Alice can achieve by traveling from the root node (node 0) to a leaf node in an undirected tree, considering Bob's movement from a given node towards the root. The solution involves calculating the optimal path for Alice while considering the interactions with Bob's path and their respective gate opening times. Approach
Let's implement this solution in PHP: 2467. Most Profitable Path in a Tree <?php
/**
* @param Integer[][] $edges
* @param Integer $bob
* @param Integer[] $amount
* @return Integer
*/
function mostProfitablePath($edges, $bob, $amount) {
// Build adjacency list
$adj = [];
foreach ($edges as $e) {
$u = $e[0];
$v = $e[1];
if (!isset($adj[$u])) $adj[$u] = [];
if (!isset($adj[$v])) $adj[$v] = [];
$adj[$u][] = $v;
$adj[$v][] = $u;
}
$n = count($adj);
$parent = array_fill(0, $n, -1);
$visited = array_fill(0, $n, false);
$queue = new SplQueue();
$queue->enqueue(0);
$visited[0] = true;
// BFS to find parent pointers
while (!$queue->isEmpty()) {
$u = $queue->dequeue();
foreach ($adj[$u] as $v) {
if (!$visited[$v] && $v != $parent[$u]) {
$parent[$v] = $u;
$visited[$v] = true;
$queue->enqueue($v);
}
}
}
// Get Bob's path and time
$bob_path = [];
$current = $bob;
while ($current != -1) {
$bob_path[] = $current;
$current = $parent[$current];
}
$bob_time = [];
$time = 0;
foreach ($bob_path as $node) {
$bob_time[$node] = $time++;
}
// Iterative DFS to find max sum
$max_sum = PHP_INT_MIN;
$stack = [[0, -1, 0, 0]]; // [node, parent, sum, depth]
while (!empty($stack)) {
$element = array_pop($stack);
list($u, $p, $sum, $depth) = $element;
// Calculate contribution for current node
if (isset($bob_time[$u])) {
$alice_time = $depth;
$b_time = $bob_time[$u];
if ($alice_time < $b_time) {
$add_val = $amount[$u];
} elseif ($alice_time == $b_time) {
$add_val = $amount[$u] / 2;
} else {
$add_val = 0;
}
} else {
$add_val = $amount[$u];
}
$new_sum = $sum + $add_val;
// Check if it's a leaf node
$is_leaf = ($u != 0) && (count($adj[$u]) == 1);
if ($is_leaf) {
if ($new_sum > $max_sum) {
$max_sum = $new_sum;
}
} else {
// Push children to stack
foreach ($adj[$u] as $v) {
if ($v != $p) {
array_push($stack, [$v, $u, $new_sum, $depth + 1]);
}
}
}
}
return $max_sum;
}
// Example Test Cases
$edges1 = [[0,1],[1,2],[1,3],[3,4]];
$bob1 = 3;
$amount1 = [-2,4,2,-4,6];
echo mostProfitablePath($edges1, $bob1, $amount1) . "\n"; // Output: 6
$edges2 = [[0,1]];
$bob2 = 1;
$amount2 = [-7280,2350];
echo mostProfitablePath($edges2, $bob2, $amount2) . "\n"; // Output: -7280
?> Explanation:
This approach efficiently handles the tree traversal and timing comparisons to determine the optimal path for Alice, ensuring the solution is both correct and performant for large trees. |
Beta Was this translation helpful? Give feedback.
We need to determine the maximum net income Alice can achieve by traveling from the root node (node 0) to a leaf node in an undirected tree, considering Bob's movement from a given node towards the root. The solution involves calculating the optimal path for Alice while considering the interactions with Bob's path and their respective gate opening times.
Approach