#include #include #include #include #include #include #include namespace nachia{ struct AdjacencyList{ public: struct AdjacencyListRange{ using iterator = typename std::vector::const_iterator; iterator begi, endi; iterator begin() const { return begi; } iterator end() const { return endi; } int size() const { return (int)std::distance(begi, endi); } const int& operator[](int i) const { return begi[i]; } }; private: int mn; std::vector E; std::vector I; public: AdjacencyList(int n, std::vector> edges, bool rev){ mn = n; std::vector buf(n+1, 0); for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; } for(int i=1; i<=n; i++) buf[i] += buf[i-1]; E.resize(buf[n]); for(int i=(int)edges.size()-1; i>=0; i--){ auto [u,v] = edges[i]; E[--buf[u]] = v; if(rev) E[--buf[v]] = u; } I = std::move(buf); } AdjacencyList(const std::vector>& edges = {}){ int n = mn = edges.size(); std::vector buf(n+1, 0); for(int i=0; i targets, std::vector bounds){ AdjacencyList res; res.mn = bounds.size() - 1; res.E = std::move(targets); res.I = std::move(bounds); return res; } AdjacencyListRange operator[](int u) const { return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] }; } int num_vertices() const { return mn; } int size() const { return num_vertices(); } int num_edges() const { return E.size(); } AdjacencyList reversed_edges() const { AdjacencyList res; int n = res.mn = mn; std::vector buf(n+1, 0); for(int v : E) ++buf[v]; for(int i=1; i<=n; i++) buf[i] += buf[i-1]; res.E.resize(buf[n]); for(int u=0; u::const_iterator; iterator begi, endi; iterator begin() const { return begi; } iterator end() const { return endi; } int size() const { return (int)std::distance(begi, endi); } const Edge& operator[](int i) const { return begi[i]; } }; private: int mn; std::vector E; std::vector I; public: AdjacencyListEdgeIndexed(int n, const std::vector>& edges, bool rev){ mn = n; std::vector buf(n+1, 0); for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; } for(int i=1; i<=n; i++) buf[i] += buf[i-1]; E.resize(buf[n]); for(int i=(int)edges.size()-1; i>=0; i--){ auto [u,v] = edges[i]; E[--buf[u]] = { v, i }; if(rev) E[--buf[v]] = { u, i }; } I = std::move(buf); } AdjacencyListEdgeIndexed() : AdjacencyListEdgeIndexed(0, {}, false) {} AdjacencyListRange operator[](int u) const { return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] }; } int num_vertices() const { return mn; } int size() const { return num_vertices(); } int num_edges() const { return E.size(); } AdjacencyListEdgeIndexed reversed_edges() const { AdjacencyListEdgeIndexed res; int n = res.mn = mn; std::vector buf(n+1, 0); for(auto [v,i] : E) ++buf[v]; for(int i=1; i<=n; i++) buf[i] += buf[i-1]; res.E.resize(buf[n]); for(int u=0; u; int main(){ int N; cin >> N; vector> edges(N-1); rep(i,N-1){ int u,v; cin >> u >> v; u--; v--; edges[i] = make_pair(u,v); } auto adj = nachia::AdjacencyList(N, edges, true); vector bfs = {0}; vector P(N, -1); rep(i,N){ int p = bfs[i]; for(int e : adj[p]) if(P[p] != e){ P[e] = p; bfs.push_back(e); } } vector heightL(N, 0); vector diamL(N, 0); for(int i=N-1; i>=0; i--){ int p = bfs[i]; if(i != 0){ diamL[P[p]] = max(diamL[P[p]], heightL[P[p]] + heightL[p] + 1); diamL[P[p]] = max(diamL[P[p]], diamL[p]); heightL[P[p]] = max(heightL[P[p]], heightL[p] + 1); } } vector heightH(N, -1); vector diamH(N, 0); for(int i=0; i=0; ci--) if(P[p] != adj[p][ci]){ int c = adj[p][ci]; diamH[c] = max(diamH[c], heightH[c] + cheight + 1); diamH[c] = max(diamH[c], cdiam); cdiam = max(cdiam, diamL[c]); cdiam = max(cdiam, cheight + heightL[c] + 2); heightH[c] = max(heightH[c], cheight + 1); cheight = max(cheight, heightL[c]); } cheight = -1; cdiam = 0; for(int ci=0; ci