#include /** * @title Graph template * @docs graph_template.md */ template class Edge{ public: int from,to; Cost cost; Edge() {} Edge(int to, Cost cost): to(to), cost(cost){} Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){} }; template using Graph = std::vector>>; template using Tree = std::vector>>; template void add_edge(C &g, int from, int to, T w = 1){ g[from].emplace_back(from, to, w); } template void add_undirected(C &g, int a, int b, T w = 1){ add_edge(g, a, b, w); add_edge(g, b, a, w); } /** * @title Rooting * @docs rooting.md */ template void rooting(Tree &tree, int cur, int par = -1){ if(par != -1){ for(auto it = tree[cur].begin(); it != tree[cur].end(); ++it){ if(it->to == par){ tree[cur].erase(it); break; } } } for(auto &e : tree[cur]){ rooting(tree, e.to, cur); } } /** * @title Fixed point combinator * @docs fix_point.md */ template struct FixPoint : F{ explicit constexpr FixPoint(F &&f) noexcept : F(std::forward(f)){} template constexpr auto operator()(Args &&... args) const { return F::operator()(*this, std::forward(args)...); } }; template inline constexpr auto make_fix_point(F &&f){ return FixPoint(std::forward(f)); } template inline constexpr auto make_fix_point(F &f){ return FixPoint(std::forward(f)); } int main(){ int N; while(std::cin >> N){ Tree tree(N); for(int i = 0; i < N-1; ++i){ int v, w; std::cin >> v >> w; --v, --w; add_undirected(tree, v, w, 1); } rooting(tree, 0); std::vector sub(N); auto f = make_fix_point( [&](auto &&f, int cur) -> int64_t { sub[cur] = 1; for(auto &e : tree[cur]){ sub[cur] += f(e.to); } return sub[cur]; } )(0); for(int k = 0; k < N; ++k){ int64_t ans = 0; ans += 1; ans += (sub[k] - 1) * 2; int64_t s = 0; for(auto &e : tree[k]){ s += sub[e.to]; } for(auto &e : tree[k]){ ans += sub[e.to] * (s - sub[e.to]); } std::cout << ans << "\n"; } } return 0; }