|
9 | 9 |
|
10 | 10 | [Next >](https://github.com/openset/leetcode/tree/master/problems/missing-element-in-sorted-array "Missing Element in Sorted Array")
|
11 | 11 |
|
12 | | -## 1059. All Paths from Source Lead to Destination (Medium) |
| 12 | +## 1059. All Paths from Source Lead to Destination (Hard) |
13 | 13 |
|
| 14 | +<p>Given the <code>edges</code> of a directed graph, and two nodes <code>source</code> and <code>destination</code> of this graph, determine whether or not all paths starting from <code>source</code> eventually end at <code>destination</code>, that is:</p> |
14 | 15 |
|
| 16 | +<ul> |
| 17 | + <li>At least one path exists from the <code>source</code> node to the <code>destination</code> node</li> |
| 18 | + <li>If a path exists from the <code>source</code> node to a node with no outgoing edges, then that node is equal to <code>destination</code>.</li> |
| 19 | + <li>The number of possible paths from <code>source</code> to <code>destination</code> is a finite number.</li> |
| 20 | +</ul> |
| 21 | + |
| 22 | +<p>Return <code>true</code> if and only if all roads from <code>source</code> lead to <code>destination</code>.</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | + |
| 26 | +<p><strong>Example 1:</strong></p> |
| 27 | + |
| 28 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/16/485_example_1.png" style="width: 200px; height: 208px;" /></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input: </strong>n = 3, edges = <span id="example-input-1-2">[[0,1],[0,2]]</span>, source = <span id="example-input-1-3">0</span>, destination = 2 |
| 32 | +<strong>Output: </strong><span id="example-output-1">false</span> |
| 33 | +<strong>Explanation: </strong>It is possible to reach and get stuck on both node 1 and node 2. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>Example 2:</strong></p> |
| 37 | + |
| 38 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/16/485_example_2.png" style="width: 200px; height: 230px;" /></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>Input: </strong>n = <span id="example-input-2-1">4</span>, edges = <span id="example-input-2-2">[[0,1],[0,3],[1,2],[2,1]]</span>, source = <span id="example-input-2-3">0</span>, destination = <span id="example-input-2-4">3</span> |
| 42 | +<strong>Output: </strong><span id="example-output-2">false</span> |
| 43 | +<strong>Explanation: </strong>We have two possibilities: to end at node 3, or to loop over node 1 and node 2 indefinitely. |
| 44 | +</pre> |
| 45 | + |
| 46 | +<p><strong>Example 3:</strong></p> |
| 47 | + |
| 48 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/16/485_example_3.png" style="width: 200px; height: 183px;" /></p> |
| 49 | + |
| 50 | +<pre> |
| 51 | +<strong>Input: </strong>n = <span id="example-input-3-1">4</span>, edges = <span id="example-input-3-2">[[0,1],[0,2],[1,3],[2,3]]</span>, source = <span id="example-input-3-3">0</span>, destination = <span id="example-input-3-4">3</span> |
| 52 | +<strong>Output: </strong><span id="example-output-3">true</span> |
| 53 | +</pre> |
| 54 | + |
| 55 | +<p><strong>Example 4:</strong></p> |
| 56 | + |
| 57 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/16/485_example_4.png" style="width: 200px; height: 183px;" /></p> |
| 58 | + |
| 59 | +<pre> |
| 60 | +<strong>Input: </strong>n = <span id="example-input-4-1">3</span>, edges = <span id="example-input-4-2">[[0,1],[1,1],[1,2]]</span>, source = <span id="example-input-4-3">0</span>, destination = <span id="example-input-4-4">2</span> |
| 61 | +<strong>Output: </strong><span id="example-output-4">false</span> |
| 62 | +<strong>Explanation: </strong>All paths from the source node end at the destination node, but there are an infinite number of paths, such as 0-1-2, 0-1-1-2, 0-1-1-1-2, 0-1-1-1-1-2, and so on. |
| 63 | +</pre> |
| 64 | + |
| 65 | +<p><strong>Example 5:</strong></p> |
| 66 | + |
| 67 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/16/485_example_5.png" style="width: 150px; height: 131px;" /></p> |
| 68 | + |
| 69 | +<pre> |
| 70 | +<strong>Input: </strong>n = <span id="example-input-5-1">2</span>, edges = <span id="example-input-5-2">[[0,1],[1,1]]</span>, source = <span id="example-input-5-3">0</span>, destination = <span id="example-input-5-4">1</span> |
| 71 | +<strong>Output: </strong><span id="example-output-5">false</span> |
| 72 | +<strong>Explanation: </strong>There is infinite self-loop at destination node. |
| 73 | +</pre> |
| 74 | + |
| 75 | +<p> </p> |
| 76 | + |
| 77 | +<p><strong>Note:</strong></p> |
| 78 | + |
| 79 | +<ol> |
| 80 | + <li><italic>The given graph may have self loops and parallel edges.</italic></li> |
| 81 | + <li>The number of nodes <code>n</code> in the graph is between <code>1</code> and <code>10000</code></li> |
| 82 | + <li>The number of edges in the graph is between <code>0</code> and <code>10000</code></li> |
| 83 | + <li><code>0 <= edges.length <= 10000</code></li> |
| 84 | + <li><code>edges[i].length == 2</code></li> |
| 85 | + <li><code>0 <= source <= n - 1</code></li> |
| 86 | + <li><code>0 <= destination <= n - 1</code></li> |
| 87 | +</ol> |
15 | 88 |
|
16 | 89 | ### Related Topics
|
17 | 90 | [[Depth-first Search](https://github.com/openset/leetcode/tree/master/tag/depth-first-search/README.md)]
|
|
0 commit comments