6762.Planets Cycles

Time Limit: 1s Memory Limit: 512MB

You are playing a game consisting of $n$ planets. Each planet has a teleporter to another planet (or the planet itself).

You start on a planet and then travel through teleporters until you reach a planet that you have already visited before.

Your task is to calculate for each planet the number of teleportations there would be if you started on that planet.

Input Format(From the terminal/stdin)

The first input line has an integer $n$ : the number of planets. The planets are numbered $1,2,\dots,n$ .

The second line has $n$ integers $t_1,t_2,\dots,t_n$ : for each planet, the destination of the teleporter. It is possible that $t_i=i$ .

  • $1 \le n \le 2 \cdot 10^5$
  • $1 \le t_i \le n$

Output Format(To the terminal/stdout)

Print $n$ integers according to the problem statement.

Sample Input

Copy
5
2 4 3 1 4
 \n
 · · · · \n

Sample Output

Copy
3 3 1 3 4
 · · · · \n
Source: CSES, Graph Algorithms, 1751

Submit

请先 登录

© 2025 FAQs