NOI Contest Archive

Archive / 2020 / July 2020

Prisoners and Enemies

Medium · National Olympiad in Informatics Sri Lanka - July 2020

The warden of Fox River needs to assign some of its prisoners for an outdoor activity. But as prisoners are cunning and always try to escape he wants to minimize the chance of an escape. With his experience, the warden knows that every prisoner has exactly one serious enemy, and that prisoner will never help his serious enemy to escape out. Knowing this fact the warden wants to make the biggest circle by selecting a set of prisoners such that for each prisoner in the circle has his serious enemy is either in his left or right. Thus no prisoners can make an escape attempt without getting his serous enemy’s attention. As warden is busy with prison work he asked your help.

Input Format

Output Format

T lines denoting the maximum number of the prisoners that can be put into the circle for each test case.

Constraints

Limits

Sample Input 0

3
5
2 3 4 5 1
5
2 3 4 1 1
9
2 3 6 3 4 5 5 5 7

Sample Output 0

5
4
4