1557. Minimum Number of Vertices to Reach All Nodes
Question
Given a directed acyclic graph, with
nvertices numbered from0ton-1, and an arrayedgeswhereedges[i] = [from i, to]represents a directed edge from nodefrom ito nodeto i.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.
Answer
由于是有向无环图(Directed acyclic graph),因此直接计算图内的入度为0的节点即可。
每条路径都需要从入度为0的点开始。
Code
1 | class Solution { |
