#pragma GCC optimize("Ofast") #include #include #include using namespace std; using namespace __gnu_pbds; using ll = long long; using u64 = uint_fast64_t; using pii = pair; using pll = pair; #define rep(i, n) for(int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007; //constexpr long long MOD = 998244353; template inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; } template inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; } inline int popcount(int x) {return __builtin_popcount(x);} inline int popcount(long long x) {return __builtin_popcountll(x);} void print() { cout << "\n"; } template void print(const T &x, const Args &... args) { cout << x << " "; print(args...); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct HeavyLightDecomposition { vector> G; vector vid, head, sub, par, dep, inv, type; HeavyLightDecomposition(int N) : G(N),vid(N,-1),head(N),sub(N,1), par(N,-1),dep(N,0),inv(N),type(N){} void add_edge(int u,int v) { G[u].emplace_back(v); G[v].emplace_back(u); } void build(vector rs={0}) { int c=0,pos=0; for(int r:rs) { dfs_sz(r); head[r]=r; dfs_hld(r,c++,pos); } } void dfs_sz(int v) { for(int &u:G[v]) if(u==par[v]) swap(u,G[v].back()); if(~par[v]) G[v].pop_back(); for(int &u:G[v]) { par[u]=v; dep[u]=dep[v]+1; dfs_sz(u); sub[v]+=sub[u]; if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]); } } void dfs_hld(int v,int c,int &pos) { vid[v]=pos++; inv[vid[v]]=v; type[v]=c; for(int u:G[v]){ if(u==par[v]) continue; head[u]=(u==G[v][0]?head[v]:u); dfs_hld(u,c,pos); } } int lca(int u,int v){ while(1){ if(vid[u]>vid[v]) swap(u,v); if(head[u]==head[v]) return u; v=par[head[v]]; } } int distance(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)]; } // for_each(vertex) template void for_each(int u, int v, const F& f) { while(1){ if(vid[u]>vid[v]) swap(u,v); f(max(vid[head[v]],vid[u]),vid[v]+1); if(head[u]!=head[v]) v=par[head[v]]; else break; } } template T for_each(int u,int v,T ti,const Q &q,const F &f){ T l=ti,r=ti; while(1){ if(vid[u]>vid[v]){ swap(u,v); swap(l,r); } l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1)); if(head[u]!=head[v]) v=par[head[v]]; else break; } return f(l,r); } // for_each(edge) // [l, r) <- attention!! template void for_each_edge(int u, int v,const F& f) { while(1){ if(vid[u]>vid[v]) swap(u,v); if(head[u]!=head[v]){ f(vid[head[v]],vid[v]+1); v=par[head[v]]; }else{ if(u!=v) f(vid[u]+1,vid[v]+1); break; } } } int search(int v, int d) { if (dep[v] < d || d < 0) return -1; while (1) { if (dep[v] - dep[head[v]] + 1 <= d) { d -= dep[v] - dep[head[v]] + 1; v = par[head[v]]; } else { return inv[vid[v]-d]; } } } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector> G(N); rep(i,N-1) { int u,v; cin >> u >> v; --u; --v; G[u].emplace_back(v); G[v].emplace_back(u); } vector ans(N),sub(N); auto dfs=[&](auto&& self, int v, int pv)->void { vector chi; sub[v] = 1; for (auto nv : G[v]) { if (pv==nv) continue; self(self,nv,v); chi.emplace_back(sub[nv]); sub[v] += sub[nv]; } ll S = accumulate(all(chi),0LL); ans[v] = S*S; for (auto nv : G[v]) { if (pv==nv) continue; ans[v] -= sub[nv]*sub[nv]; } ans[v] += sub[v]*2-1; }; dfs(dfs,0,-1); rep(i,N) cout << ans[i] << ln; }