This problem shows the power of combining topological sort with DP. Finding the longest path is NP-hard in general graphs, but easy in DAGs because topological order guarantees we process predecessors before successors.
The state dist[v] stores the longest path from source to . The transition takes the max over all predecessors: dist[v] = max(dist[u] + 1) for all edges . Path reconstruction uses parent pointers set during the DP.
core insight: any optimization problem on a DAG (longest path, shortest path, counting paths) can be solved in using topological sort plus DP. The graph structure eliminates the need for more complex algorithms like Dijkstra or Bellman-Ford.