Pattern recognition:
- Exactly one outgoing edge per node = functional graph
- Structure: trees leading into cycles
- Need distance queries
Approach: Find cycles and identify which cycle each node eventually reaches
Compute distance from each node to its cycle
For query :
- If is an ancestor of (in the tree part), answer is depth difference
- If both reach the same cycle, compute cycle distance
- Otherwise, impossible
This requires careful decomposition of the functional graph.