GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Saturday, 29 October 2022

[Solution] Hanging Hearts Codeforces Solution




E. Hanging Hearts
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pak Chanek has n blank heart-shaped cards. Card 1 is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card i (i>1) is hanging onto card pi (pi<i).

In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation a of [1,2,,n]. Then, the number written on card i is ai.

After that, Pak Chanek must do the following operation n times while maintaining a sequence s (which is initially empty):

  1. Choose a card x such that no other cards are hanging onto it.
  2. Append the number written on card x to the end of s.
  3. If x1 and the number on card px is larger than the number on card x, replace the number on card px with the number on card x.
  4. Remove card x.

After that, Pak Chanek will have a sequence s with n elements. What is the maximum length of the longest non-decreasing subsequence of s at the end if Pak Chanek does all the steps optimally?

 A sequence b is a subsequence of a sequence c if b can be obtained from c by deletion of several (possibly, zero or all) elements. For example, [3,1] is a subsequence of [3,2,1][4,3,1] and [3,1], but not [1,3,3,7] and [3,10,4].

Input

The first line contains a single integer n (2n105) — the number of heart-shaped cards.

The second line contains n1 integers p2,p3,,pn (1pi<i) describing which card that each card hangs onto.

Output

Print a single integer — the maximum length of the longest non-decreasing subsequence of s at the end if Pak Chanek does all the steps optimally.


No comments:

Post a Comment