50 lines
1002 B
C++
50 lines
1002 B
C++
#include <algorithm>
|
|
#include <iostream>
|
|
#include <queue>
|
|
const int MAX_N{int(2e5+5)};
|
|
|
|
int in_degree[MAX_N]{},n,next[MAX_N];
|
|
std::queue<int> q;
|
|
bool vis[MAX_N];
|
|
|
|
int main(){
|
|
std::cin>>n;
|
|
for(int i=1;i<=n;i++){
|
|
int input;
|
|
std::cin>>input;
|
|
in_degree[input]++;
|
|
next[i]=input;
|
|
}
|
|
for(int i=1;i<=n;i++){
|
|
if(in_degree[i]==0){
|
|
q.push(i);
|
|
}
|
|
}
|
|
|
|
while(q.empty()==false){
|
|
int top = q.front();
|
|
in_degree[next[top]]--;
|
|
q.pop();
|
|
if(in_degree[next[top]]==0){
|
|
q.push(next[top]);
|
|
}
|
|
}
|
|
|
|
int ans{-1};
|
|
|
|
for(int i=1;i<=n;i++){
|
|
if(in_degree[i]!=0 && vis[i]==false){
|
|
int begin{i},s{1};
|
|
int now{next[begin]};
|
|
vis[begin]=true;
|
|
while(now!=begin){
|
|
vis[now]=true;
|
|
s++;
|
|
now=next[now];
|
|
}
|
|
ans=std::max(ans,s);
|
|
}
|
|
}
|
|
|
|
std::cout<<ans<<"\n";
|
|
} |