HLD
Time limit5sMemory limit1024 MB
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 vertices. The root is vertex . 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 , which is initially empty. Assign each edge a heavy or light label according to , while satisfying the condition above. After each update to , find an assignment that minimizes the total number of light edges over all paths in .
updates are given in total. Each query consists of three integers , , and , which insert copies of the simple path from to into . After each update, print the minimum total number of light edges over all paths in .
Input
The first line contains two space-separated integers and .
The -th of the following lines contains two space-separated integers and , meaning that the -th edge connects vertices and .
The -th of the following lines contains three space-separated integers , , and , describing the -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 lines in total.
Constraints
- ,
- The given edges form a tree.
- ,
- All values in the input are integers.