#P2661. Information Relay Game

    ID: 15926 Type: Default 1000ms 256MiB

Information Relay Game

Information Relay Game

There are n students labeled from 1 to n playing an information relay game. In this game, each student has a fixed person to whom they relay the information. Specifically, for each student i, the designated target is student Ti (with 1 ≤ i ≤ n).

At the start, every student only knows his or her own birthday. In each round, every student simultaneously sends all the birthday information they have collected so far to their designated target. Note that a student might receive information from multiple sources in a single round; however, each student only sends information to one fixed person.

The game ends at the moment when some student hears his or her own birthday from someone else (i.e. not from oneself). Determine the number of rounds that the game will last.

Mathematical Formulation:

Let the mapping T: {1, 2, ..., n} → {1, 2, ..., n} be given such that student i sends to Ti. The game stops in the first round r for which there exists a student i and some student j (with j ≠ i) such that the birthday of i comes to i via a sequence of transmissions with exactly r rounds. It can be shown that the game stops when, for the first time, a cycle of length at least 2 is fully formed. In fact, if a cycle has length k (with k ≥ 2), then each student on that cycle will receive his or her birthday from another student exactly in round k.

Your task is to compute the number of rounds the game will take before it ends.

inputFormat

The input consists of two lines. The first line contains a single integer n (the number of students). The second line contains n integers: T1, T2, …, Tn, where each Ti (1 ≤ Ti ≤ n) is the designated target of student i.

outputFormat

Output a single integer representing the number of rounds that the game lasts before a student hears his/her own birthday from another student.

sample

3
2 3 1
3

</p>