Problem B
Climbing the Ladder
Languages
en
is

You have obtained a map of the computer system belonging to the blackhat hacker group you are attempting to stop. Each hacker has a hacker boss with whom they discuss their plans. There is one exception, which is the hacker leader, who is their own boss. When you attempt a remote access to take over a computer, the subordinates of the hacker who owns the computer might notice it, due to no longer receiving responses or receiving suspicious responses about their plans. Thus, you must have control of all the computers of a hacker’s subordinates before taking control of that hacker’s computer. This only counts direct subordinates, if someone has this hacker as their boss’s boss you do not need to control their computer to get this hacker’s computer.
In order to minimise the chances of your operation being noticed, you want to control as few computers as possible at any given moment. You can release control of a computer at any given moment by disconnecting from it.
You are given the hierarchy of the blackhat hacking group. What is the minimum amount of computers you must control at the same time in order to take control of the leader’s computer while going unnoticed?
Input
The first line of the input contains a single integer $n$, representing the number of blackhat hackers, where $1 \leq n \leq 200\, 000$.
Then $n-1$ lines follow, where the $i$th line contains a single integer $p_{i+1}$, describing who is the boss of hacker $i+1$, where $1 \leq p_{i+1} < i + 1$. Hacker $1$ is always the hacker who is their own boss, as they are the CEO of blackhat hacking.
Output
Output a single integer representing the minimum number of computers you must control at the same time in order to take control of the leader’s computer.
Explanation of samples
In the first sample we start by taking control of computers number $5, 6$ and $2$ respectively. Then we control $3$ computers at once. Then we sever the connection to $5, 6$ and gain control of $4$ and $3$ next respectively. Finally we can then sever the connection to $4$ and gain control of computer $1$. Then we finish without ever having more than $3$ computers at once, and this is the best we can do.
![\includegraphics[width=0.5\textwidth ]{tree1}](/problems/klifrauppstigann/file/statement/en/img-0002.png)
![\includegraphics[width=0.5\textwidth ]{tree2}](/problems/klifrauppstigann/file/statement/en/img-0003.png)
Sample Input 1 | Sample Output 1 |
---|---|
6 1 1 3 2 2 |
3 |
Sample Input 2 | Sample Output 2 |
---|---|
14 1 2 1 4 5 4 7 8 7 1 11 12 11 |
4 |