Problems > Total Depth

Total Depth

Difficulty: 1300 4000 ms 128 MB

trees

Rooted at node 1, print the sum of the depths of all nodes (the root has depth 0).

Input: the first line has N; each of the next N−1 lines has an edge u v.

Output: the sum of all node depths.

Constraints:

  • 1 ≤ N ≤ 105

Sample Tests

Input 1
4
1 2
2 3
2 4
Output 1
5
Input 2
1
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.