Google Code Jam 2016 Round 1A Problem C in J language

You are a teacher at the brand new Little Coders kindergarten. You have N kids in your class, and each one has a different student ID number from 1 through N. Every kid in your class has a single best friend forever (BFF), and you know who that BFF is for each kid. BFFs are not necessarily reciprocal -- that is, B being A's BFF does not imply that A is B's BFF.

Your lesson plan for tomorrow includes an activity in which the participants must sit in a circle. You want to make the activity as successful as possible by building the largest possible circle of kids such that each kid in the circle is sitting directly next to their BFF, either to the left or to the right. Any kids not in the circle will watch the activity without participating.

What is the greatest number of kids that can be in the circle?

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of two lines. The first line of a test case contains a single integer N, the total number of kids in the class. The second line of a test case contains N integers F1, F2, ...,FN, where Fi is the student ID number of the BFF of the kid with student ID i.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the maximum number of kids in the group that can be arranged in a circle such that each kid in the circle is sitting next to his or her BFF.

Limits

1 ≤ T ≤ 100.
1 ≤ FiN, for all i.
Fi ≠ i, for all i. (No kid is their own BFF.)

Small dataset

3 ≤ N ≤ 10.

Large dataset

3 ≤ N ≤ 1000.

Sample

Input Output
4
4
2 3 4 1
4
3 3 4 1
4
3 3 4 3
10
7 8 10 10 9 2 9 6 3 3
Case #1: 4
Case #2: 3
Case #3: 3
Case #4: 6

In sample case #4, the largest possible circle seats the following kids in the following order: 7 9 3 10 4 1. (Any reflection or rotation of this circle would also work.) Note that the kid with student ID 1 is next to the kid with student ID 7, as required, because the list represents a circle.

Solution in J lang:


#!/usr/bin/env jconsole

NB. convert string to decimal
atoi=: 10"_#. '0123456789'"_ i. ]

visit=: ''
friends=: ''

NB. Handle case
case=: 3 : 0
    NB. convert the number of cases into decimal
    count=: atoi > 0 { y
    current=: 0
    line=: 0
    while. current ~: count do.
        NB. take input, compute and print
        current=: >: current
        line=: >: line
        n=: atoi > line { y
        line=: >: line
        bffs=: 0, atoi ;._1 ' ', > line { y
        answer=: 0
        i=: 1
        while. i <: n do.
            visit=: , 1 1024 $ 0
            k=: 0
            j=: i
            while. 0 = j { visit do.
                visit=: 1 j } visit
                j=: j { bffs
                k=: >: k
            end.
            if. i = j do.
                answer=: answer >. k
            end.
            i=: >: i
        end.
        
        visit=: , 1 1024 $ 0
        friends=: , 1 1024 $ 0
        
        j=: 1
        while. j <: n do.
            i=: 1
            while. i <: n do.
                friend=: i { bffs
                if. i ~: friend { bffs do.
                    max=: ((friend { friends) >. (>: (i { friends)))
                    friends=: max friend } friends
                end.
                i=: >: i
            end.
            j=: >: j
        end.
        
        answer2=: 0
        i=: 1
        while. i <: n do.
            if. 0 = i { visit do.
                if. ((i { bffs) { bffs) = i do.
                    answer2=: answer2 + 2 + (i { friends) + ((i { bffs) { friends)
                    visit=: 1 (i { bffs) } visit
                    visit=: 1 i } visit
                end.
            end.
            i=: >: i
        end.
        
        NB. print answer
        echo ; ":&.> 'Case #' ; current ; ': ' ; (answer >. answer2)
    end.
)


NB. get data and handle them
] input=: <;._1 LF , stdin ''
case input
exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *

seven − two =