Under construction

Still being built. Things may change

Tree and queries 5

Time limit2sMemory limit512 MB

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

Hard9 of 10

Topics
Tree, Divide and conquer, Dynamic programming, Shortest path
Solved
No attempts yet

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 N−1N-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 (2≤N≤1000002 \le N \le 100000).

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

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

Each of the next MM lines contains one query, written as 1 i or 2 v, with 1≤i,v≤N1 \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.

Examples2

  1. Example 1

    Input
    10
    1 2
    1 3
    2 4
    1 5
    1 6
    4 7
    7 8
    5 9
    1 10
    10
    1 6
    1 6
    1 6
    2 3
    1 1
    1 1
    2 3
    2 10
    2 4
    2 6
    
    Expected output
    2
    2
    2
    3
    0
    
  2. Example 2

    Input
    2
    1 2
    6
    2 1
    1 2
    2 1
    2 2
    1 2
    2 1
    
    Expected output
    -1
    1
    0
    -1