diff --git a/.gitignore b/.gitignore index fd8b854..9cb2338 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .* build -!.gitignore \ No newline at end of file +!.gitignore +*.json \ No newline at end of file diff --git a/P2661/main.cpp b/P2661/main.cpp index 7f5e3c1..5b4243a 100644 --- a/P2661/main.cpp +++ b/P2661/main.cpp @@ -2,26 +2,42 @@ #include #include #include -#include +#include +#include using std::cin,std::cout; const int MAX_N {(int)2e5+5}; int n; std::vector next[MAX_N]; -int inDegree[MAX_N]; +int in_degree[MAX_N]; bool vis[MAX_N]; std::queue 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<