#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") // (without edge property) // sub[u]: inside subtree at u, rooted at u // bus[u]: outside subtree at u, rooted at par[u] // tot[u]: rooted at u // T: monoid representing information of a subtree. // T::init() should assign the identity. // T::pull(const T &l, const T &r) should assign the product. // T::up(int u) should attach vertex u to the product of the subtrees. template struct ForestDP { int n; vector> graph; vector par; vector sub, bus, tot; ForestDP() : n(0) {} explicit ForestDP(int n_) : n(n_), graph(n_) {} void ae(int u, int v) { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); graph[u].push_back(v); graph[v].push_back(u); } void run() { par.assign(n, -2); sub.resize(n); bus.resize(n); tot.resize(n); for (int u = 0; u < n; ++u) if (par[u] == -2) { dfs0(u, -1); dfs1(u, -1); } } void dfs0(int u, int p) { par[u] = p; const int deg = graph[u].size(); int w = -1; for (int j = deg; --j >= 0; ) { const int v = graph[u][j]; if (p != v) { dfs0(v, u); if (~w) { bus[v].pull(sub[v], bus[w]); } else { bus[v] = sub[v]; } w = v; } } if (~w) { sub[u] = bus[w]; } else { sub[u].init(); } sub[u].up(u); } void dfs1(int u, int p) { const int deg = graph[u].size(); int v = -1; for (int j = 0; j < deg; ++j) { const int w = graph[u][j]; if (p != w) { if (~v) { bus[v].pull(tot[v], bus[w]); bus[v].up(u); tot[w].pull(tot[v], sub[v]); dfs1(v, u); } else { if (~p) { tot[w] = bus[u]; } else { tot[w].init(); } } v = w; } } if (~v) { bus[v] = tot[v]; bus[v].up(u); tot[u].pull(tot[v], sub[v]); dfs1(v, u); } else { if (~p) { tot[u] = bus[u]; } else { tot[u].init(); } } tot[u].up(u); } }; struct Data { int mx[2]; Data() : mx{0, 0} {} friend ostream &operator<<(ostream &os, const Data &t) { return os << "[" << t.mx[0] << ", " << t.mx[1] << "]"; } void init() { mx[0] = 0; mx[1] = 0; } void pull(const Data &a, const Data &b) { assert(this != &a); assert(this != &b); for (int s = 0; s < 2; ++s) { mx[s] = a.mx[s] + b.mx[s]; } } void up(int u) { swap(mx[0], mx[1]); mx[1] += 1; chmax(mx[1], mx[0]); } }; int N; vector A, B; int main() { for (; ~scanf("%d", &N); ) { A.resize(N - 1); B.resize(N - 1); for (int i = 0; i < N - 1; ++i) { scanf("%d%d", &A[i], &B[i]); --A[i]; --B[i]; } ForestDP f(N); for (int i = 0; i < N - 1; ++i) { f.ae(A[i], B[i]); } f.run(); // cerr<<"sub = "<