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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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 vertices with unit-length edges. Let be the number of edges on their unique connecting path.
For each vertex , compute the sum of over all vertices , including itself (where ).
Input
- The first line contains .
- The next lines each contain one edge .
Output
Print integers; the -th integer is the total distance from vertex .
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): .
Examples
Input
5
1 2
1 3
3 4
3 5
Output
6 9 5 8 8
Explanation
From vertex , the distances to vertices are , totaling . From vertex they total .