#abc355f. F - MST Query

    ID: 4185 传统题 1000ms 256MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>来源atcoder高级数据结构并查集树结构最小生成树

F - MST Query

Score : 550550 points

问题陈述

给定一个权重无向连通图 GG,包含 NN 个顶点和 N1N-1 条边,顶点编号从 11NN,边编号从 11N1N-1。边 ii 连接顶点 aia_ibib_i,权重为 cic_i

你需要依次处理 QQ 个查询。第 ii 个查询描述如下:

  • 给定整数 ui,vi,wiu_i, v_i, w_i。在 GG 中添加一条连接顶点 uiu_iviv_i 的边,权重为 wiw_i。然后,打印 GG 的最小生成树中边的权重之和。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a weighted undirected connected graph GG with NN vertices and N1N-1 edges, where vertices are numbered 11 to NN and edges are numbered 11 to N1N-1. Edge ii connects vertices aia_i and bib_i with a weight of cic_i.

You are given QQ queries to process sequentially. The ii-th query is described as follows:

  • You are given integers ui,vi,wiu_i, v_i, w_i. Add an edge with weight wiw_i between vertices uiu_i and viv_i in GG. Then, print the sum of the weights of the edges in a minimum spanning tree of GG.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 1ai<biN1 \leq a_i < b_i \leq N
  • 1ui<viN1 \leq u_i < v_i \leq N
  • 1ci,wi101 \leq c_i, w_i \leq 10
  • The graph is connected before processing the queries.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN QQ

a1a_1 b1b_1 c1c_1

\vdots

aN1a_{N-1} bN1b_{N-1} cN1c_{N-1}

u1u_1 v1v_1 w1w_1

\vdots

uQu_Q vQv_Q wQw_Q

Output

Print QQ lines. The ii-th line should contain the answer to the ii-th query.

Sample Input 1

4 4
1 2 6
2 3 5
2 4 4
1 3 3
1 2 3
1 4 10
3 4 1

Sample Output 1

12
10
10
7

Here is the graph after adding the edge for each query. The edges included in the minimum spanning tree are colored red.

Sample Input 2

8 6
1 8 8
1 6 10
1 5 8
2 6 6
6 7 6
1 3 9
2 4 7
1 3 4
1 6 7
3 4 6
1 5 1
7 8 4
3 5 3

Sample Output 2

49
46
45
38
34
33