#include #if __has_include() #endif using namespace std; #define eb emplace_back #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define RDVV(T,n,...) vec__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);vin(__VA_ARGS__) #define VV(n,...) RDVV(ll,n,__VA_ARGS__) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range(0,__VA_ARGS__);i(1,__VA_ARGS__);i>=i##stop;i+=i##step) #define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a) #define defpp templatevoid pp(const auto&...a){[[maybe_unused]]const char*c="";((o<(a...);} #define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{ namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};} templateconstexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};} void lin(auto&...a){(cin>>...>>a);} void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);} void dec(auto&...a){((--a),...);} templateusing pack_back_t=tuple_element_t>; } namespace my{ templateconstexpr int depth=0; templatestruct core_t_helper{using type=T;}; templateusing core_t=core_t_helper::type; templatestruct vec; templatestruct hvec_helper{using type=vec::type>;}; templatestruct hvec_helper<0,T>{using type=T;}; templateusing hvec=hvec_helper::type; templatestruct vec:vector{ static constexpr int D=depth+1; using C=core_t; using vector::vector; void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else{ }} static auto make(ll n,const auto&...a){ if constexpr(sizeof...(a)==1)return vec(n,array{a...}[0]); else { } } vec&operator--(){fe(*this,e)--e;return*this;} ll size()const{return vector::size();} auto&emplace_back(auto&&...a){vector::emplace_back(std::forward(a)...);return*this;} }; templaterequires(sizeof...(A)>=2)vec(const A&...a)->vec>>; } namespace my{ templatestruct edge_with_rev_index{ int from_,to_; WT wt_; int rev_idx_; edge_with_rev_index()=default; edge_with_rev_index(int from,int to,WT wt,int rev_idx):from_(from),to_(to),wt_(wt),rev_idx_(rev_idx){} }; } namespace my{ template&),S finalize(S,ll),S e()>class RerootingDPTree{ public: ll n_; vec>>edges_; vec>dp_; vecsum_; S operator[](ll i)const{return sum_[i];} RerootingDPTree(ll n):n_(n),edges_(n),dp_(n),sum_(n){} void add_edges(const vec&a,const vec&b){ fo(i,a.size()){ edges_[a[i]].eb(a[i],b[i],1,edges_[b[i]].size()); edges_[b[i]].eb(b[i],a[i],1,edges_[a[i]].size()-1); } } void build(){ auto dfs=[&](auto&&dfs,ll u,ll p)->S{ ll deg=edges_[u].size(); S cum=e(); dp_[u].resize(deg); fo(i,deg){ ll v=edges_[u][i].to_; if(v==p)continue; dp_[u][i]=connect(dfs(dfs,v,u),edges_[u][i]); cum=op(cum,dp_[u][i]); } return finalize(cum,u); }; dfs(dfs,0,-1); auto dfs2=[&](auto&&dfs2,ll u,ll p)->void{ ll deg=edges_[u].size(); vec dp_left(deg+1,e()),dp_right(deg,e()); fo(i,deg)dp_left[i+1]=op(dp_left[i],dp_[u][i]); of(i,deg,1)dp_right[i-1]=op(dp_[u][i],dp_right[i]); sum_[u]=finalize(dp_left[deg],u); fo(i,deg){ ll v=edges_[u][i].to_; if(v==p)continue; ll j=edges_[u][i].rev_idx_; dp_[v][j]=connect(finalize(op(dp_left[i],dp_right[i]),u),edges_[v][j]); dfs2(dfs2,v,u); } }; dfs2(dfs2,0,-1); } }; templateusing RerootingDPTreeMonoidAlias=RerootingDPTree; } namespace my{entry void main(){ LL(N); VV(N-1,a,b);dec(a,b); struct distance_sum_node{ using S=ll; static S op(S a,S b){return a+b;}; static S connect(S x,const edge_with_rev_index&e){return x+(e.from_>e.to_);}; static S finalize(S x,ll u){return x;}; static S e(){return 0;} }; RerootingDPTreeMonoidAliasrt(N); rt.add_edges(a,b); rt.build(); fo(i,N)pp(rt[i]); }}