原文链接: https://leetcode-cn.com/problems/minimum-number-of-vertices-to-reach-all-nodes
英文原文
Given a directed acyclic graph, with n
vertices numbered from 0
to n-1
, and an array edges
where edges[i] = [fromi, toi]
represents a directed edge from node fromi
to node toi
.
Find the smallest set of vertices from which all nodes in the graph are reachable. It's guaranteed that a unique solution exists.
Notice that you can return the vertices in any order.
Example 1:
Input: n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]] Output: [0,3] Explanation: It'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].
Example 2:
Input: n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]] Output: [0,2,3] Explanation: 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.
Constraints:
2 <= n <= 10^5
1 <= edges.length <= min(10^5, n * (n - 1) / 2)
edges[i].length == 2
0 <= fromi, toi < n
- All pairs
(fromi, toi)
are distinct.
中文题目
给你一个 有向无环图 , n
个节点编号为 0
到 n-1
,以及一个边数组 edges
,其中 edges[i] = [fromi, toi]
表示一条从点 fromi
到点 toi
的有向边。
找到最小的点集使得从这些点出发能到达图中所有点。题目保证解存在且唯一。
你可以以任意顺序返回这些节点编号。
示例 1:
输入:n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]] 输出:[0,3] 解释:从单个节点出发无法到达所有节点。从 0 出发我们可以到达 [0,1,2,5] 。从 3 出发我们可以到达 [3,4,2,5] 。所以我们输出 [0,3] 。
示例 2:
输入:n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]] 输出:[0,2,3] 解释:注意到节点 0,3 和 2 无法从其他节点到达,所以我们必须将它们包含在结果点集中,这些点都能到达节点 1 和 4 。
提示:
2 <= n <= 10^5
1 <= edges.length <= min(10^5, n * (n - 1) / 2)
edges[i].length == 2
0 <= fromi, toi < n
- 所有点对
(fromi, toi)
互不相同。
通过代码
高赞题解
如果图中有一条a->b的边,那么b一定不会在最小的点集中,因为b能到达的点a也一定能到达,且a还能比b多到达一个点(a自己),选b不如选a。因此,只有入度为0的点才可能在最小点集中。
最小点集中必须包括所有入度为0的点,假如某个入度为0的点a不在最小点集中,那么最小点集中的其他点一定无法访问到a点,不符合最小点集能到达图中所有点的要求。
由于题目保证解存在且唯一,因此最小点集为图中所有入度为0的点
public List<Integer> findSmallestSetOfVertices(int n, List<List<Integer>> edges) {
int[] inDegrees = new int[n];
for (List<Integer> edge : edges) {
inDegrees[edge.get(1)]++;
}
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < inDegrees.length; i++) {
if (inDegrees[i] == 0) {
ans.add(i);
}
}
return ans;
}
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
8659 | 11199 | 77.3% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|