B - Tournament Editorial /

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 800800

問題文

NN 人の人が大会に出場しました。この大会はトーナメント形式であり、N1N-1 回の試合が行われました。 諸事情により、このトーナメントは全参加者に平等に組まれているとは限りません。 すなわち、各人に対し、優勝するために必要なその人が勝者となるような試合数が等しいとは限りません。 トーナメントの構造は、正式には問題文の末尾で定義されます。

各試合では必ず片方の人が勝者、もう片方の人が敗者となり、最後まで負けなかった 11 人が優勝となります。

図: トーナメントの例

人には 11 から NN までの番号がついており、11 番の人が優勝し、優勝者以外の人 i(2iN)i(2 ≦ i ≦ N) は人 aia_i との試合で負けました。

トーナメントの深さとは、すべての人に対する、その人が優勝するために必要な、その人が勝者となるような試合数の最大値です。

トーナメントの深さとしてありうる最小の値を求めてください。

トーナメントの構造の正式な定義は以下の通りです。ii 回目の試合では、

  • あらかじめ決められた、まだ試合をしていない 22 人の人
  • あらかじめ決められたまだ試合をしていない人 11 人と、あらかじめ決められた j(j<i)j(j<i) に対する、jj 回目の試合の勝者
  • あらかじめ決められた j,k(j,k<i,jk)j,k(j,k<i, j ≠ k) に対する、jj 回目の試合の勝者と kk 回目の試合の勝者

のうちのいずれかの 22 人が試合を行います。このような構造であって、どのように試合結果を決めても、すでに一度試合に負けた人が再び試合をすることのないようなものをトーナメントと呼びます。

制約

  • 2N1052 ≦ N ≦ 10^5
  • 1aiN(2iN)1 ≦ a_i ≦ N(2 ≦ i ≦ N)
  • 入力の試合結果と合致するようなトーナメントが存在する

入力

入力は以下の形式で標準入力から与えられる。

NN
a2a_2
:
aNa_N

出力

トーナメントの深さとしてありうる最小の値を出力せよ。


入力例 1Copy

Copy
5
1
1
2
4

出力例 1Copy

Copy
3

次のような試合結果が条件を満たします。

  • 11 回目の試合では、人 44 と 人 55 が対戦し、人 44 が勝つ
  • 22 回目の試合では、人 22 と 人 44 が対戦し、人 22 が勝つ
  • 33 回目の試合では、人 11 と 人 33 が対戦し、人 11 が勝つ
  • 44 回目の試合では、人 11 と 人 22 が対戦し、人 11 が勝つ
783f7be9c88350e31963edba8a958879.png

このトーナメントは、人 55 が優勝するためには 33 回勝利する必要があるので、深さ 33 のトーナメントとなります。 逆に、深さ 22 以下の条件を満たすトーナメントは存在しないので、33 を出力します。


入力例 2Copy

Copy
7
1
2
1
3
1
4

出力例 2Copy

Copy
3

入力例 3Copy

Copy
4
4
4
1

出力例 3Copy

Copy
3

Score : 800800 points

Problem Statement

NN contestants participated in a competition. The total of N1N-1 matches were played in a knockout tournament. For some reasons, the tournament may not be "fair" for all the contestants. That is, the number of the matches that must be played in order to win the championship may be different for each contestant. The structure of the tournament is formally described at the end of this statement.

After each match, there were always one winner and one loser. The last contestant standing was declared the champion.

Figure: an example of a tournament

For convenience, the contestants were numbered 11 through NN. The contestant numbered 11 was the champion, and the contestant numbered i(2iN)i(2 ≦ i ≦ N) was defeated in a match against the contestant numbered aia_i.

We will define the depth of the tournament as the maximum number of the matches that must be played in order to win the championship over all the contestants.

Find the minimum possible depth of the tournament.

The formal description of the structure of the tournament is as follows. In the ii-th match, one of the following played against each other:

  • Two predetermined contestants
  • One predetermined contestant and the winner of the jj-th match, where j(j<i)j(j<i) was predetermined
  • The winner of the jj-th match and the winner of the kk-th match, where jj and kk (j,k<i,jk)(j,k<i, j ≠ k) were predetermined

Such structure is valid structure of the tournament, if and only if no contestant who has already been defeated in a match will never play in a match, regardless of the outcome of the matches.

Constraints

  • 2N1052 ≦ N ≦ 10^5
  • 1aiN(2iN)1 ≦ a_i ≦ N(2 ≦ i ≦ N)
  • It is guaranteed that the input is consistent (that is, there exists at least one tournament that matches the given information).

Input

The input is given from Standard Input in the following format:

NN
a2a_2
:
aNa_N

Output

Print the minimum possible depth of the tournament.


Sample Input 1Copy

Copy
5
1
1
2
4

Sample Output 1Copy

Copy
3

The following tournament and the result of the matches are consistent with the given information:

  • In the first match, contestants 44 and 55 played against each other, and contestant 44 won.
  • In the second match, contestants 22 and 44 played against each other, and contestant 22 won.
  • In the third match, contestants 11 and 33 played against each other, and contestant 11 won.
  • In the fourth match, contestants 11 and 22 played against each other, and contestant 11 won.
783f7be9c88350e31963edba8a958879.png

The depth of this tournament is 33, since contestant 55 must play three matches in order to win the championship. There is no tournament with depth 22 or less that is consistent with the given information, thus the output should be 33.


Sample Input 2Copy

Copy
7
1
2
1
3
1
4

Sample Output 2Copy

Copy
3

Sample Input 3Copy

Copy
4
4
4
1

Sample Output 3Copy

Copy
3


2025-03-15 (Sat)
03:04:03 +00:00