吴晨曦 • 13天前
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 005;
vector <int> adj[N];
int d, vis[N];
void dfs(int cur, int depth) {
d = max(d, depth);
for (auto p : adj[cur])
if (!vis[p])
dfs(p, depth + 1);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
if (x == 0 and y == 0)
continue;
adj[i].push_back(x);
adj[i].push_back(y);
}
dfs(1, 1);
cout << d << endl;
return 0;
}
评论: