问题描述
思路
这一题可以用并查集来写,将整个图看作若干个树,找出树的深度的最大值
AC代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
typedef long long LL;
#define IOS ios::sync_with_stdio(false)
#define CIN cin.tie(0)
#define COUT cout.tie(0)
using namespace std;
const int N = 2010;
int n;
int fa[N];
int deep[N];
int getdeep(int u) {
if (deep[u] > 0) return deep[u];
if (fa[u] == -1) return 1;
deep[u] = getdeep(fa[u]) + 1;
return deep[u];
}
int main() {
IOS;
CIN;
COUT;
int res = 0;
memset(deep, -1, sizeof deep);
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> fa[i];
for (int i = 1; i <= n; i ++)
if (res < getdeep(i))
res = getdeep(i);
cout << res << endl;
return 0;
}