#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp" #include template struct Edge { int src, dst; Cost cost; Edge() = default; Edge(int src, int dst, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return cost < e.cost; } bool operator>(const Edge& e) const { return cost > e.cost; } }; template struct Graph : public std::vector>> { using std::vector>>::vector; void span(bool direct, int src, int dst, Cost cost = 1) { (*this)[src].emplace_back(src, dst, cost); if (!direct) (*this)[dst].emplace_back(dst, src, cost); } }; #line 2 "main.cpp" #include #line 4 "main.cpp" using namespace std; using lint = long long; void solve() { int n; cin >> n; Graph<> graph(n); for (int i = n - 1; i--;) { int u, v; cin >> u >> v; graph.span(false, --u, --v); } lint ans = 0; auto dfs = [&](auto&& f, int v, int p) -> lint { vector cz; lint sz = 1; for (auto e : graph[v]) { int u = e.dst; if (u == p) continue; lint c = f(f, u, v); cz.push_back(c); sz += c; } cz.push_back(n - sz); ans += n; for (auto c : cz) ans += c * (n - c); return sz; }; dfs(dfs, 0, -1); cout << ans << "\n"; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }