Good Boxes Bad Boxes

0

0 votes
Problem

Utkarsh, president of CSA, likes to label boxes. Given n boxes where each box has a link to another box, Utkarsh calls first box a good box and calls every box as a good box which has a link to any one of the good box ( not when a good box links to some box). Now, he is given n boxes and their links. He has to tell the minimum number of steps where he can shift the links of bad boxes to make all the boxes good.

INPUT N, number of Boxes followed by the N links which each box points to. 0<N<10000

OUTPUT Number of links to be shifted so as to make all the boxes as the good boxes.

Sample Test case #1

Input

5 2 3 4 5 5

Output

1

Explanation: We have 5 boxes with box 1 as a good box. Box 1 points to box 2. Box 2 points to Box 3. Box 3 points to Box 4. Box 4 points to Box 5. Box 5 points to itself. So when we shift the 5th link to point to Box 1 all becomes good boxes.

Sample Test case #2

Input

4 2 1 3 4

Output

2

Explanation:

Box 1 points to Box 2 and Box 2 points to Box 1. So it makes Box 2 good. Box 3 and Box 4 are self-looped. So you need them to point to good boxes.

Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?