Valid Restaurant Paths
You are given an undirected tree with n vertices, rooted at vertex 1. The leaf vertices represent your valid destinations.
You are given an integer m, a binary array a where a[i - 1] == 1 means vertex i has an obstacle, and a 2D array edges representing the tree's connections.
Starting from the root, find the total number of distinct leaf vertices you can safely reach. You must abort your path if you ever traverse more than m consecutive vertices that contain an obstacle.
Example 1:
Input: m = 1, a = [1, 1, 0, 0], edges = [[1, 2], [1, 3], [1, 4]]
Output: 2
Explanation:
The path to leaf 2 has two consecutive cats (at nodes 1 and 2), which exceeds m=1. Paths to leaves 3 and 4 only have one cat (at node 1), making them safe.
Example 2:
Input: m = 2, a = [1, 1, 1], edges = [[1, 2], [2, 3]]
Output: 0
Explanation:
The only leaf is 3. The path 1 -> 2 -> 3 has 3 consecutive cats. Since 3 > 2, Kefa cannot safely reach any restaurant.
Now Your Turn!
Pick the correct output for the given inputInput: m = 1, a = [1, 0, 1, 1, 0, 0, 0], edges = [[1, 2], [1, 3], [2, 4], [2, 5], [3, 6], [3, 7]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 <= n <= 105
- 1 <= m <= n
- a[i] is either 0 or 1.
- The given n-1 edges are guaranteed to form a valid tree.