#include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, s, n) for (int i = (s); i < (int)(n); i++) using ll = long long; using P = pair; const long long MOD = 1e9+7; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template istream& operator>>(istream &is, vector &v) { for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i); return is; } int main() { int n; cin >> n; vector> graph(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; graph.at(a).push_back(b); graph.at(b).push_back(a); } // first 直径, second 最遠頂点 function dfs = [&](int p, int v){ P res(0, v); for (int to: graph.at(v)) if (to != p) { P r = dfs(v, to); r.first++; if (res.first < r.first) res = r; } return res; }; P x = dfs(-1, 0); P y = dfs(-1, x.second); cout << n - 1 - y.first << endl; }