Tree and queries 5

On a tree where vertices flip black and white, answer for each query the distance from a given vertex to the nearest white vertex.

Hard9TreeDivide and conquerDynamic programmingShortest pathNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a tree with NN vertices. A tree is a connected undirected graph with no cycle. The vertices are numbered from 1 to NN, and the edges are numbered from 1 to N1N-1. Every vertex is black at the start.

Write a program that handles the two query types below.

  • 1 i: flip the color of vertex ii. Black becomes white, and white becomes black.
  • 2 v: among all white vertices uu, print the smallest distance from uu to vv. Here uu and vv may be the same vertex, so the answer is 0 when vv is white. If the tree has no white vertex, print -1.

The distance between two vertices is the number of edges on the path that joins them.

Input

The first line contains the number of vertices NN (2N1000002 \le N \le 100000).

Each of the next N1N-1 lines contains the two vertex numbers uu and vv joined by edge ii (1u,vN1 \le u, v \le N).

The next line contains the number of queries MM (1M1000001 \le M \le 100000).

Each of the next MM lines contains one query, written as 1 i or 2 v, with 1i,vN1 \le i, v \le N.

Output

For each query of type 2, print the answer on its own line, in the order the queries are given.