// VALIDATOR #include "testlib.h" #include #include #include using namespace std; // UnionFind Tree (0-indexed), based on size of each disjoint set struct UnionFind { std::vector par, cou; UnionFind(int N = 0) : par(N), cou(N, 1) { iota(par.begin(), par.end(), 0); } int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return false; if (cou[x] < cou[y]) std::swap(x, y); par[y] = x, cou[x] += cou[y]; return true; } int count(int x) { return cou[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } }; vector to[100000]; int main(int argc, char **argv) { registerValidation(argc, argv); int N = inf.readInt(2, 100000); inf.readEoln(); UnionFind uf(N); for (int i = 0; i < N - 1; i++) { int a = inf.readInt(1, N); inf.readSpace(); int b = inf.readInt(1, N); inf.readEoln(); a--, b--; uf.unite(a, b); to[a].push_back(b); to[b].push_back(a); } inf.readEof(); if (uf.count(0) != N) { // NOT tree exit(8); } long long ret = 1LL * N * N; auto f = [&N](long long x) -> long long { return x * (N - x); }; auto dfs = [&](auto self, int now, int prv) -> int { int subtree_size = 1; for (auto nxt : to[now]) { if (nxt != prv) { int tmp = self(self, nxt, now); ret += f(tmp); subtree_size += tmp; } } ret += f(subtree_size); return subtree_size; }; dfs(dfs, 0, -1); cout << ret << '\n'; }