#P16957. [SCCPC 2026] 那一年的秘密基地
[SCCPC 2026] 那一年的秘密基地
Problem Description
That summer, everyone made a promise under the shade of the trees in the town: they must find the most suitable secret base.
There are locations in the town, connected by paths. Between any two locations, there is a unique simple path. Therefore, these locations and paths form an unrooted tree, with locations numbered from to .
Every day, everyone visits all locations in the town one by one following an order. This order is represented by a permutation of length , where is the -th visited location.
If we choose location as the secret base, we can root the whole tree at . Then, for two different locations , if lies on the simple path from to , we say is an ancestor of .
Everyone believes that if a location is visited earlier, but one of its ancestors is visited later, then an “exposure risk” occurs. Formally, for a secret base , define the danger level as the number of pairs satisfying: , and when rooted at , is an ancestor of .
In other words, is the number of pairs of locations such that, in the current visiting order, the later location is an ancestor of the earlier location.
However, as time goes by, plans may change. Next, there are operations. Each operation gives an integer , meaning we swap the two adjacent locations and in the visiting order.
Before all operations, and after each operation, you need to choose the most suitable secret base again to make the danger level as small as possible, and output this minimum danger level.
Input Format
The first line contains two integers (), representing the number of locations and the number of operations.
The second line contains integers (), representing the initial visiting order. It is guaranteed that is a permutation.
The next lines each contain two integers (), indicating there is a path between location and location . It is guaranteed that these paths form a tree.
The next lines each contain one integer (), indicating an operation where and need to be swapped.
Output Format
Output lines.
The first line outputs the minimum danger level for the initial visiting order.
Then, the -th line outputs the minimum danger level after the -th operation.
5 3
3 5 1 2 4
1 2
2 3
2 4
4 5
2
4
1
3
3
4
4
Hint
Translated by ChatGPT 5