Problems > Nodes on Cycles

Nodes on Cycles

Difficulty: 1700 5000 ms 128 MB

graphs

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

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.