#P2018. Optimal Message Broadcast in a Hierarchical Kingdom
Optimal Message Broadcast in a Hierarchical Kingdom
Optimal Message Broadcast in a Hierarchical Kingdom
The Kingdom of Bashu has a strict hierarchical structure. Except for the king, every person has exactly one direct superior. Of course, the king has no superior. Moreover, the superior relation is transitive: if A is the superior of B and B is the superior of C, then A is also the superior of C. It is guaranteed that no cyclic superior relationships exist.
At time \(0\) you start by spending 1 unit of time to send a message to one chosen person; that person will then start disseminating the message. In every subsequent unit of time, any person who has received the message can pass it on to exactly one of his/her direct neighbors (either his/her direct superior or any of his/her direct subordinates).
Your task is to determine:
- The minimum amount of time needed for the message to spread to everyone in the kingdom.
- The list of people (by their ID numbers) for which choosing them as the initial recipient minimizes the total time of message dissemination.
Note: If a person u has several neighbors to inform, the optimal strategy is to call the neighbor with the largest delay first. Formally, define a function \(f(u, p)\) for a node \(u\) (with parent \(p\)) as follows:
[ f(u, p) = \max_{0 \leq i < k} {a_i + i} \quad \text{with} \quad a_i = f(v_i, u) + 1 ]
where \(\{v_0, v_1, \dots, v_{k-1}\}\) are the neighbors of \(u\) (excluding \(p\)) sorted in descending order by \(f(v, u) + 1\). The overall broadcast time when starting at node \(s\) is \(T(s) = f(s, \text{null}) + 1\) (the extra 1 accounts for the initial transmission at time 0).
inputFormat
The first line contains a single integer (n) (number of people).
For (n \ge 2), the second line contains (n-1) space-separated integers, where the (i)-th integer (for (i=1) to (n-1)) represents the direct superior of person (i+1). It is guaranteed that the hierarchy forms a tree structure (i.e. a connected acyclic graph).
outputFormat
Output two lines:
The first line contains a single integer representing the minimum total time required for the message to reach everyone in the kingdom.
The second line contains the list of all candidate person IDs (in increasing order) that achieve this optimal time, separated by a space.
sample
1
1
1
</p>