#P6417. Maximum Number of Bad Guys

    ID: 19632 Type: Default 1000ms 256MiB

Maximum Number of Bad Guys

Maximum Number of Bad Guys

There are n people. Some of them are civilians, and some are bad guys. All n people have accused exactly one person of being a bad guy. The civilians make arbitrary accusations, but if a person is a bad guy then he always accuses a civilian.

Your task is to determine the maximum possible number of bad guys under these conditions.

Formally, assume the people are numbered from 1 to n. You are given an array f of n integers, where f[i] (1‑indexed) is the number of the person accused by person i. We call an assignment of each person as either a bad guy or a civilian valid if for every person i that is assigned as a bad guy, the person f[i] is assigned as a civilian. (Note that civilians can accuse anyone, and there is no restriction on whom a civilian may accuse.)

Find the maximum possible size of the set of bad guys among all valid assignments.

Important: A person who accuses him/herself (i.e. f[i] = i) cannot be a bad guy because that would force him/her to be a civilian at the same time.

inputFormat

The first line contains an integer n (1 ≤ n ≤ 105), the number of people.

The second line contains n integers f[1], f[2], …, f[n] (1 ≤ f[i] ≤ n). It is possible that f[i] = i (self‑accusation).

outputFormat

Output a single integer, the maximum possible number of bad guys.

sample

3
2 3 1
1