#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int Nmax = 1e5+5; long long ans = 0; struct edge { int x, y; bool operator < (const edge &other) const { return x+y < other.x + other.y; } } a[Nmax]; int pos[Nmax], i, n, X, Y, b[Nmax]; vector<int> v[Nmax]; void combine(int x, int y, int cost) { if(v[x].size() > v[y].size()) swap(x, y); int i = 0, j, nr = 0; for(j=v[x].size()-1; j>=0; --j) { nr = v[x][j]; while(i<v[y].size() && v[y][i] + nr < cost) ++i; ans -= i; } i = j = 0; nr = 0; while(i<v[x].size() && j<v[y].size()) { if(v[x][i] < v[y][j]) b[++nr] = v[x][i++]; else b[++nr] = v[y][j++]; } while(i<v[x].size()) b[++nr] = v[x][i++]; while(j<v[y].size()) b[++nr] = v[y][j++]; v[y].clear(); v[x].clear(); for(i=1; i<=nr; ++i) pos[b[i]] = y, v[y].push_back(b[i]); } int main() { // freopen("input", "r", stdin); // freopen("output", "w", stdout); scanf("%d", &n); for(i=1; i<n; ++i) scanf("%d%d", &a[i].x, &a[i].y); sort(a+1, a+n); for(i=1; i<=n; ++i) v[i].push_back(pos[i] = i); ans = 1LL * n * (n-1) / 2; for(i=1; i<n; ++i) { X = a[i].x; Y = a[i].y; combine(pos[X], pos[Y], X + Y); } printf("%lld\n", ans); return 0; }