1557. Minimum Number of Vertices to Reach All Nodes
https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/
Python
Outdegree Difference
- 能夠透過其他節點到達的節點(edge outdegree)都可以視為reachable
- 所有節點與edge outdegree的差集,即是至少要使用的出發點
class Solution:
def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]:
nodes = set(range(n))
outdegree = set([edge[1] for edge in edges])
return list(nodes - outdegree)