1059. All Paths from Source Lead to Destination
Last updated
Last updated
https://leetcode.com/problems/all-paths-from-source-lead-to-destination
Given the edges
of a directed graph where edges[i] = [ai, bi]
indicates there is an edge between nodes ai
and bi
, and two nodes source
and destination
of this graph, determine whether or not all paths starting from source
eventually, end at destination
, that is:
At least one path exists from the source
node to the destination
node
If a path exists from the source
node to a node with no outgoing edges, then that node is equal to destination
.
The number of possible paths from source
to destination
is a finite number.
Return true
if and only if all roads from source
lead to destination
.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
1 <= n <= 104
0 <= edges.length <= 104
edges.length == 2
0 <= ai, bi <= n - 1
0 <= source <= n - 1
0 <= destination <= n - 1
The given graph may have self-loops and parallel edges.