Time Taken to Mark All Nodes Problem
Description
LeetCode Problem 3241.
There exists an undirected tree with n nodes numbered 0 to n - 1. You are given a 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i] indicates that there is an edge between nodes u_i and v_i in the tree.
Initially, all nodes are unmarked. For each node i:
- If
iis odd, the node will get marked at timexif there is at least one node adjacent to it which was marked at timex - 1. - If
iis even, the node will get marked at timexif there is at least one node adjacent to it which was marked at timex - 2.
Return an array times where times[i] is the time when all nodes get marked in the tree, if you mark node i at time t = 0.
Note that the answer for each times[i] is independent, i.e. when you mark node i all other nodes are unmarked.
Example 1:
Input: edges = [[0,1],[0,2]]
Output: [2,4,3]
Explanation:

- For i = 0:
- Node 1 is marked at t = 1, and Node 2 at t = 2.
- For i = 1:
- Node 0 is marked at t = 2, and Node 2 at t = 4.
- For i = 2:
- Node 0 is marked at t = 2, and Node 1 at t = 3.
Example 2:
Input: edges = [[0,1]]
Output: [1,2]
Explanation:

- For i = 0:
- Node 1 is marked at t = 1.
- For i = 1:
- Node 0 is marked at t = 2.
Example 3:
Input: edges = [[2,4],[0,1],[2,3],[0,2]]
Output: [4,6,3,5,5]
Explanation:

Constraints:
2 <= n <= 10^5edges.length == n - 1edges[i].length == 20 <= edges[i][0], edges[i][1] <= n - 1- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: Rerooting DP
Marking spreads along the tree, and node v is marked 1 unit after a neighbour if v is odd and 2 units after if v is even. So the time for start node s is the longest path from s, where each step into node v costs w(v) (1 for odd, 2 for even).
Compute this for all starts with two passes. First, in a bottom-up pass over a BFS order from node 0, compute for each node the longest downward path, keeping the best and second-best values over its children together with the child giving the best one. Second, in a top-down pass, compute up[c] for each child c of u, the longest path from c that first goes to u: it is w(u) + max(up[u], best downward path of u not through c), where the second term is the best value, or the second-best if c is the best child. The answer for a node is the larger of its downward path and up.
Complexity: O(n) time and space.
class Solution:
def timeTaken(self, edges: List[List[int]]) -> List[int]:
n = len(edges) + 1
g = [[] for _ in range(n)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
parent = [-1] * n
order = [0]
for u in order:
for v in g[u]:
if v != parent[u]:
parent[v] = u
order.append(v)
w = [2 - (i & 1) for i in range(n)]
d1 = [0] * n
d2 = [0] * n
best = [-1] * n
for u in reversed(order):
p = parent[u]
if p >= 0:
val = d1[u] + w[u]
if val > d1[p]:
d2[p] = d1[p]
d1[p] = val
best[p] = u
elif val > d2[p]:
d2[p] = val
up = [0] * n
for u in order:
for v in g[u]:
if v != parent[u]:
via = d2[u] if best[u] == v else d1[u]
up[v] = w[u] + max(up[u], via)
return [max(d1[i], up[i]) for i in range(n)]