提交时间:2023-08-23 14:51:56

运行 ID: 99660

#include <bits/stdc++.h> using namespace std; int tree[10005][2]; int depth[10005]; int main() { int n, m=0; cin >> n; for (int i = 1; i <= n; i++) { cin >> tree[i][0] >> tree[i][1]; } queue<int> q; q.push(1); depth[1] = 1; while (!q.empty()) { int x = q.front(); q.pop(); m = max(m, depth[x]); if (tree[x][0] != 0) { depth[tree[x][0]] = depth[x] + 1; q.push(tree[x][0]); } if (tree[x][1] != 0) { depth[tree[x][1]] = depth[x] + 1; q.push(tree[x][1]); } } cout << m << endl; return 0; }