爆搜,过了7/11个数据
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
int a[N];
bool st[N];
int n;
int ans;
void dfs(int start,int u,int step)
{
st[u] = true;
if(a[u] == start)
{
ans = max(ans,step + 1);
return ;
}
if(!st[a[u]])dfs(start,a[u],step+1);
}
int main()
{
cin>>n;
for (int i = 1; i <= n; i ++ )cin>>a[i];
for (int i = 1; i <= n; i ++ )
{
dfs(i,i,0);
memset(st, 0, sizeof st);
}
cout<<ans<<endl;
return 0;
}
后续补剪枝优化