Tree and Query 21
Time limit5sMemory limit1024 MB
Maintain a weighted tree under edge cut-and-link operations and report the total weight of the union of paths between each given vertex set.
- Level
Hard9 of 10
- Topics
- Tree, DFS, Implementation
- Solved
- No attempts yet
Problem
The tree has vertices numbered from to . Each edge has a non-negative integer weight.
Process the following two kinds of queries.
1 u v w x: Remove the edge between and , then add an edge with weight between and . An edge between and is guaranteed to exist. The graph is guaranteed to remain a tree after the operation. ()2 k x1 x2 ... xk: For the vertices , consider the unique simple path between and for every . Print the sum of the weights of all edges in the union of these simple paths. All are distinct. ()
Input
The first line contains , the size of the tree. ()
Each of the next lines contains three integers , , and , meaning there is an edge of weight between vertices and . (, )
The next line contains , the number of queries. ()
The next lines contain the queries described above.
The sum of over all type 2 queries is between 1 and 100,000, inclusive.
Output
For each type 2 query, print the answer on its own line, in the order the queries appear.