ProgramAlgTrain/20240830/P2661/P2661.cpp

50 lines
1002 B
C++
Raw Normal View History

2024-08-30 10:04:27 +00:00
#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]);
}
}
2024-08-30 10:19:48 +00:00
int ans{-1};
2024-08-30 10:04:27 +00:00
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";
}