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 build
!.gitignore !.gitignore
*.json

View File

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