Gemini Tree (Ver.Jadeite)
Time limit5sMemory limit1024 MB
Given a weighted tree with green and blue stones, find the minimum swap cost to split it into single-color parts after each edge length update.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, Segment tree
- Solved
- No attempts yet
Problem
Consider a tree with a green or blue stone placed on each vertex. Such a tree is called a "Gemini Tree" if condition 3 can be satisfied after performing the following operations 1 and 2.
- First, select pairs of vertices that are directly connected by an edge and exchange the stones on their two endpoints. Repeat this zero or more times.
- Second, select at most one edge and delete it.
- Then the tree splits into at most two connected components, and each component contains only one type of stone.
Consider a Gemini Tree where each edge has a given length, and define its value as follows.
- First, select pairs of vertices that are directly connected by an edge and exchange the stones on their endpoints. Repeat this zero or more times. Each exchange costs the length of that edge.
- Second, select at most one edge and delete it.
- Then the tree splits into at most two connected components, and each component contains only one type of stone.
- The value is the minimum total cost of operation 1 needed to satisfy condition 3.
Stones are not moved when computing the value.
You are given a Gemini Tree with vertices, where is odd. The -th edge connects vertices and and has length . The stone colors form the string , where is the color of vertex .
You perform operations on this tree in order. The -th operation is given by two integers and , and increases the length of the -th edge by . The change stays in effect for all later operations. After each operation, print the value of the tree.
Input
Output
Print lines. On the -th line, print the value of the tree after the -th operation. Print a newline at the end of each line.
Constraints
- All inputs are integers.
- is odd.
- is either "G" or "B", the color of vertex . "G" means green and "B" means blue.
- The given graph is a Gemini Tree.
Hint
In the first sample, there is one green stone. Therefore, the problem is to move this stone to one of the leaves at minimal cost.