#P8990. [北大集训 2021] 小明的树

[北大集训 2021] 小明的树

Background

CTT2021 D3T1

Problem Description

Xiaoming has a tree with nn nodes rooted at 11. Each non-root node has a lamp on it. He is given a permutation a1,a2,,an1a_1, a_2, \dots, a_{n-1} of 2n2 \thicksim n. He also has a counter, initially 00.

He will turn on these n1n-1 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 n1n-1 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 n,mn, m, meaning the tree has nn nodes and there are mm modifications.

The next n1n-1 lines each contain 22 integers, describing an edge.

The next line contains n1n-1 integers aia_i, representing a permutation of 2n2 \thicksim n.

The next mm lines each contain four integers x1,y1,x2,y2x_1, y_1, x_2, y_2, meaning to remove the edge between x1,y1x_1, y_1 and add an edge between x2,y2x_2, y_2. The testdata is guaranteed to be valid.

Output Format

Output m+1m+1 lines in total. The first line is the answer for the initial tree.

The next mm 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 11 (1010 points): it is guaranteed that 2n5000002 \leq n \leq 500000, m=0m = 0.

  • Subtask 22 (2020 points): it is guaranteed that 2n80002 \leq n \leq 8000, 0m80000 \leq m \leq 8000.

  • Subtask 33 (7070 points): it is guaranteed that 2n5000002 \leq n \leq 500000, 0m5000000 \leq m \leq 500000.

Translated by ChatGPT 5