代码:
#include<vector>
using namespace std;
vector<int> e[100005];
int dep[100005], f[100005];
void dfs(int x) {
int len = e[x].size();
f[x] = dep[x];
for (int i = 0; i < len; ++i) {
dep[e[x][i]] = dep[x] + 1;
dfs(e[x][i]);
f[x] = max(f[x], f[e[x][i]]);
}
}
int main() {
int n, x, y;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> x >> y;
if (x != 0) e[i].push_back(x);
if (y != 0) e[i].push_back(y);
}
dfs(1);
cout << f[1] + 1;
return 0;
}