P25453 [AtCoder] Arrangement
提高+
题目描述

Problem Description

Niwango-kun has $ N $ cards numbered $1, 2, \ldots, N$. He decided to arrange these cards in a row.

Niwango-kun wants to know if there exists an arrangement of the cards that satisfies all of the following $ N $ conditions. Determine if such an arrangement exists for Niwango-kun, and if it does, find the lexicographically smallest arrangement.

  • The card to the right of card $ 1 $ (if it exists) is not $ a_1 $
  • The card to the right of card $ 2 $ (if it exists) is not $ a_2 $
  • $\vdots$
  • The card to the right of card $ N $ (if it exists) is not $ a_N $

Input Format

The input is given from the standard input in the following format:

$ N $ $ a_1 $ $ a_2 $ $\ldots$ $ a_N $

Output Format

If no arrangement satisfies the conditions, output -1. If an arrangement exists, output the lexicographically smallest arrangement of the cards in the following format, where $ b_i $ is the number of the card at the $ i $-th position from the left.

$ b_1 $ $ b_2 $ $\ldots$ $ b_N $

Sample Input and Output

Sample Input #1

4
2 3 4 1

Sample Output #1

1 3 2 4

Sample Input #2

2
2 1

Sample Output #2

-1

Sample Input #3

13
2 3 4 5 6 7 8 9 10 11 12 13 12

Sample Output #3

1 3 2 4 6 5 7 9 8 10 12 11 13

Notes/Hints

Constraints

  • $2 \leq N \leq 10^{5}$
  • $1 \leq a_i \leq N$
  • $a_i \neq i$

Sample Explanation 1

  • The arrangement $ (1, 2, 3, 4) $ is lexicographically smaller than $ (1, 3, 2, 4) $, but it violates the condition that the card to the right of card $ 1 $ is not $ 2 $, making it unsuitable.

Sample Explanation 2

  • Output -1 if no arrangement satisfies the conditions.
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 2 3 4 1
样例输出
1 3 2 4