#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 33 can be satisfied after performing the following operations 11 and 22.

  1. 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.
  2. Second, select one or fewer edges and delete them.
  3. 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.

  1. 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.
  2. Second, select one or fewer edges and delete them.
  3. At this time, the tree is divided into at most two connected components, and only one type of stone is placed in either.
  4. The minimum value of the total cost of operation 11 required to achieve condition 33 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 NN vertices, where NN is an odd number. The ii-th edge connects two vertices, uiu_i and viv_i, and has a length wiw_i. The stone colors placed on vertices represent the string S=s1s2sNS = s_1s_2 \ldots s_N.

You sequentially perform QQ operations on this tree. The jj-th operation is defined by two integers ej,aje_j, a_j, which represents increasing the length of the eje_j-th edge by aja_j. 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.
  • 3N1053 \leq N \leq 10^5
  • NN is an odd number.
  • sis_i is either "G" or "B" and represents the stone's color at vertex ii. "G" represents green, and "B" represents blue.
  • 1ui,viN1 \leq u_i, v_i \leq N
  • 0wi1050 \leq w_i \leq 10^5
  • The given graph satisfies the condition of "Gemini Tree."
  • 1Q1051 \leq Q \leq 10^5
  • 1ejN11 \leq e_j \leq N - 1
  • 1aj1051 \leq a_j \leq 10^5

输出格式

Output the answer in QQ lines. On the jj-th line, output the value of the tree after the jj-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.