The Championship
Time limit1sMemory limit128 MB
Pair as many employees as possible so each team holds two people with neither managing the other.
Problem
A company has a hierarchical chain of command.
- Each employee has at most one direct manager.
- The "is a manager of" relation is transitive: if A is a manager of B (not necessarily directly) and B is a manager of C, then A is a manager of C.
- The relation has no cycles: there are no two distinct people A and B such that A is a manager of B while B is a manager of A at the same time.
The company decides to hold a beach volleyball championship contested by teams of two employees. So that nobody feels awkward, a team may be formed only from a pair (A, B) where A is not a manager of B and B is not a manager of A. Each employee can belong to at most one team.
What is the maximum number of teams that can enter the championship?
Input
The first line contains the number of test cases Z ().
For each test case, the first line contains the number of employees N (). The second line contains N integers , where is the index of the direct manager of employee (). If employee has no manager, then .
Output
For each test case, print on its own line the maximum number of teams that can enter the championship.