n as the number of nodes, adj as the adjacency list, indegree as the array to store the number of incoming edges for each node, and count as a 2D-array to store color frequencies.q and push all nodes with indegree of 0 into it.answer to 0 and nodesSeen to 0.q is not empty, process nodes in the queue:
node from q.node's color in count and update answer.nodesSeen.neighbor of node, update count[neighbor] and decrement indegree[neighbor].indegree[neighbor] becomes 0, enqueue neighbor.nodesSeen is less than n, return -1 to indicate a cycle. Otherwise, return answer.