#LCA0000086. Cộng giá trị qua một phía của cạnh (Through Path)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cộng giá trị qua một phía của cạnh (Through Path)
Through Path
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
An -vertex tree has edges numbered by input order; edge joins . All vertex values start at zero. In operation , consider the two components obtained by temporarily deleting edge . Add to every vertex in the component containing when , otherwise the component containing when . All updates accumulate; no edge is permanently removed.
Input
First line: , followed by edge lines ; then and lines .
Output
Print lines, with the final value at vertex on line .
Subtasks
-
Subtask 1 (20 points): ; ; ; ; ; ; edges form a tree.
-
Subtask 2 (30 points): ; ; ; ; ; ; edges form a tree.
-
Subtask 3 (50 points): ; ; ; ; ; ; edges form a tree.
Examples
Input
5
1 2
2 3
2 4
4 5
4
1 1 1
1 4 10
2 1 100
2 2 1000
Output
11
110
1110
110
100
Explanation
Operation 1 adds to vertex ; operation 2 adds to vertices ; the final two operations add to the component of vertex and to vertex .