QTREE5 - Query on a tree V

题意翻译

你被给定一棵n个点的树,点从1到n编号。每个点可能有两种颜色:黑或白。我们定义dist(a,b)为点a至点b路径上的边个数。 一开始所有的点都是黑色的。 要求作以下操作: 0 i 将点i的颜色反转(黑变白,白变黑) 1 v 询问dist(u,v)的最小值。u点必须为白色(u与v可以相同),显然如果v是白点,查询得到的值一定是0。 特别地,如果作'1'操作时树上没有白点,输出-1。 Translated by @vegacx

题目描述

You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N. We define dist(a, b) as the number of edges on the path from node a to node b. Each node has a color, white or black. All the nodes are black initially. We will ask you to perfrom some instructions of the following form: - 0 i : change the color of i-th node(from black to white, or from white to black). - 1 **v** : ask for the minimum dist(u, **v**), node u must be white(u can be equal to **v**). Obviously, as long as node **v** is white, the result will always be 0.

输入输出格式

输入格式


- In the first line there is an integer N (N <= 100000) - In the next N-1 lines, the i-th line describes the i-th edge: a line with two integers a b denotes an edge between a and b. - In the next line, there is an integer Q denotes the number of instructions (Q <= 100000) - In the next Q lines, each line contains an instruction "0 i" or "1 **v**"

输出格式


For each "1 **v**" operation, print one integer representing its result. If there is no white node in the tree, you should write "**-1**".

输入输出样例

输入样例 #1

10
1 2
1 3
2 4
1 5
1 6
4 7
7 8
5 9
1 10
10
0 6
0 6
0 6
1 3
0 1
0 1
1 3
1 10
1 4
1 6

输出样例 #1

2
2
2
3
0