This commit is contained in:
Zengtudor 2024-10-01 22:43:16 +08:00
parent 6756c27bf9
commit 1df9602ee5
2 changed files with 25 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.*
build
!.gitignore
*.json

View File

@ -2,26 +2,42 @@
#include <iostream>
#include <istream>
#include <queue>
#include<vector>
#include <vector>
#include <functional>
using std::cin,std::cout;
const int MAX_N {(int)2e5+5};
int n;
std::vector<int> next[MAX_N];
int inDegree[MAX_N];
int in_degree[MAX_N];
bool vis[MAX_N];
std::queue<int> q;
int ans;
int input;
int dfs(const int start, const int now, int depth){
depth++;
vis[now] = true;
int max_depth{ depth };
for (int i : next[now]) {
if (i == start) {
max_depth = std::max(max_depth, depth);
}
else {
max_depth = std::max(dfs(start, i, depth), max_depth);
}
}
return max_depth;
};
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]++;
in_degree[input]++;
next[i].push_back(input);
}
const auto push_vis = [](int n){
@ -29,7 +45,7 @@ int main(){
vis[n]=true;
};
for(int i=1;i<=n;i++){
if(inDegree[i]<=0){
if(in_degree[i]<=0){
push_vis(i);
}
}
@ -37,26 +53,16 @@ int main(){
int front = q.front();
q.pop();
for(int i:next[front]){
if(--inDegree[i]<=0){
if(--in_degree[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);
ans=std::max(ans,dfs(i,i,0));
}
cout<<ans<<"\n";
}