Score : 500 points
Problem Statement
Given is a tree with N vertices. The vertices are numbered 1,2,…,N, and the i-th edge is an undirected edge connecting Vertices ui and vi.
For each integer i(1≤i≤N), find ∑j=1Ndis(i,j).
Here, dis(i,j) denotes the minimum number of edges that must be traversed to go from Vertex i to Vertex j.
Constraints
- 2≤N≤2×105
- 1≤ui<vi≤N
- The given graph is a tree.
- All values in input are integers.
Input is given from Standard Input in the following format:
N
u1 v1
u2 v2
⋮
uN−1 vN−1
Output
Print N lines.
The i-th line should contain ∑j=1Ndis(i,j).
3
1 2
2 3
3
2
3
We have:
dis(1,1)+dis(1,2)+dis(1,3)=0+1+2=3,
dis(2,1)+dis(2,2)+dis(2,3)=1+0+1=2,
dis(3,1)+dis(3,2)+dis(3,3)=2+1+0=3.
2
1 2
1
1
6
1 6
1 5
1 3
1 4
1 2
5
9
9
9
9
9