题目
题解
遍历。
这是提高题?这比一半的普及题还普及吧。。。
太简单了,肯定不会有人搜这个博客。
如果你搜到了。。。那么你确实该好好学一下算法了。
代码
#include<bits/stdc++.h>
using namespace std;
int ans, n, from[1010];
int main()
{
cin>>n;
for(int i = 1;i <= n;i ++) cin>>from[i];
for(int i = 1;i <= n;i ++) {
int cnt = 1;
for(int j = from[i];j != 0;j = from[j]) cnt ++;
ans = max(ans, cnt);
}
cout << ans;
}