Created
April 17, 2016 05:36
-
-
Save bowbowbow/f92b2f032128455334c740e1a15834de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 1등 nika 코드 참고함 */ | |
#include <iostream> | |
#include <cstdio> | |
#include <vector> | |
#include <algorithm> | |
#include <queue> | |
#include <sstream> | |
using namespace std; | |
#define FOR(i,N) for(int i=1; i<=N; i++) | |
#define MAXN 1001 | |
int main(){ | |
freopen("codejam/C/C-large-practice.in", "r", stdin); | |
freopen("codejam/C/C-large-practice.out", "w", stdout); | |
int T, N; | |
cin >> T; | |
FOR(t, T){ | |
int g[MAXN]; | |
cin >> N; | |
FOR(i, N) cin >> g[i]; | |
int ans = 0; | |
bool ck[MAXN]; | |
FOR(i, N){ | |
FOR(j, N) ck[j] = false; | |
int now = i, depth = 0; | |
while(!ck[now]){ | |
depth++; | |
ck[now] = true; | |
now = g[now]; | |
} | |
if(now == i) ans = max(ans, depth); | |
} | |
int dis[MAXN]; | |
FOR(i, N) dis[i] = 0; | |
FOR(i, N) FOR(j, N) if(g[g[j]] != j){ | |
dis[g[j]] = max(dis[g[j]], dis[j]+1); | |
} | |
int ans2 = 0; | |
FOR(i, N) ck[i] = false; | |
FOR(i, N) if(g[g[i]] == i && !ck[i]){ | |
ck[i] = ck[g[i]] = true; | |
ans2 += 2+dis[i]+dis[g[i]]; | |
} | |
printf("Case #%d: %d\n", t, max(ans, ans2)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment