#include #include #include #include #include #include #include #include #include #include #include #include #define vll vector #define vvvl vector #define vvl vector> #define VV(a, b, c, d) vector>(a, vector(b, c)) #define VVV(a, b, c, d) vector(a, vvl(b, vll (c, d))); #define re(c, b) for(ll c=0;c> G(1000001, vector()); vll dist(1000001, 0); vll c(1000001, 0); vll ans(1000001, 0); void f(ll now, ll from){ for(auto e:G[now]){ if(e.to==from) continue; f(e.to, now); ans[now] += dist[e.to] + c[e.to]*e.w; dist[now] += dist[e.to] + c[e.to]*e.w; c[now] += c[e.to]; } c[now]++; } int main(int argc, char const *argv[]) { ll n;std::cin >> n; vll use(n+1, 0); re(i, n-1){ ll a, b, c=1;std::cin >> a >> b; G[a].push_back({b, c}); G[b].push_back({a, c}); use[b] = 1; } ll root = -1; for(int i=1;i<=n;i++) { if(!use[i]) { root = i; break; } } f(root, -1); ll s = 0; for(int i=1;i<=n;i++) s += ans[i]; std::cout << s << '\n'; }