Nodes on Cycles — Solution & Editorial
A functional graph is 'rho-shaped' per component. Walk from each unvisited node marking a gray path; when you re-hit a gray node, the tail from that node is a cycle. Iterative to avoid recursion limits.
Complexity: O(N)
Watch out: Self-loops (ai = i) are cycles of length 1.