Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 C

Problem: A functional graph has n = 9 nodes where node i points to f(i) given by the list [1, 2, 0, 4, 5, 3, 3, 6, 7] (node i has exactly one outgoing edge to f(i)). How many nodes lie on cycles?

  1. A. 9
  2. B. 2
  3. C. 6
  4. D. 3

Correct answer: C. 6

Following f from every node finds the cycles 0 → 1 → 2 → 0 and 3 → 4 → 5 → 3; nodes 8 → 7 → 6 form a tail that drains into node 3, so 6 nodes lie on cycles. Assuming every node of a functional graph is on a cycle gives 9, counting cycles gives 2, and counting tail nodes gives 3.

More Coding Interview Problems questions

Exams that ask this topic