#P4338. [ZJOI2018] 历史
[ZJOI2018] 历史
Description
There are cities in this world, connected by exactly bidirectional roads, so any two cities are mutually reachable. City lies at the center of the world; whoever takes this city dominates the world.
Initially, none of the cities is controlled by any country. As society develops, some cities will rise to form countries and seize world hegemony. For convenience, we label the country that rises at city as country . During the rise at city , country will take control of all cities on the path from city to city .
The rise of a new city often means war and death. If, during the rise of country , it needs to take a city that is currently controlled by another country (), then country must declare war on country and fight.
Now, Kelian knows that in history, city rose exactly times. However, the relative order of these events is unknown. The only information is that while one city is rising to dominate the world, no new city will start rising.
War is catastrophic for the people. Kelian defines the disaster degree of a single rise as the number of distinct countries it goes to war with during that rise (multiple wars against the same country within one rise count only once). She wants to know, among all possible rise orders, what is the maximum possible sum of the disaster degrees.
Meanwhile, thanks to archaeologists’ efforts, more and more historical records are being unearthed. Based on these new materials, Kelian will adjust the values of . Specifically, she will perform operations on , each time adding to . She hopes that after each modification, the maximum total disaster degree can be computed.
However, Kelian is not interested in complicated calculations, so she wants you to help her compute these values.
Additional notes:
- Multiple rises at the same city form the same country. This means that if the same city rises twice in a row, it will not go to war with any country, because those cities are already under its control.
- Over the course of history, country may temporarily control no city. This does not mean country is destroyed. When city rises, country will still take control of all cities on the path from to .
Input Format
The first line contains two integers , , the number of cities and the number of operations.
The second line contains integers, the initial values of .
The next lines each contain two integers , () describing a road.
The next lines each contain two integers , , meaning add to .
Output Format
Output lines. The first line is the answer for the initial . Each of the next lines is the answer after that modification.
5 3
1 1 1 1 1
1 2
1 3
2 4
2 5
2 1
3 1
4 1
6
7
9
10
Hint
Before the modifications, if the cities rise in the order , then they will go to war with countries, respectively.
A total of pairs of hostile relationships will be produced. It can be proven that this is the maximum among all rise orders.

Translated by ChatGPT 5
京公网安备 11011102002149号