Graph Theory37 sections · 1633 units
Open in Course

Coin Collector - Approach

(SCC + DAG DP)

First, find all SCCs using Kosaraju or Tarjan. Compress the graph: each SCC becomes one node with total coins equal to the sum of all coins in that SCC. Build the condensation DAG with edges between components. Run DP on this DAG: dp[v] represents the maximum coins you can collect starting from component vv.

Base case: If component vv has no outgoing edges, dp[v] = coins[v]. Transition: dp[v] = coins[v] + max(dp[u]) for all edges vuv \to u in the condensation graph. Process nodes in reverse topological order so each successor's DP value is ready before you need it.