P26889 [AtCoder] Permutation
提高+
题目描述

Problem Description

You are given a permutation of $1,\ 2,\ \ldots,\ N$ as $A_1,\ A_2,\ \ldots,\ A_N$.

For each integer $1\ \leq\ i\ \leq\ N$, find the smallest integer $ j $ greater than or equal to $ 1 $ that satisfies the following condition:

  • Let $x\ =\ i$. After performing the operation of replacing $ x $ with $ A_x $ exactly $ j $ times, $x\ =\ i$.

Input Format

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

$ N $ $ A_1 $ $ A_2 $ $\ldots$ $ A_N $

Output Format

For each $1,\ 2,\ \ldots,\ N$, output in this order, the smallest integer $ j $ greater than or equal to $ 1 $ that satisfies the condition described in the problem statement.

Sample Input and Output

Sample Input #1

6
1 3 2 5 6 4

Sample Output #1

1 2 2 3 3 3

Sample Input #2

3
3 2 1

Sample Output #2

2 1 2

Sample Input #3

2
1 2

Sample Output #3

1 1

Notes/Hints

Constraints

  • $1\ \leq\ N\ \leq\ 100$
  • $1\ \leq\ A_i\ \leq\ N$
  • $A_i\ \neq\ A_j\ (i\ \neq\ j)$
  • All inputs are integers

Sample Explanation 1

  • For $i\ =\ 1$, since $A_1\ =\ 1$, the smallest $ j $ that satisfies the condition is $ 1 $.
  • For $i\ =\ 2$, since $A_2\ =\ 3$ and $A_3\ =\ 2$, the smallest $ j $ that satisfies the condition is $ 2 $.
  • For $i\ =\ 3$, since $A_3\ =\ 2$ and $A_2\ =\ 3$, the smallest $ j $ that satisfies the condition is $ 2 $.
  • For $i\ =\ 4$, since $A_4\ =\ 5$, $A_5\ =\ 6$, and $A_6\ =\ 4$, the smallest $ j $ that satisfies the condition is $ 3 $.
  • For $i\ =\ 5$, since $A_5\ =\ 6$, $A_6\ =\ 4$, and $A_4\ =\ 5$, the smallest $ j $ that satisfies the condition is $ 3 $.
  • For $i\ =\ 6$, since $A_6\ =\ 4$, $A_4\ =\ 5$, and $A_5\ =\ 6$, the smallest $ j $ that satisfies the condition is $ 3 $.
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

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