Traffic Analysis
Time limit3sMemory limit1024 MB
Given a tree whose edges carry traffic volumes, find the maximum total city flow where each edge's endpoints sum to at most its volume, and recompute after each change.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, Segment tree
- Solved
- No attempts yet
Problem
The country has cities numbered 1 to . There are also roads numbered 1 to , each connecting two different cities. Any two cities can reach each other through the roads, so the road network is a tree.
Seon-jae, who has just joined Hyundai Autoever, is tasked with analyzing the traffic flow of each city and the traffic volume of each road to provide optimized traffic information. According to the analysis, the traffic flow of each city and the traffic volume of each road can be expressed as non-negative integers. Also, the traffic volume of each road is at least the sum of the traffic flows of the two cities it connects.
The traffic volume of road is currently . Seon-jae wants to compute the maximum possible total traffic flow over all cities and provide it to users. However, Hyundai Autoever, which is developing the next-generation GIS, has decided to recompute this value in real time whenever traffic volumes change.
Seon-jae could solve this alone, but he asks you, his new colleague, for help. Solve the task together with him.
Input
The first line contains the number of cities and the number of traffic volume changes , separated by a space.
The next lines are given in order. On line , there are the two city numbers and connected by road , and the current traffic volume of road , separated by spaces.
The next lines are given in order. On line , there are the road number whose traffic volume changed and the new traffic volume , separated by a space. This means the traffic volume of road has changed to .
Output
On the first line, print the maximum possible total traffic flow of all cities that can be obtained from the current traffic volumes.
On each of the next lines, print the maximum possible total traffic flow that can be obtained from the traffic volumes after each change.
Constraints
- ()
- Any two different cities can be reached from each other through the roads.
- ()
- ()
- ()
- All numbers in the input are integers.