Reachable cities
You are given a directed graph with n cities (labeled from 0 to n - 1) and a starting city start. The graph is represented by a 2D array edges where edges[i] = [u, v] indicates a one-way road from city u to city v. Find every city that is reachable from the starting city by following zero or more roads.
Return the IDs of the reachable cities in increasing order.
Example 1:
Input:
n = 4 start = 0 edges = [[0, 1], [1, 2], [0, 2], [2, 3]]
Output:
[0, 1, 2, 3]
Explanation:
- From city 0, you can reach 1 and 2 directly.
- From city 1, you can reach 2.
- From city 2, you can reach 3.
- Including the starting city 0, the reachable cities are 0, 1, 2, and 3.
Example 2:
Input:
n = 3 start = 1 edges = [[0, 1], [2, 0]]
Output:
[1]
Explanation:
- The starting city is 1. There are no outgoing roads from 1.
- Therefore, only the starting city 1 is reachable.
Now Your Turn!
Pick the correct output for the given inputInput:
n = 5 start = 0 edges = [[0, 1], [1, 2], [3, 4]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 105
- 0 <= start < n
- 0 <= edges.length <= 2 * 105
- edges[i].length == 2
- 0 <= u, v < n