GCJ Round 1A 2016 C.BFFS

Problem

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, TT 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 F1F2, ..., 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 ≤ Fi ≤ N, 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.

題意:

A有一個最好的朋友B,但B不一定認爲A是他最好的朋友。每個人都擁有一個最好的朋友,若安排大家坐成一個環,每個人的左邊或者右邊必須坐自己最好的朋友,否則不能坐進這個環中。問這個環最多能容納多少人。

分析:

最大的環有兩種情況(假設此時最佳的環容納了n人):

1.n個人恰好頭尾銜接組成一個圓環,此時情況最簡單,無法在這個環中增加任何人,即最大的環最多容納n人。

2.n個人並非頭尾銜接,而是類似於A-B-C-D-C的情況,出現了兩個人互相爲最好的朋友,如例中C-D-C,構成了一個大小爲2的頭尾銜接的環,此時可在C與D兩邊各坐一名認爲C(或認爲D)是最好朋友的人,並依此找出這兩個人能聯合的最大長度。如例中C旁邊是B,B-A,A此時已經有最好的朋友B在旁邊,則另一邊可接受一個非最好朋友X,X就是D另一邊的尾部。此種情況相當於一個環帶着兩條邊,結果就是2+兩條邊的長度。

代碼先算情況1最大環大小,再算情況2最大環大小,最後取最大的。

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const int N=1015;
const int mod=1e9+7;

int a[N],v[N],d[N];

int main() {
    int t,n,m,l,k;
    ifstream ifile;
    ofstream ofile;
    ofile.open("/Users/lijiechen/Downloads/outt.txt",ios::out);
    ifile.open("/Users/lijiechen/Downloads/C-large-practice.in.txt",ios::out);
    ifile>>t;
    int kase=0;
    while (t--) {
        ifile>>n;
        kase++;
        int ans=0;
        for (int i=1; i<=n; i++) {
            ifile>>a[i];
        }
        for (int i=1; i<=n; i++) {
            for (int j=1; j<=n; j++) {
                v[j]=0;
            }
            k=0;
            int flag=i;
            while (1) {
                v[flag]=1;
                flag=a[flag];
                k++;
                if (v[flag]) break;
            }
            if (flag==i) ans=max(ans, k);
        }
        for (int j=1; j<=n; j++) {
            v[j]=d[j]=0;
        }
        for (int j=1; j<=n; j++) {
            for (int i=1; i<=n; i++) {
                if (a[a[i]]!=i) d[a[i]]=max(d[a[i]], d[i]+1);
            }
        }
        int ans2=0;
        for (int i=1; i<=n; i++) {
            if (!v[i]&&a[a[i]]==i) {
                ans2+=2+d[i]+d[a[i]];
                v[i]=v[a[i]]=1;
            }
        }
        ans=max(ans, ans2);
        ofile<<"Case #"<<kase<<": "<<ans<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章