#include #include #include #include int main() { int N; std::cin >> N; std::vector> graph(N); std::vector dist(N, 0); while (--N) { int u, v; std::cin >> u >> v; u--, v--; graph[u].emplace_back(v); graph[v].emplace_back(u); } { auto dist_dfs = [&](const auto &dist_dfs, const int &u, const int &parent) -> void { for (int &v : graph[u]) { if (v == parent) continue; dist_dfs(dist_dfs, v, u); dist[u] = std::max(dist[u], dist[v] + 1); } }; dist_dfs(dist_dfs, 0, -1); } auto solve = [&](const auto &solve, const int &u, const int &d_parent, const int &parent) -> int { std::vector> branches{{0, -1}}; for (int &v : graph[u]) { if (v == parent) branches.emplace_back(d_parent + 1, v); else branches.emplace_back(dist[v] + 1, v); } std::sort(branches.rbegin(), branches.rend()); int result = 0; for (int i = 0; i < branches.size(); i++) { result = std::max(result, (i + 1) * branches[i].first + 1); } for (int &v : graph[u]) { if (v == parent) continue; if (branches[0].second == v) result = std::max(result, solve(solve, v, branches[1].first, u)); else result = std::max(result, solve(solve, v, branches[0].first, u)); } return result; }; std::cout << solve(solve, 0, 0, -1) << std::endl; }