fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef long double ld;
  6. typedef vector<int> vi;
  7. typedef vector<ll> vl;
  8. typedef pair<int, int>pi;
  9. typedef pair<ll, ll>pl;
  10. typedef vector<pi>vpi;
  11. typedef vector<pl>vpl;
  12. typedef vector<vi> vvi;
  13. typedef vector<vl> vvl;
  14. typedef vector<string> vs;
  15. typedef vector<bool> vb;
  16. const long double PI = acos(-1);
  17. const ll oo = LLONG_MAX;
  18. const int MOD = 1e9 + 7;
  19. const int N = 2e3 + 7;
  20. #define all(v) (v).begin(),(v).end()
  21. #define rall(v) (v).rbegin(),(v).rend()
  22. #define read(v) for (auto& it : v) scanf("%d", &it);
  23. #define readl(v) for (auto& it : v) scanf("%lld", &it);
  24. #define print(v) for (auto it : v) printf("%d ", it); puts("");
  25. #define printl(v) for (auto it : v) printf("%lld ", it); puts("");
  26. int n, ans = 0;
  27. vector<vector<int>>g(N);
  28. void DFS(int u, int dep) {
  29. ans = max(ans, dep);
  30. for (auto& v : g[u])
  31. DFS(v, dep + 1);
  32. }
  33. void init() {
  34. ans = 0;
  35. for (int i = 0; i <= n; i++)
  36. g[i].clear();
  37. }
  38. void solve() {
  39. scanf("%d", &n);
  40. init();
  41. vector<int>p(n);
  42. for (int i = 0; i < n; i++) {
  43. scanf("%d", &p[i]);
  44. if (p[i] != -1) {
  45. p[i]--;
  46. g[p[i]].push_back(i);
  47. }
  48. }
  49. for (int i = 0; i < n; i++)
  50. if (p[i] == -1)
  51. DFS(i, 0);
  52. printf("%d\n", ans + 1);
  53. }
  54. int t = 1;
  55. int main() {
  56. #ifndef ONLINE_JUDGE
  57. freopen("input.txt", "r", stdin);
  58. #endif
  59. //scanf("%d", &t);
  60. while (t--) solve();
  61. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
1