#3241

Time Taken to Mark All Nodes

international master · 2040 · lc hard +32 · 27.6% accepted · 139 likes · top 6%

Description

An undirected tree has n nodes (0 to n - 1) described by edges. All nodes start unmarked. Node i becomes marked at time x when:

- i is odd: some neighbor was marked at time x - 1.

- i is even: some neighbor was marked at time x - 2.

For each node i, if you mark it at t = 0, return the time when all nodes become marked. Return array times (each query is independent).

Code

1
2
3