PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000005. Tổng khoảng cách từ mỗi đỉnh (Tree Distances II)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng khoảng cách từ mỗi đỉnh (Tree Distances II)

Tree Distances II

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree has nn vertices with unit-length edges. Let d(u,v)d(u,v) be the number of edges on their unique connecting path.

For each vertex uu, compute the sum of d(u,v)d(u,v) over all vertices vv, including uu itself (where d(u,u)=0d(u,u)=0).

Input

  • The first line contains nn.
  • The next n−1n-1 lines each contain one edge ai,bia_i,b_i.

Output

Print nn integers; the uu-th integer is the total distance from vertex uu.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; 1≤n≤2⋅1051\le n\le2\cdot10^5.
  • Subtask 2 (30 points): n≤1000n\le1000; 1≤n≤2⋅1051\le n\le2\cdot10^5.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5.

Examples

Input

5
1 2
1 3
3 4
3 5

Output

6 9 5 8 8

Explanation

From vertex 11, the distances to vertices 1,2,3,4,51,2,3,4,5 are 0,1,1,2,20,1,1,2,2, totaling 66. From vertex 22 they total 99.