#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; #include #include namespace nachia{ template class CsrArray{ public: struct ListRange{ using iterator = typename std::vector::iterator; iterator begi, endi; iterator begin() const { return begi; } iterator end() const { return endi; } int size() const { return (int)std::distance(begi, endi); } Elem& operator[](int i) const { return begi[i]; } }; struct ConstListRange{ 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 Elem& operator[](int i) const { return begi[i]; } }; private: int m_n; std::vector m_list; std::vector m_pos; public: CsrArray() : m_n(0), m_list(), m_pos() {} static CsrArray Construct(int n, std::vector> items){ CsrArray res; res.m_n = n; std::vector buf(n+1, 0); for(auto& [u,v] : items){ ++buf[u]; } for(int i=1; i<=n; i++) buf[i] += buf[i-1]; res.m_list.resize(buf[n]); for(int i=(int)items.size()-1; i>=0; i--){ res.m_list[--buf[items[i].first]] = std::move(items[i].second); } res.m_pos = std::move(buf); return res; } static CsrArray FromRaw(std::vector list, std::vector pos){ CsrArray res; res.m_n = pos.size() - 1; res.m_list = std::move(list); res.m_pos = std::move(pos); return res; } ListRange operator[](int u) { return ListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; } ConstListRange operator[](int u) const { return ConstListRange{ m_list.begin() + m_pos[u], m_list.begin() + m_pos[u+1] }; } int size() const { return m_n; } int fullSize() const { return (int)m_list.size(); } }; } // namespace nachia namespace nachia{ struct Graph { public: struct Edge{ int from, to; void reverse(){ std::swap(from, to); } int xorval() const { return from ^ to; } }; Graph() : m_n(0), m_e(0), m_isUndir(false) {} explicit Graph(int n, bool undirected = false, int m = 0) : m_n(n), m_e(m), m_isUndir(undirected) {} explicit Graph(int n, const std::vector>& edges, int undirected = false) : m_n(n), m_isUndir(undirected){ m_e.resize(edges.size()); for(std::size_t i=0; i static Graph Input(Cin& cin, int n, bool undirected, int m, int offset = 0){ Graph res(n, undirected, m); for(int i=0; i> u >> v; res[i].from = u - offset; res[i].to = v - offset; } return res; } int numVertices() const noexcept { return m_n; } int numEdges() const noexcept { return int(m_e.size()); } int addNode() noexcept { return m_n++; } int addEdge(int from, int to){ m_e.push_back({ from, to }); return numEdges() - 1; } Edge& operator[](int ei) noexcept { return m_e[ei]; } const Edge& operator[](int ei) const noexcept { return m_e[ei]; } Edge& at(int ei) { return m_e.at(ei); } const Edge& at(int ei) const { return m_e.at(ei); } auto begin(){ return m_e.begin(); } auto end(){ return m_e.end(); } auto begin() const { return m_e.begin(); } auto end() const { return m_e.end(); } bool isUndirected() const noexcept { return m_isUndir; } void reverseEdges() noexcept { for(auto& e : m_e) e.reverse(); } void contract(int newV, const std::vector& mapping){ assert(numVertices() == int(mapping.size())); for(int i=0; i induce(int num, const std::vector& mapping) const { int n = numVertices(); assert(n == int(mapping.size())); for(int i=0; i indexV(n), newV(num); for(int i=0; i= 0) indexV[i] = newV[mapping[i]]++; std::vector res; res.reserve(num); for(int i=0; i= 0) res[mapping[e.to]].addEdge(indexV[e.from], indexV[e.to]); return res; } CsrArray getEdgeIndexArray(bool undirected) const { std::vector> src; src.reserve(numEdges() * (undirected ? 2 : 1)); for(int i=0; i::Construct(numVertices(), src); } CsrArray getEdgeIndexArray() const { return getEdgeIndexArray(isUndirected()); } CsrArray getAdjacencyArray(bool undirected) const { std::vector> src; src.reserve(numEdges() * (undirected ? 2 : 1)); for(auto e : m_e){ src.emplace_back(e.from, e.to); if(undirected) src.emplace_back(e.to, e.from); } return CsrArray::Construct(numVertices(), src); } CsrArray getAdjacencyArray() const { return getAdjacencyArray(isUndirected()); } private: int m_n; std::vector m_e; bool m_isUndir; }; } // namespace nachia #include namespace nachia{ template class TreeDP{ private: template< class NodeInitializer, class RakeFunc, class CompressFunc, typename std::enable_if_t, void*> = nullptr, typename std::enable_if_t, void*> = nullptr, typename std::enable_if_t, void*> = nullptr > class Inner{ private: std::vector low; std::vector high; std::vector XorEdge; std::vector P; RakeFunc rake; CompressFunc compress; public: // S rake(S a, S b) // S compress(S a, int edgeIndex, int newRoot) Inner(const Graph& tree, NodeInitializer _node, RakeFunc _rake, CompressFunc _compress) : rake(std::move(_rake)) , compress(std::move(_compress)) { int n = tree.numVertices(); auto adj = tree.getEdgeIndexArray(true); std::vector node; node.reserve(n); for(int v=0; v bfs(n, 0); int bfsi = 1; P.assign(n, -1); for(int v : bfs){ for(int e : adj[v]){ int w = v ^ XorEdge[e]; if(P[v] != e){ P[w] = e; bfs[bfsi++] = w; } } } low = node; for(int i=n-1; i>=1; i--){ int w = bfs[i]; int v = w ^ XorEdge[P[w]]; low[v] = rake(low[v], compress(low[w], P[w], v), v); } high = node; for(int i=0; i=0; ci--){ int e = adj[v][ci]; if(P[v] == e) continue; int w = v ^ XorEdge[e]; high[w] = fold; fold = rake(compress(low[w], e, v), fold, v); } fold = node[v]; emp = true; for(int ci=0; ci= 0 && XorEdge[P[u]] == (u^v)) return P[u]; return P[v]; } S getAtVtx(int i){ if(i == 0) return low[i]; return rake(compress(high[i], P[i], i), low[i], i); } S getAtEdge(int root, int edge){ if(P[root] == edge) return low[root]; return high[root ^ XorEdge[edge]]; } }; public: // S node(int root) // S rake(S a, S b, int root) // S compress(S a, int edgeIndex, int newRoot) template< class NodeInitializer, class RakeFunc, class CompressFunc > static auto Solver( const Graph& tree, NodeInitializer node, RakeFunc rake, CompressFunc compress) { return Inner( tree, std::move(node), std::move(rake), std::move(compress)); } }; } // namespace nachia void testcase(){ int N; cin >> N; auto tree = nachia::Graph::Input(cin, N, true, N-1, 1); auto dp = nachia::TreeDP::Solver(tree, [](int){ return 0; }, [](int a, int b, int){ return max(a,b); }, [](int a, int, int){ return a + 1; }); auto adj = tree.getAdjacencyArray(); int ans = 0; rep(v,N){ vector Q; for(int w : adj[v]) Q.push_back(dp.getAtEdge(w,dp.edgeBetween(v,w))); sort(Q.rbegin(), Q.rend()); rep(i,Q.size()) chmax(ans, (Q[i]+1)*(i+1)+1); } cout << ans << "\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }