#P6729. [WC2020] 有根树

[WC2020] 有根树

Description

Given a rooted tree with nn nodes, the nodes are numbered from 1n1 \sim n, and node 11 is the root. Xiaoming has a node set SS. For a node uu in SS, he defines the value wuw_u as the number of nodes in the subtree of uu (including uu itself) that are contained in the set SS. For convenience, for nodes not in SS, we consider wu=0w_u = 0.

Next, Xiaoming needs you to choose a connected component CC that contains the root. Let aa be the number of nodes in CC that are contained in SS, and let bb be the maximum ww value among the nodes not in CC. If there is no node outside CC, then b=0b = 0. Xiaoming wants you to minimize max(a,b)\max(a,b).

Xiaoming thinks this problem is quite simple, so he also provides qq operations. Each operation will insert or delete one node in the set SS. For the set SS after each operation, please output the minimum possible value of max(a,b)\max(a,b).

Input Format

The first line contains a positive integer nn, indicating the number of nodes.

The next n1n - 1 lines each contain two integers x,yx, y, indicating an edge (x,y)(x,y) in the tree.

The next line contains a positive integer qq, indicating the number of operations.

The next qq lines each contain two numbers t,vt, v, indicating an operation. If t=1t = 1, the operation inserts node vv into SS, and it is guaranteed that before the operation vSv \notin S. If t=2t = 2, the operation deletes node vv from SS, and it is guaranteed that before the operation vSv \in S. Initially, SS is empty.

Output Format

After each operation, output one line with one integer indicating the answer.

5
1 2
1 3
1 4
2 5
5
1 4
1 1
1 2
1 5
2 2
1
1
1
2
1

Hint

Sample Explanation

After the first operation, S={4}S = \{4\}. One possible choice is C={1}C = \{1\}, and then a=0,b=1a = 0, b = 1.

After the second operation, S={1,4}S = \{1,4\}. One possible choice is C={1}C = \{1\}, and then a=1,b=1a = 1, b = 1.

After the third operation, S={1,2,4}S = \{1,2,4\}. One possible choice is C={1}C = \{1\}, and then a=1,b=1a = 1, b = 1.

After the fourth operation, S={1,2,4,5}S = \{1,2,4,5\}. One possible choice is C={1,2}C = \{1,2\}, and then a=2,b=1a = 2, b = 1.

After the fifth operation, S={1,4,5}S = \{1,4,5\}. One possible choice is C={1}C = \{1\}, and then a=1,b=1a = 1, b = 1.

Constraints

For all test points: 1n5×1051 \le n \le 5 \times 10^5, 1q1061 \le q \le 10^6, 1x,y,vn1 \le x, y, v \le n, t{1,2}t \in \{1,2\}.

Test Point ID nn \le qq \le Special Restriction
121 \sim 2 100100 500500 None
343 \sim 4 2×1042 \times 10^4
565 \sim 6 10510^5 2×1052 \times 10^5 A
787 \sim 8 2×1052 \times 10^5 4×1054 \times 10^5 B
9119 \sim 11 C
121612 \sim 16 None
172017 \sim 20 5×1055 \times 10^5 10610^6

The meanings of the symbols in the “Special Restriction” column are:

A: At any time, the size of the set SS does not exceed 5050.

B: The tree is a chain, and node 11 has degree 11.

C: For every node in the tree, its parent node index is smaller than itself, n=qn = q, and the ii-th operation inserts node ii into SS.

Translated by ChatGPT 5