#FT0000187. So sánh tổng trọng số hai phía của cạnh (Compare Tree Weights)
Bài tập thuộc lộ trình
Mục lục đề ⌄
So sánh tổng trọng số hai phía của cạnh (Compare Tree Weights)
Compare Tree Weights
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A tree has vertices and its edges are indexed from to in input order. Initially every vertex has weight 1. Process queries: 1 x w increases vertex 's weight by ; 2 y asks for the absolute difference between total vertex weights in the two components that would result from removing edge . A type-2 query does not actually delete the edge: future queries use the same original tree. Weight increments accumulate and answers may exceed 32-bit range.
Input
The first line contains , followed by edges U_i V_i in edge-index order. Next is , followed by commands 1 x w or 2 y.
Output
For each type-2 query print on a separate line. Type-1 commands produce no output.
Subtasks
- Subtask 1 (20 points): ; ; exactly numbered undirected edges form a tree; each command is
1 x w(, ) or2 y(); at least one type-2 query. - Subtask 2 (30 points): ; ; exactly numbered undirected edges form a tree; each command is
1 x w(, ) or2 y(); at least one type-2 query. - Subtask 3 (50 points): ; ; exactly numbered undirected edges form a tree; each command is
1 x w(, ) or2 y(); at least one type-2 query.
Examples
Example 1
Input
6
1 2
1 3
2 4
4 5
4 6
5
2 1
1 1 3
2 1
1 4 10
2 5
Output
2
1
17
Explanation
2 1: hypothetical edge removal yields component weights 2 and 4; print ; keep the edge.1 1 3: vertex 1 now weighs 4; total weight 9.2 1: hypothetical edge removal yields component weights 5 and 4; print ; keep the edge.1 4 10: vertex 4 now weighs 11; total weight 19.2 5: hypothetical edge removal yields component weights 18 and 1; print ; keep the edge.
Example 2
Input
2
1 2
5
2 1
1 1 5
2 1
1 2 3
2 1
Output
0
5
2
Explanation
2 1: hypothetical edge removal yields component weights 1 and 1; print ; keep the edge.1 1 5: vertex 1 now weighs 6; total weight 7.2 1: hypothetical edge removal yields component weights 6 and 1; print ; keep the edge.1 2 3: vertex 2 now weighs 4; total weight 10.2 1: hypothetical edge removal yields component weights 6 and 4; print ; keep the edge.