AT_abc460_f [ABC460F] Farthest Pair Query

Description

There is a tree with $ N $ vertices. The vertices are numbered $ 1, 2, \ldots, N $ , and the $ i $ -th edge connects vertices $ U_i $ and $ V_i $ . Initially, all vertices are painted black. Process $ Q $ queries of the following form in order and find the answer for each query. - An integer $ x $ $ (1 \leq x \leq N) $ is given. If vertex $ x $ is white, repaint it black; if vertex $ x $ is black, repaint it white. Then, find the maximum distance between two black vertices. Here, the distance between two vertices on a tree is the number of edges in the simple path between them. In the given inputs, there are always at least two black vertices when processing the queries in order.

Input Format

The input is given from Standard Input in the following format, where $ \text{query}_i $ denotes the $ i $ -th query: > $ N $ $ U_1 $ $ V_1 $ $ U_2 $ $ V_2 $ $ \vdots $ $ U_{N-1} $ $ V_{N-1} $ $ Q $ $ \text{query}_1 $ $ \text{query}_2 $ $ \vdots $ $ \text{query}_Q $ Each query is given in the following format: > $ x $

Output Format

Output $ Q $ lines. The $ i $ -th line should contain the answer for the $ i $ -th query when processed in order.

Explanation/Hint

### Sample Explanation 1 - After the repainting in the $ 1 $ st query, the black vertices are $ 2, 3, 4, 5, 6, 7 $ . The distance between vertices $ 4 $ and $ 6 $ is $ 4 $ , and the answer is $ 4 $ . - After the repainting in the $ 2 $ nd query, the black vertices are $ 2, 3, 5, 6, 7 $ . The distance between vertices $ 2 $ and $ 6 $ is $ 3 $ , and the answer is $ 3 $ . - After the repainting in the $ 3 $ rd query, the black vertices are $ 3, 5, 6, 7 $ . The distance between vertices $ 5 $ and $ 6 $ is $ 3 $ , and the answer is $ 3 $ . - After the repainting in the $ 4 $ th query, the black vertices are $ 3, 5, 7 $ . The distance between vertices $ 5 $ and $ 7 $ is $ 2 $ , and the answer is $ 2 $ . - After the repainting in the $ 5 $ th query, the black vertices are $ 5, 7 $ . The distance between vertices $ 5 $ and $ 7 $ is $ 2 $ , and the answer is $ 2 $ . - After the repainting in the $ 6 $ th query, the black vertices are $ 1, 5, 7 $ . The distance between vertices $ 1 $ and $ 5 $ is $ 3 $ , and the answer is $ 3 $ . - After the repainting in the $ 7 $ th query, the black vertices are $ 5, 7 $ . The distance between vertices $ 5 $ and $ 7 $ is $ 2 $ , and the answer is $ 2 $ . - After the repainting in the $ 8 $ th query, the black vertices are $ 4, 5, 7 $ . The distance between vertices $ 4 $ and $ 5 $ is $ 3 $ , and the answer is $ 3 $ . - After the repainting in the $ 9 $ th query, the black vertices are $ 4, 5, 6, 7 $ . The distance between vertices $ 4 $ and $ 6 $ is $ 4 $ , and the answer is $ 4 $ . Note that the examples given above are just one instance achieving the maximum value. ### Constraints - $ 3 \leq N \leq 10^5 $ - $ 1 \leq U_i, V_i \leq N $ - The given graph is a tree. - $ 1 \leq Q \leq 10^5 $ - For each query, $ 1 \leq x \leq N $ . - There are always at least two black vertices. - All input values are integers.