Gaggle

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

At the new start-up company Gaggle, we have rejected the oppressive corporate structures of old, with all of their managers and subordinates and hierarchies and so on. Instead we have embraced a free and open corporate culture in which all employees (called Gagglers) are in charge of themselves and allowed to roam free.

Rather than having managers overseeing the work, the main method used to coordinate work at Gaggle is a mentor system: each Gaggler designates some other Gaggler as their mentor, with whom they discuss their ongoing projects.  This mentor relation may or may not be symmetric (in other words you may or may not be the mentor of your mentor) but you can never be the mentor of yourself.

Initially, all Gagglers were able to pick anyone they liked as their mentor, but after a while it was discovered that this lead to two problems:

  1. Some people were more popular than others and had too many choosing them as their mentor, causing them not to have time to do their actual work.
  2. Some flocks of Gagglers ended up isolated from the rest of the company (e.g., if Gagglers AA and BB are each other's mentors and they are not the mentor of anyone else), causing failure of these flocks to coordinate with the rest of the company.

In order to remedy these two flaws, it was (collectively) decided that:

  1. Every Gaggler must be the mentor of exactly one other Gaggler, and
  2. Assuming every Gaggler only communicates with their mentor and their mentee, it must still be possible for any information that any Gaggler has to reach any other Gaggler.

In order to reward lower-numbered (more senior) Gagglers while introducing this new policy, it was decided that lower-numbered Gagglers should get to keep their current mentor if possible, and if they have to change, their new mentor should be as low-numbered (more senior, and therefore more experienced) as possible.

Concretely, consider two possible new assignments of mentors, and suppose the lowest-numbered Gaggler where these assignments differ is Gaggler number ii.  Then if one of the two assignments assigns Gaggler ii the same mentor as they originally had, we prefer that assignment.  Otherwise, if Gaggler ii gets a new mentor in both of the two assignments, then we prefer the assignment where the number of the new mentor of Gaggler ii is smaller.

For example, consider Sample Input 2 below.  One possible new assignment of mentors would be to simply change so that Gaggler 11 becomes mentored by Gaggler 22.  However, in the best assignment, shown in Sample Output 2, we let Gaggler 11 keep their current mentor and instead change the mentors of both Gagglers 22 and 33.

입력

The first line of input contains a single integer nn (2n500,0002 \le n \le 500\\,000), the number of Gagglers.  Then follows a line containing nn integers a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n (1a_in1 \le a\_i \le n and a_iia\_i \ne i for each ii) where a_ia\_i is the current mentor of Gaggler ii (the Gagglers are numbered from 11 to nn).

출력

Then output a line with the new assignment b_1,,b_nb\_1, \ldots, b\_n of mentors, in the same format as in the input.  The new list should be a valid assignment according to the new requirements, and be the best according to the tie-breaking rule described above.