#P11501. Expedition Team Selection

    ID: 13587 Type: Default 1000ms 256MiB

Expedition Team Selection

Expedition Team Selection

There are n candidates numbered from 1 to n for an expedition team. Each candidate may specify one person they do not want to join the expedition. For candidate i, the answer is an integer a_{i}. If ai = -1, candidate i has no objection; otherwise, candidate i does not want to join together with candidate ai.

You must select a subset of candidates such that for every selected candidate i with ai \neq -1, candidate ai is not selected. Your task is to determine the maximum possible number of candidates that can be selected while satisfying all these conditions.

The conflict condition between candidate i and candidate ai can be seen as a constraint: if both are selected then a candidate’s wish is violated. Note that the conflict is one‐directional (only matters when the candidate who specifies a forbidden companion is chosen), but selecting both would cause a violation. Equivalently, for each candidate i with ai \neq -1 an edge exists between i and ai, and you must pick an independent set in this graph with maximum size. Since each candidate forbids at most one other, the underlying graph is a pseudoforest (each connected component is either a tree or a tree with exactly one cycle).

If any formula is needed, use \( \lfloor k/2 \rfloor \) to represent the maximum independent set size for a cycle of length \(k\).

inputFormat

The first line contains an integer n (1 \(\le n \le\) 105), the number of candidates. The second line contains n space‐separated integers representing a1, a2, ..., an where each ai is either -1 or an integer in the range [1, n] (\(a_{i} = -1\) indicates candidate i has no objection, otherwise candidate i does not want to join with candidate ai).

outputFormat

Output a single integer, which is the maximum number of candidates that can be selected while satisfying all candidates’ wishes.

sample

3
-1 -1 -1
3