Spies
Time limit3sMemory limit512 MB
Given a functional graph where spy k shadows a_k, choose the largest subset S such that every member of S is shadowed by at least one spy outside S.
- Level
Medium7 of 10
- Topics
- Graph, Greedy, Dynamic programming
- Solved
- No attempts yet
Problem
An intelligence agency employs spies. Each spy shadows exactly one other spy. This shadowing assignment is fixed: spy shadows spy (with ).
The agency wants to assign as many spies as possible to a secret operation. However, every spy taking part in the operation must be shadowed by at least one spy that does not take part in it. (The shadowing assignment never changes.)
Write a program that:
- reads from standard input which spy each spy shadows,
- computes the maximum number of spies that can be assigned to the operation so that each of them is shadowed by at least one spy not taking part in the operation,
- writes the result to standard output.
Input
The first line contains the number of spies (). The spies are numbered from to . Each of the next lines describes whom a spy shadows: the -th line contains a single integer , meaning that spy shadows spy (, , ).
Output
Print a single integer: the maximum number of spies that can be assigned to the secret operation.
Hint
