Nodes on Cycles
Each node has exactly one outgoing edge (node i points to ai). Count the nodes that lie on some cycle.
Input: the first line has N; the second line has the N successor values.
Output: the number of nodes on cycles.
Constraints:
- 1 ≤ N ≤ 105
- 1 ≤ ai ≤ N
Sample Tests
Input 1
4 2 3 1 3
Output 1
3
Input 2
2 1 2
Output 2
2
Discussion (opens after you solve — no spoilers before)
No comments yet.