01 · Question
Given a directed adjacency list and a valid source vertex, return the number of paths from the source to every vertex. Count the empty path from the source to itself once. Unreachable vertices have count zero. Assume there are no duplicate edges and counts fit signed 64-bit integers. Reject a cycle anywhere in the graph (None in Python, a false status in Go).
Example: graph=[[1,2],[3],[3],[]], source=0 returns [1,1,1,2].
02 · Analysis
Initialize ways[source]=1. Kahn's queue guarantees all predecessors are processed before a vertex; when removing u, add ways[u] along every outgoing edge, then decrement indegrees. Each path to a neighbor has one final incoming edge, so the additions count disjoint possibilities. Compare processed count with V to reject cycles, including disconnected ones. O(V+E) time and O(V) auxiliary space. This counts paths, not possible topological orderings.