#include //#include using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } struct ReRooting { struct Edge { int to; Edge(int _to) :to(_to) {} }; struct Data { int diameter; //直径 int rootmax1st; //根からのmax int rootmax2nd; //根からの2ndmax Data() :diameter(0), rootmax1st(0), rootmax2nd(0) {} Data(int _diameter, int _rootmax1st, int _rootmax2nd) :diameter(_diameter), rootmax1st(_rootmax1st), rootmax2nd(_rootmax2nd) {} }; vector>g; vectordp; int ans; int sz; ReRooting(int _sz) :sz(_sz) { g.resize(sz); dp.reserve(sz); } void ReadGraph() { rep(i, sz - 1) { int a, b; cin >> a >> b; a--; b--; g[a].push_back({ b }); g[b].push_back({ a }); } } Data merge(const Data &lh, const Data &rh) { Data ret = lh; chmax(ret.diameter, rh.diameter); chmax(ret.diameter, rh.rootmax1st + 1); if (rh.rootmax1st + 1 >= ret.rootmax1st) { ret.rootmax2nd = ret.rootmax1st; ret.rootmax1st = rh.rootmax1st + 1; } else if (rh.rootmax1st + 1 >= ret.rootmax2nd) { ret.rootmax2nd = rh.rootmax1st + 1; } chmax(ret.diameter, ret.rootmax1st + ret.rootmax2nd); return ret; } Data mergeSub(const Data &lh, const Data &rh) { Data ret; ret.diameter = max(lh.diameter, rh.diameter); chmax(ret.diameter, lh.rootmax1st + rh.rootmax1st); vectorv = { lh.rootmax1st,lh.rootmax2nd,rh.rootmax1st,rh.rootmax2nd }; sort(allR(v)); ret.rootmax1st = v[0]; ret.rootmax2nd = v[1]; return ret; } void dfs(int v, int p = -1) { Data val; for (auto e : g[v]) { if (p == e.to)continue; dfs(e.to, v); val = merge(val, dp[e.to]); } dp[v] = val; } void dfsR(int v, int p = -1) { if (-1 != p) { Data data = dp[p]; int anssub = 0; chmax(anssub, data.diameter); chmax(anssub, dp[v].diameter); chmax(anssub, 1 + (dp[v].diameter + 1) / 2 + (data.diameter + 1) / 2); chmin(ans, anssub); } vectorvdata; for (auto e : g[v]) vdata.push_back(dp[e.to]); int sz = vdata.size(); vectorsumL(sz + 1), sumR(sz + 1); rep(i, sz)sumL[i + 1] = merge(sumL[i], vdata[i]); rep(i, sz)sumR[i + 1] = merge(sumR[i], vdata[sz - i - 1]); for (int i = 0; i < g[v].size(); ++i) { auto e = g[v][i]; if (p == e.to)continue; dp[v] = mergeSub(sumL[i], sumR[sz - (i + 1)]); dfsR(e.to, v); } } void output() { cout << ans << endl; } void solve() { ans = INF; ReadGraph(); dfs(0); dfsR(0); output(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; ReRooting reroot(n); reroot.solve(); return 0; }