#include #include #include using namespace std; template struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template struct Graph { vector>> g; int es; Graph() = default; explicit Graph(int n) : g(n), es(0) {} size_t size() const { return g.size(); } void add_directed_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es++); } void add_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es); g[to].emplace_back(to, from, cost, es++); } void read(int M, int padding = -1, bool weighted = false, bool directed = false) { for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a += padding; b += padding; T c = T(1); if (weighted) cin >> c; if (directed) add_directed_edge(a, b, c); else add_edge(a, b, c); } } }; template struct TreeDiameter : Graph { public: using Graph::Graph; using Graph::g; vector> path; T build() { to.assign(g.size(), -1); auto p = dfs(0, -1); auto q = dfs(p.second, -1); int now = p.second; while (now != q.second) { for (auto &e : g[now]) { if (to[now] == e.to) { path.emplace_back(e); } } now = to[now]; } return q.first; } explicit TreeDiameter(const Graph &g) : Graph(g) {} private: vector to; pair dfs(int idx, int par) { pair ret(0, idx); for (auto &e : g[idx]) { if (e.to == par) continue; auto cost = dfs(e.to, idx); cost.first += e.cost; if (ret < cost) { ret = cost; to[idx] = e.to; } } return ret; } }; int main() { int n; cin >> n; TreeDiameter g(n); g.read(n - 1, -1); cout << n - 1 - g.build() << endl; return 0; }