algorithm_2024/P2661/main.cpp
2024-10-01 22:03:35 +08:00

62 lines
1.2 KiB
C++

#include <algorithm>
#include <iostream>
#include <istream>
#include <queue>
#include<vector>
using std::cin,std::cout;
const int MAX_N {(int)2e5+5};
int n;
std::vector<int> next[MAX_N];
int inDegree[MAX_N];
bool vis[MAX_N];
std::queue<int> q;
int ans;
int input;
int main(){
std::iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
std::cin>>n;
for(int i=1;i<=n;i++){
cin>>input;
inDegree[input]++;
next[i].push_back(input);
}
const auto push_vis = [](int n){
q.push(n);
vis[n]=true;
};
for(int i=1;i<=n;i++){
if(inDegree[i]<=0){
push_vis(i);
}
}
while(q.empty()==false){
int front = q.front();
q.pop();
for(int i:next[front]){
if(--inDegree[i]<=0){
push_vis(i);
}
}
}
const auto dfs = [](){
};
for(int i=1;i<=n;i++){
if(vis[i])continue;
int loop_size {0};
int start {i};
int now {start};
do{
vis[now]=true;
now=next[now];
loop_size++;
}while(now!=start);
ans=std::max(ans,loop_size);
}
cout<<ans<<"\n";
}