Distance Sums 2

AtCoder
IDabc220_f
Time2000ms
Memory256MB
Difficulty
Given is a tree with $N$ vertices. The vertices are numbered $1,2,\ldots ,N$, and the $i$\-th edge is an undirected edge connecting Vertices $u_i$ and $v_i$. For each integer $i\,(1 \leq i \leq N)$, find $\sum_{j=1}^{N}dis(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 \leq N \leq 2 \times 10^5$ * $1 \leq u_i < v_i \leq N$ * The given graph is a tree. * All values in input are integers. ## Input Input is given from Standard Input in the following format: $N$ $u_1$ $v_1$ $u_2$ $v_2$ $\vdots$ $u_{N-1}$ $v_{N-1}$ [samples]
Samples
Input #1
3
1 2
2 3
Output #1
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$.
Input #2
2
1 2
Output #2
1
1
Input #3
6
1 6
1 5
1 3
1 4
1 2
Output #3
5
9
9
9
9
9
API Response (JSON)
{
  "problem": {
    "name": "Distance Sums 2",
    "description": {
      "content": "Given is a tree with $N$ vertices. The vertices are numbered $1,2,\\ldots ,N$, and the $i$\\-th edge is an undirected edge connecting Vertices $u_i$ and $v_i$. For each integer $i\\,(1 \\leq i \\leq N)$, f",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc220_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given is a tree with $N$ vertices. The vertices are numbered $1,2,\\ldots ,N$, and the $i$\\-th edge is an undirected edge connecting Vertices $u_i$ and $v_i$.\nFor each integer $i\\,(1 \\leq i \\leq N)$, f...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments