#P15709. [JAG 2023 Summer Camp #2] Gemini Tree (Ver.Jadeite)
[JAG 2023 Summer Camp #2] Gemini Tree (Ver.Jadeite)
题目描述
Consider a tree with a green or blue stone placed at each vertex. Such a tree is called a "Gemini Tree" if condition can be satisfied after performing the following operations and .
- First, operate "selecting pairs of vertices that are directly connected by edges and exchanging the stones placed on each endpoint," any number of times from zero to more.
- Second, select one or fewer edges and delete them.
- At this time, the tree is divided into at most two connected components, and only one type of stone is placed in either.
Consider a "Gemini tree" with a specified length for each edge, and define its value as follows.
- First, operate "selecting pairs of vertices that are directly connected by edges and exchanging the stones placed on each endpoint" any number of times from zero to more. Each exchange operation costs equal to the length of the edge.
- Second, select one or fewer edges and delete them.
- At this time, the tree is divided into at most two connected components, and only one type of stone is placed in either.
- The minimum value of the total cost of operation required to achieve condition is the value of "Gemini Tree."
Note that stones are not moved when calculating the value.
You are given a "Gemini tree" with a specified length for each edge. It has vertices, where is an odd number. The -th edge connects two vertices, and , and has a length . The stone colors placed on vertices represent the string .
You sequentially perform operations on this tree. The -th operation is defined by two integers , which represents increasing the length of the -th edge by . The effect of the operation remains even in subsequent operations. Answer the value of the tree after each operation.
输入格式
$$\begin{aligned} & N \\ & s_1s_2 \ldots s_N \\ & u_1 \ v_1 \ w_1 \\ & \vdots \\ & u_{N-1} \ v_{N-1} \ w_{N-1} \\ & Q \\ & e_1 \ a_1 \\ & \vdots \\ & e_Q \ a_Q \end{aligned} $$The input satisfies the following constraints:
- All inputs consist of integers.
- is an odd number.
- is either "G" or "B" and represents the stone's color at vertex . "G" represents green, and "B" represents blue.
- The given graph satisfies the condition of "Gemini Tree."
输出格式
Output the answer in lines. On the -th line, output the value of the tree after the -th operation. Add a new line at the end of each line.
5
GBBBB
1 2 0
1 3 0
2 4 0
2 5 0
5
1 1
2 3
3 3
4 10
2 2
0
1
1
3
4
5
BGBGB
1 2 0
1 3 0
2 4 0
2 5 1000
4
4 1
3 1
1 1
2 1
0
1
3
4
7
GGGBBBB
1 5 1
2 5 1
7 5 0
7 6 0
3 6 0
4 6 0
6
5 1
5 1
5 1
6 3
3 3
5 100000
1
2
2
3
6
11
提示
In Sample Input 1, there is one green stone. Therefore, the problem is to move this to one of the leaves at a minimal cost.