Call forwarding chaos
InterviewTime limit1sMemory limit1024 MB
Given each phone's forwarding target, change the fewest settings so that every phone's call ends in a loop of two or more phones, and print the new settings.
- Level
Easy3 of 10
- Topics
- Graph, Implementation
- Solved
- No attempts yet
Problem
Dohun's unit has phones with a feature called call forwarding. Call forwarding sends an incoming call on one phone to another phone instead. For example, if phone forwards to phone , a call to phone rings phone . Each phone can forward to at most one other phone.
This feature has a serious flaw. When forwarding settings form a loop, some phones go dead. For example, phone forwards to phone , phone forwards to phone , and phone forwards to phone . Then a call to any of the three phones waits forever, and all three phones go dead.
Impressed by this, Dohun plans to change the forwarding settings of some phones so that every phone in the unit goes dead. The unit has phones numbered to . Phone currently forwards to phone . If , phone has no forwarding.
Changing too many settings would get Dohun caught by an officer, so he must change as few phones as possible. Find the minimum number of phones whose settings must change, along with the new settings. If several answers exist, any one is accepted. It is always possible to make every phone go dead.
Input
The first line contains , the number of phones. ()
The second line contains integers separated by spaces. ()
If , phone has no call forwarding.
Output
The first line contains the minimum number of phones whose forwarding settings must change so that every phone goes dead.
The second line contains integers , the forwarding settings after the changes. If several answers exist, print any of them.