Under construction

Still being built. Things may change

HLD

Time limit5sMemory limit1024 MB

Summary
After each update adds k copies of a tree path, print the fewest light edges achievable when each vertex keeps at most one heavy child edge.
Level

Hard9 of 10

Topics
Tree, Dynamic programming, Greedy
Solved
No attempts yet

Problem

You are given a rooted tree with NN vertices. The root is vertex 11. Each edge is either a heavy edge or a light edge. For each vertex, at most one of the edges to its children can be a heavy edge. This is the condition that a heavy-light decomposition of the tree must satisfy.

There is a multiset of simple paths TT, which is initially empty. Assign each edge a heavy or light label according to TT, while satisfying the condition above. After each update to TT, find an assignment that minimizes the total number of light edges over all paths in TT.

QQ updates are given in total. Each query consists of three integers ss, ee, and kk, which insert kk copies of the simple path from ss to ee into TT. After each update, print the minimum total number of light edges over all paths in TT.

Input

The first line contains two space-separated integers NN and QQ.

The ii-th of the following N−1N-1 lines contains two space-separated integers xix_i and yiy_i, meaning that the ii-th edge connects vertices xix_i and yiy_i.

The ii-th of the following QQ lines contains three space-separated integers ss, ee, and kk, describing the ii-th update. Updates are processed in input order, and the effect of each update accumulates.

Output

Print the answer after each update on its own line, for QQ lines in total.

Constraints

  • 2≤N≤100 0002 \le N \le 100\,000
  • 1≤Q≤100 0001 \le Q \le 100\,000
  • 1≤xi,yi≤N1 \le x_i, y_i \le N, xi≠yix_i \neq y_i (1≤i≤N−1)(1 \le i \le N-1)
  • The given edges form a tree.
  • 1≤s,e≤N1 \le s, e \le N, s≠es \neq e
  • 1≤k≤1091 \le k \le 10^9
  • All values in the input are integers.

Examples5

  1. Example 1

    Input
    3 3
    1 2
    3 1
    1 3 2
    1 3 3
    1 2 2
    
    Expected output
    0
    0
    2
    
  2. Example 2

    Input
    5 5
    3 4
    2 4
    1 2
    5 3
    5 4 2
    1 2 4
    3 4 1
    5 3 4
    1 2 2
    
    Expected output
    0
    0
    0
    0
    0
    
  3. Example 3

    Input
    8 8
    4 6
    8 4
    1 6
    5 1
    2 1
    3 2
    7 3
    2 7 1
    8 2 1
    5 3 6
    8 3 5
    1 4 2
    6 7 1
    5 6 4
    6 2 3
    
    Expected output
    0
    1
    7
    12
    14
    15
    23
    26
    
  4. Example 4

    Input
    8 10
    8 2
    7 8
    1 2
    4 2
    5 1
    6 2
    3 7
    4 5 4
    7 2 5
    5 8 7
    1 3 7
    3 2 3
    6 8 4
    7 3 4
    6 7 6
    4 8 3
    1 3 2
    
    Expected output
    4
    8
    15
    15
    15
    19
    19
    25
    28
    28
    
  5. Example 5

    Input
    10 10
    9 7
    1 7
    5 7
    3 9
    10 3
    8 1
    4 7
    2 8
    6 9
    4 9 9
    9 3 2
    1 9 8
    5 4 6
    1 5 2
    3 5 2
    5 6 1
    3 5 6
    2 8 5
    2 1 5
    
    Expected output
    9
    9
    9
    21
    23
    25
    27
    33
    33
    38