Problem statement

https://binarysearch.com/problems/Minimum-Starting-Nodes-to-Visit-Graph/

Solution

Equal to Leetcode 1557. Minimum Number of Vertices to Reach All Nodes.

Complexity

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

Code

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