java.lang.Object
g1501_1600.s1557_minimum_number_of_vertices_to_reach_all_nodes.Solution

public class Solution extends Object
1557 - Minimum Number of Vertices to Reach All Nodes.<p>Medium</p> <p>Given a** directed acyclic graph** , with <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, and an array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represents a directed edge from node <code>from<sub>i</sub></code> to node <code>to<sub>i</sub></code>.</p> <p>Find <em>the smallest set of vertices from which all nodes in the graph are reachable</em>. It&rsquo;s guaranteed that a unique solution exists.</p> <p>Notice that you can return the vertices in any order.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/07/07/untitled22.png" alt="" /></p> <p><strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]</p> <p><strong>Output:</strong> [0,3]</p> <p><strong>Explanation:</strong> It&rsquo;s not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/07/07/untitled.png" alt="" /></p> <p><strong>Input:</strong> n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]</p> <p><strong>Output:</strong> [0,2,3]</p> <p><strong>Explanation:</strong> Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 10^5</code></li> <li><code>1 <= edges.length <= min(10^5, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 <= from<sub>i,</sub> to<sub>i</sub> < n</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details