解题思路:
思路很简单,从每个小朋友开始找直到找到自己为止,记录寻找最大值。感觉要是数据强一点我这方法可能会超时,有大佬有好的优化方法可以在评论区写一下。
注意事项:
参考代码:
#include<iostream> #include<string.h> using namespace std; int a[100010]; int n; int maxx = 0; bool vis[100010]; int main(){ cin>>n; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } for(int i=1;i<=n;i++){ memset(vis,0,sizeof(vis)); int times = 1; int t = a[i]; while(t!=i&&!vis[t]){ vis[t] = true; t = a[t]; times++; } if(t==i&×>maxx) maxx = times; } cout<<maxx; return 0; }
0.0分
5 人评分