Under construction

Still being built. Things may change

Tree and Query 21

Time limit5sMemory limit1024 MB

Summary
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 NN vertices numbered from 00 to N−1N-1. Each edge has a non-negative integer weight.

Process the following two kinds of queries.

  • 1 u v w x: Remove the edge between uu and vv, then add an edge with weight xx between vv and ww. An edge between uu and vv is guaranteed to exist. The graph is guaranteed to remain a tree after the operation. (0≤x≤100 0000 \le x \le 100\,000)
  • 2 k x1 x2 ... xk: For the vertices x1,x2,…,xkx_1, x_2, \ldots, x_k, consider the unique simple path between xix_i and xjx_j for every 1≤i<j≤k1 \le i < j \le k. Print the sum of the weights of all edges in the union of these simple paths. All xix_i are distinct. (1≤k≤n1 \le k \le n)

Input

The first line contains NN, the size of the tree. (2≤N≤100 0002 \le N \le 100\,000)

Each of the next N−1N-1 lines contains three integers uu, vv, and ww, meaning there is an edge of weight ww between vertices uu and vv. (0≤u,v≤N−10 \le u, v \le N-1, 0≤w≤100 0000 \le w \le 100\,000)

The next line contains QQ, the number of queries. (1≤Q≤100 0001 \le Q \le 100\,000)

The next QQ lines contain the queries described above.

The sum of kk 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.

Examples1

  1. Example 1

    Input
    7
    0 1 1
    0 2 2
    1 3 3
    1 4 4
    2 5 5
    2 6 6
    4
    1 1 4 5 7
    2 3 0 4 6
    1 0 2 1 8
    2 3 0 4 6
    
    Expected output
    20
    27