Time Taken to Mark All Nodes - Problem

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] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree.

Initially, all nodes are unmarked. For each node i:

  • If i is odd, the node will get marked at time x if there is at least one node adjacent to it which was marked at time x - 1.
  • If i is even, the node will get marked at time x if there is at least one node adjacent to it which was marked at time x - 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.

Input & Output

Example 1 — Simple Chain
$ Input: edges = [[0,1],[1,2]]
Output: [3,2,3]
💡 Note: Starting from node 0: 0→1 (t=1) then 1→2 (t=3), max=3. Starting from node 1: 1→0 (t=2) and 1→2 (t=2), max=2. Starting from node 2: 2→1 (t=1) then 1→0 (t=3), max=3.
Example 2 — Single Edge
$ Input: edges = [[0,1]]
Output: [1,2]
💡 Note: From node 0: mark node 1 at time 1 (odd). From node 1: mark node 0 at time 2 (even).
Example 3 — Star Graph
$ Input: edges = [[1,0],[1,2],[1,3]]
Output: [2,2,2,2]
💡 Note: Node 1 connects to all others. From any node, the maximum time depends on the parity rules and distances.

Constraints

  • 1 ≤ n ≤ 105
  • edges.length == n - 1
  • 0 ≤ ui, vi ≤ n - 1
  • The given input represents a valid tree.

Visualization

Tap to expand
Time Taken to Mark All Nodes INPUT Undirected Tree (3 nodes) 0 even 1 odd 2 even edges array: [[0,1], [1,2]] Marking Rules: Odd node: marked at t if adj at t-1 Even node: marked at t if adj at t-2 n = 3 nodes, linear tree ALGORITHM STEPS 1 Root at each node Compute time for each start 2 DFS down (base case) Find max time to mark subtree 3 Reroot DP Transfer info parent--child 4 Combine results Max of all paths from root Trace (start at node 0): t=0: mark node 0 t=1: node 1 (odd, adj at 0) t=2: wait... t=3: wait (need adj at t-2) t=4: node 2 (even, adj at 2) Time: O(n) with rerooting Space: O(n) FINAL RESULT Time to mark all nodes from each start: Start: Node 0 0--1--2: path needs 4 steps 4 Start: Node 1 Center: max dist = 2 2 Start: Node 2 2--1--0: path needs 3 steps 3 Output Array: 4 [0] 2 [1] 3 [2] OK - All times computed! Key Insight: Tree Rerooting DP avoids O(n^2) by computing subtree times once, then transferring information when changing roots. The different delays (1 for odd, 2 for even nodes) require tracking both the maximum and second maximum times to handle parent contributions during rerooting. TutorialsPoint - Time Taken to Mark All Nodes | Tree Rerooting Dynamic Programming
Asked in
Meta 8 Google 5
8.4K Views
Medium Frequency
~35 min Avg. Time
256 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen