Problem statement

https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/

Solution

Just find all nodes with indegree zero.

Complexity

It is O(m) for time and O(n) for space.

Code

class Solution:
    def findSmallestSetOfVertices(self, n, E):
        G = Counter()
        for x, y in E:
            G[y] += 1
            G[x] += 0
        return [x for x in G if G[x] == 0]