You can think of every connected component of a functional graph as a rooted tree with all edges directed toward the root plus an additional edge going out of the root.
Ideas:
Solution : Binary Jumping
Solution : Graph Coloring https://atcoder.jp/contests/abc357/tasks/abc357_e