|
| 1 | +# [1557. 可以到达所有点的最少点数目](https://leetcode-cn.com/problems/minimum-number-of-vertices-to-reach-all-nodes) |
| 2 | + |
| 3 | +[English Version](/solution/1500-1599/1557.Minimum Number of Vertices to Reach All Nodes/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | +<p>给你一个 <strong>有向无环图</strong> , <code>n</code> 个节点编号为 <code>0</code> 到 <code>n-1</code> ,以及一个边数组 <code>edges</code> ,其中 <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> 表示一条从点 <code>from<sub>i</sub></code> 到点 <code>to<sub>i</sub></code> 的有向边。</p> |
| 9 | + |
| 10 | +<p>找到最小的点集使得从这些点出发能到达图中所有点。题目保证解存在且唯一。</p> |
| 11 | + |
| 12 | +<p>你可以以任意顺序返回这些节点编号。</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | + |
| 16 | +<p><strong>示例 1:</strong></p> |
| 17 | + |
| 18 | +<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5480e1.png" style="height: 181px; width: 231px;"></p> |
| 19 | + |
| 20 | +<pre><strong>输入:</strong>n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]] |
| 21 | +<strong>输出:</strong>[0,3] |
| 22 | +<strong>解释:</strong>从单个节点出发无法到达所有节点。从 0 出发我们可以到达 [0,1,2,5] 。从 3 出发我们可以到达 [3,4,2,5] 。所以我们输出 [0,3] 。</pre> |
| 23 | + |
| 24 | +<p><strong>示例 2:</strong></p> |
| 25 | + |
| 26 | +<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5480e2.png" style="height: 201px; width: 201px;"></p> |
| 27 | + |
| 28 | +<pre><strong>输入:</strong>n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]] |
| 29 | +<strong>输出:</strong>[0,2,3] |
| 30 | +<strong>解释:</strong>注意到节点 0,3 和 2 无法从其他节点到达,所以我们必须将它们包含在结果点集中,这些点都能到达节点 1 和 4 。 |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | + |
| 35 | +<p><strong>提示:</strong></p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + <li><code>2 <= n <= 10^5</code></li> |
| 39 | + <li><code>1 <= edges.length <= min(10^5, n * (n - 1) / 2)</code></li> |
| 40 | + <li><code>edges[i].length == 2</code></li> |
| 41 | + <li><code>0 <= from<sub>i,</sub> to<sub>i</sub> < n</code></li> |
| 42 | + <li>所有点对 <code>(from<sub>i</sub>, to<sub>i</sub>)</code> 互不相同。</li> |
| 43 | +</ul> |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +## 解法 |
| 48 | + |
| 49 | +<!-- 这里可写通用的实现逻辑 --> |
| 50 | + |
| 51 | + |
| 52 | +<!-- tabs:start --> |
| 53 | + |
| 54 | +### **Python3** |
| 55 | + |
| 56 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 57 | + |
| 58 | +```python |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +### **Java** |
| 63 | + |
| 64 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 65 | + |
| 66 | +```java |
| 67 | + |
| 68 | +``` |
| 69 | + |
| 70 | +### **...** |
| 71 | +``` |
| 72 | +
|
| 73 | +``` |
| 74 | + |
| 75 | +<!-- tabs:end --> |
0 commit comments