Graph Theory37 sections · 1633 units
Open in Course

State Definition

dp[u] = max cities to u

Define dp[u] as the maximum number of cities on any path from city 11 to city uu. Initialize dp[1] = 1 (you start at city 11, so you have visited 11 city) and dp[u] = -Infinity for all other cities. For each city uu in topological order, for each outgoing flight (u,v)(u, v), update: dp[v] = max(dp[v], dp[u] + 1) After processing all cities, check dp[n].

If it is still -\infty, output "IMPOSSIBLE". Otherwise, dp[n] is the answer.