Graph Theory37 sections · 1633 units
Open in Course

Codeforces 580C Kefa and Park - State

What to carry?

You need to track: where you are (vertex uu), where you came from (parent pp to avoid backtracking), and the current consecutive cat count. If vertex uu has a cat, increment the count. Otherwise, reset it to zero. Before recursing to a child, check if the count exceeds mm.

If yes, do not recurse (that path is blocked). If you reach a leaf (degree 11 vertex other than root) with valid count, increment the answer. The state flows down the recursion tree, updating at each step.