提交时间:2024-01-22 17:22:17

运行 ID: 123142

#include<bits/stdc++.h> using namespace std; int const maxn = 100005; struct S { int lef, rig; } tree[maxn]; int siz[maxn], dep[maxn], mid[maxn], bak[maxn], fro[maxn], pos[maxn]; vector<int> lev[maxn]; int maxx = 0; void dfs(int u, int f){ dep[u] = dep[f] + 1; lev[dep[u]].push_back(u); siz[u] = 1; if (tree[u].lef){ dfs(tree[u].lef, u); siz[u] = siz[tree[u].lef] + siz[u]; } if (tree[u].rig){ dfs(tree[u].rig, u); siz[u] = siz[tree[u].rig] + siz[u]; } maxx = max(maxx, dep[u]); } int main (){ int n; cin >> n; for(int i = 1; i <= n; i++){ int l, r; cin >> l >> r; tree[i].lef = l; tree[i].rig = r; } dfs(1, 0); cout << maxx; return 0; }