#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template void chmin(T &a,const T &b){if(a>b) a=b;} template void chmax(T &a,const T &b){if(a> g; vector sz; void szdfs(int now){ sz[now]=1; for(auto nex:g[now]){ szdfs(nex); sz[now]+=sz[nex]; } } ll ans; void dfs(int now,ll dep){ for(auto nex:g[now]){ ans+=(dep+1)*sz[nex]; dfs(nex,dep+1); } } int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; g.resize(N); vector A(N-1),B(N-1); rep(i,N-1){ cin>>A[i]>>B[i]; A[i]--;B[i]--; g[A[i]].push_back(B[i]); } vector num(N,0); rep(i,N-1) num[B[i]]++; int root=0; rep(i,N) if(num[i]==0) root=i; sz.resize(N); szdfs(root); dfs(root,0); cout<