#P2018. 消息传递

消息传递

Description

In the Kingdom of Bashu (Bāshǔ), the social hierarchy is strict: except for the king, everyone has exactly one direct superior, and the king has no superior. If AA is the superior of BB, and BB is the superior of CC, then AA is the superior of CC. It is absolutely impossible to have a relationship where AA is the superior of BB and BB is also the superior of AA.

The initial time is 00. Your task is to spend 11 unit of time to tell a message to one person, and then let them spread the message on their own. In any time unit, any person who has already received the message can tell the message to one of their direct superiors or direct subordinates.

Now, you want to know:

  1. How long will it take for the message to reach everyone in the entire Kingdom of Bashu?
  2. Which people can be chosen so that the total time spent during transmission is minimized?

Input Format

The first line of input contains an integer NN (N1000N \le 1000), the total number of people in the Kingdom of Bashu. People are numbered from 11 to NN, and the king’s number is 11. Lines 22 through NN (a total of N1N-1 lines) each contain one integer; for each ii from 22 to NN, line ii gives the number of the direct superior of person ii.

Output Format

The output contains two lines:

  • The first line contains one integer, the earliest time when the last person receives the message.
  • The second line contains several integers, the numbers of all valid choices of the initial person, output in increasing order and separated by single spaces.
8
1
1
3
4
4
4
3
5
3 4 5 6 7

Hint

Translated by ChatGPT 5