Under construction

Still being built. Things may change

Gaggle

Time limit2sMemory limit512 MB

Level

Not classified yet

Solved
No attempts yet

Gaggle

Gaggle is a start-up that replaced its management structure with mentor relations. Each Gaggler designates one other Gaggler as their mentor, and a mentor relation may or may not be symmetric. Nobody can be their own mentor.

The company wants two things. First, every Gaggler must be the mentor of exactly one other Gaggler. Second, if each Gaggler only communicates with their mentor and their mentee, any Gaggler must be able to pass information to any other Gaggler.

Lower-numbered Gagglers keep their current mentor whenever possible. Compare two valid assignments at the lowest index ii where they differ. If one of them gives Gaggler ii the current mentor, that assignment wins. Otherwise, the assignment where Gaggler ii gets the smaller new mentor number wins.

Given the current mentors a1,…,ana_1, \ldots, a_n, output the best new assignment b1,…,bnb_1, \ldots, b_n.

Examples2

  1. Example 1

    Input
    4
    2 1 4 3
    
    Expected output
    2 3 4 1
    
  2. Example 2

    Input
    3
    3 3 1
    
    Expected output
    3 1 2