Google codes error 2016: round 1A, BFF

Question:

You are a teacher in the brand new Little Coders kindergarten. You have N children in your class and they each have a different student ID number from 1 to N. Every child in your class has one best friend forever (BFF) and you know who that BFF is for every child. BFFs are not necessarily reciprocal, that is, B being A BFF does not mean A is B BFF.

Your lesson plan for tomorrow includes activities in which participants should sit in a circle. You want to make the activity as successful as possible by creating the largest possible circle of children, with each child in the circle sitting right next to their BFF, either left or right. Any children outside the circle will observe the activity without participation.

What is the largest number of children in a circle?

Input

The first line of input gives the number of test cases, followed by TT test cases. Each test case consists of two lines. The first line of the test case contains one integer N, the total number of children in the class. The second line of the test case contains N integers F1, F2, ..., FN, where Fi is the BFF student ID of the child with student ID i.

Output

For each test case, print one line containing "Case #x: y", where x is the test case number (starting from 1), and y is the maximum number of children in the group that can be arranged in a circle so that each child in the circle sits next to his BFF.

My problem: there is an analysis of the competition on the jam-code site, but I don't understand it. Where does the optimization take place? If someone can explain this problem and the solution in detail, it will be very helpful.

Edit: I am not adding pseudocode because I want to better understand the problem and it is not an encoding problem.

+3


source to share





All Articles