#P1543. Maximizing Selected Monitored Students
Maximizing Selected Monitored Students
Maximizing Selected Monitored Students
There are n students numbered from 1 to n, managed by the class monitor (whose number is 0). Each student (except the class monitor) supervises exactly one other student. In other words, for every student i (1 ≤ i ≤ n) an integer f(i) is given with 0 ≤ f(i) ≤ n. Here f(i)=0 means that student i is assigned to monitor the class monitor; note that no one supervises the class monitor.
You are to help the class monitor select as many students as possible to perform a task under the following restrictions:
- A student v can only be selected if at least one student supervises her, i.e. there exists some u with f(u)=v (we call such v eligible).
- The selected set S must satisfy that for every selected student v (i.e. v ∈ S) there is at least one supervisor u with f(u)=v who is not selected.
Using the notation below, the second restriction can be written in LaTeX as follows:
$$\text{For every }v\in S,\quad \exists\, u\text{ with }f(u)=v\text{ and }u\notin S. $$Since every student (except the class monitor) supervises exactly one other student, choosing a student in S forces that at least one of her supervisors is not chosen. In fact, you may view every student u (that is not selected) as a witness for her supervisee f(u) if f(u) is selected. Note that a witness can cover exactly one selected student. (Also note that if a student is not supervised by any other student then she cannot be selected.)
Find the maximum possible size of S.
inputFormat
The first line contains a single integer n (1 ≤ n ≤ 2×105), the number of students (excluding the class monitor).
The second line contains n integers: f(1), f(2), …, f(n), where 0 ≤ f(i) ≤ n. It is guaranteed that for every i (1 ≤ i ≤ n) the student i supervises exactly one student, and the class monitor (number 0) is supervised by no one.
Note: A student v is eligible for selection only if there is at least one u with f(u)=v (i.e. if v is being supervised by someone).
outputFormat
Output a single integer, the maximum number of students that can be selected such that for every selected student v, at least one of her supervisors (i.e. some u with f(u)=v) is not selected.
sample
2
0 1
1
</p>