Please do in ruby only. Correct and detailed answer will upvoted else downvoted. To learn as quickly as time permits the most recent news about their number one in a general sense new working framework, BolgenOS people group from Nizhni Tagil chose to foster a plan. As per this plan a local area part, who is quick to become familiar with the news, calls another part, the last mentioned, in his turn, calls some third part, etc; for example an individual with list
Please do in ruby only. Correct and detailed answer will upvoted else downvoted.
To learn as quickly as time permits the most recent news about their number one in a general sense new working framework, BolgenOS people group from Nizhni Tagil chose to foster a plan. As per this plan a local area part, who is quick to become familiar with the news, calls another part, the last mentioned, in his turn, calls some third part, etc; for example an individual with list I got an individual with list fi, to whom he needs to call, on the off chance that he learns the news. With time BolgenOS people group individuals comprehended that their plan doesn't work at times — there were situations when a few individuals didn't become familiar with the news by any stretch of the imagination. Presently they need to enhance the plan: they add into the plan a few directions of type (xi, yi), which imply that individual xi needs to call individual yi too. What is the base measure of guidelines that they need to add all in all, that toward the end everybody learns the news, regardless of who is quick to learn it?
Input :The initially input line contains number n (2 ≤ n ≤ 105) — measure of BolgenOS people group individuals. The subsequent line contains n space-isolated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — record of an individual, to whom calls an individual with list I.
Output :In the primary line output one number — the base measure of guidelines to add. Then, at that point, output one of the potential variations to add these directions into the plan, one guidance in each line. In the event that the arrangement isn't remarkable, output any
Step by step
Solved in 3 steps with 1 images