#P13191. [GCJ 2016 #1A] BFFs

    ID: 15060 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 4 上传者: 标签>图论2016深度优先搜索 DFSGoogle Code Jam

[GCJ 2016 #1A] BFFs

题目描述

You are a teacher at the brand new Little Coders kindergarten. You have N\mathbf{N} kids in your class, and each one has a different student ID number from 1 through N\mathbf{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?

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case consists of two lines. The first line of a test case contains a single integer N\mathbf{N}, the total number of kids in the class. The second line of a test case contains N\mathbf{N} integers F1\mathbf{F}_1, F2\mathbf{F}_2, ..., FN\mathbf{F}_\mathbf{N}, where Fi\mathbf{F}_i is the student ID number of the BFF of the kid with student ID ii.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy 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.

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

提示

Sample Explanation

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.

Sample Explanation

  • 1T1001 \leqslant \mathbf{T} \leqslant 100.
  • 1FiN1 \leqslant \mathbf{F}_i \leqslant \mathbf{N}, for all ii.
  • Fii\mathbf{F}_i \neq i, for all ii. (No kid is their own BFF.)

Small dataset (16 Pts, Test Set 1 - Visible)

  • 3N103 \leqslant \mathbf{N} \leqslant 10.

Large dataset (29 Pts, Test Set 2 - Hidden)

  • 3N10003 \leqslant \mathbf{N} \leqslant 1000.