#P8990. [北大集训 2021] 小明的树
[北大集训 2021] 小明的树
Background
CTT2021 D3T1
Problem Description
Xiaoming has a tree with nodes rooted at . Each non-root node has a lamp on it. He is given a permutation of . He also has a counter, initially .
He will turn on these lamps one by one in the order of the permutation. After each lamp operation, he will check whether the whole tree is beautiful. If it is beautiful, the counter will be increased by the number of connected components formed by the nodes whose lamps are currently on.
After the lamp operations, the value of the counter is called the answer of this tree.
A tree is beautiful if and only if, for every node whose lamp is on, all nodes in the subtree of this node are also on.
Xiaoming thinks this problem is too easy, so he wants to make the tree move.
After the initial query, he will delete one edge in the tree and add one edge, ensuring that the graph is still a tree after the modification. He wants to know, after each modification, if he resets the counter to zero and turns on the lamps again and counts again, what the answer of the tree is.
Input Format
The first line contains two integers , meaning the tree has nodes and there are modifications.
The next lines each contain integers, describing an edge.
The next line contains integers , representing a permutation of .
The next lines each contain four integers , meaning to remove the edge between and add an edge between . The testdata is guaranteed to be valid.
Output Format
Output lines in total. The first line is the answer for the initial tree.
The next lines are the answers after each modification.
10 10
2 1
3 1
4 2
5 1
6 4
7 6
8 5
9 4
10 1
6 4 2 7 8 9 10 3 5
6 7 10 7
1 5 8 9
1 2 10 8
10 8 7 6
2 4 2 9
8 9 1 5
5 8 8 2
2 9 10 8
10 7 4 10
10 8 8 9
13
15
4
6
2
2
10
7
8
8
7
10 10
2 1
3 2
4 3
5 4
6 2
7 5
8 7
9 1
10 8
6 8 3 9 2 5 7 10 4
1 9 3 9
4 5 2 7
2 7 6 7
8 10 10 1
6 7 8 1
3 9 9 8
1 2 7 3
2 3 2 9
8 1 1 7
2 9 2 8
3
2
2
1
2
4
4
3
3
3
3
Hint
-
Subtask ( points): it is guaranteed that , .
-
Subtask ( points): it is guaranteed that , .
-
Subtask ( points): it is guaranteed that , .
Translated by ChatGPT 5