#include using namespace std; int main(){ int N; cin >> N; vector> E(N); for (int i = 0; i < N - 1; i++){ int u, v; cin >> u >> v; u--; v--; E[u].push_back(v); E[v].push_back(u); } vector p(N, -1); vector> c(N); queue Q; vector bfs; Q.push(0); while (!Q.empty()){ int v = Q.front(); Q.pop(); bfs.push_back(v); for (int w : E[v]){ if (w != p[v]){ p[w] = v; c[v].push_back(w); Q.push(w); } } } reverse(bfs.begin(), bfs.end()); vector dp1(N, 0); vector D1(N, 0); for (int v : bfs){ vector A; for (int w : c[v]){ dp1[v] = max(dp1[v], dp1[w] + 1); D1[v] = max(D1[v], D1[w]); A.push_back(dp1[w] + 1); } sort(A.begin(), A.end(), greater()); if (A.size() == 1){ D1[v] = max(D1[v], A[0]); } if (A.size() >= 2){ D1[v] = max(D1[v], A[0] + A[1]); } } reverse(bfs.begin(), bfs.end()); vector dp2(N, 0); vector D2(N, 0); for (int v : bfs){ int cnt = c[v].size(); vector L1(cnt + 1, 0), L2(cnt + 1, 0), L(cnt + 1, 0); for (int i = 0; i < cnt; i++){ int w = c[v][i]; L1[i + 1] = max(L1[i], dp1[w] + 1); L2[i + 1] = max(L2[i], min(L1[i], dp1[w] + 1)); L[i + 1] = max(L[i], D1[w]); } vector R1(cnt + 1, 0), R2(cnt + 1, 0), R(cnt + 1, 0); for (int i = cnt - 1; i >= 0; i--){ int w = c[v][i]; R1[i] = max(R1[i + 1], dp1[w] + 1); R2[i] = max(R2[i + 1], min(R1[i + 1], dp1[w] + 1)); R[i] = max(R[i + 1], D1[w]); } for (int i = 0; i < cnt; i++){ int w = c[v][i]; dp2[w] = max(L1[i] + 1, R1[i + 1] + 1); if (v != 0){ dp2[w] = max(dp2[w], dp2[v] + 1); } D2[w] = max(L[i], R[i + 1]); D2[w] = max(D2[w], L1[i] + L2[i]); D2[w] = max(D2[w], R1[i + 1] + R2[i + 1]); D2[w] = max(D2[w], L1[i] + R1[i + 1]); if (v != 0){ D2[w] = max(D2[w], dp2[v] + L1[i]); D2[w] = max(D2[w], dp2[v] + R1[i + 1]); } } } int ans = N; for (int i = 1; i < N; i++){ ans = min(ans, max({D1[i], D2[i], (D1[i] + 1) / 2 + (D2[i] + 1) / 2 + 1})); } cout << ans << endl; }