bit of dfs, and dynamic programming