This page is still under construction.

Parts of this page are still being built. What you see may change.

New Barns

Time limit2sMemory limit512 MB

Summary
Process online queries that add a leaf to a growing forest or ask for the eccentricity (distance to the farthest node) of a given node.
Level

Hard8 of 10

Topics
Tree, Graph, BFS, Dynamic programming
Solved
No attempts yet

Problem

Farmer John notices that his cows tend to get into arguments if they are packed too closely together, so he wants to open a series of new barns to spread them out.

Whenever John builds a new barn, he connects it with at most one bidirectional pathway to a barn that already exists. To make sure his cows are spread far enough apart, he sometimes wants the distance from a certain barn to the farthest barn reachable from it. The distance between two barns is the number of pathways you traverse to go from one to the other.

John gives QQ (1≤Q≤1051 \leq Q \leq 10^5) queries in total, each either a build query or a distance query. In a build query, John builds one barn and links it with at most one previously built barn. In a distance query, John asks for the distance from a certain barn to the farthest barn reachable from it along the pathways. The queried barn has already been built. Answer all of the queries.

Input

The first line contains the integer QQ. Each of the next QQ lines contains one query, either "B p" or "Q k", telling you to build a barn and connect it with barn pp, or to report the farthest distance from barn kk. If p=−1p = -1, the new barn is connected to no other barn. Otherwise pp is the index of a barn that has already been built. Barn indices start from 11, so the first barn built is barn 11, the second is barn 22, and the numbering continues that way.

Output

Print one line for each distance query. A barn that is connected to no other barn has farthest distance 00.

Note

The input of the first example corresponds to this network of barns.

  (1) 
    \   
     (2)---(4)
    /
  (3)

Query 1 builds barn 11. Query 2 asks for the distance from barn 11 to the farthest connected barn. Barn 11 is connected to no other barn, so the answer is 00. Query 3 builds barn 22 and connects it to barn 11, and query 4 builds barn 33 and connects it to barn 22. Query 5 asks for the farthest barn from barn 33. The farthest one is barn 11 at distance 22, so the answer is 22. Query 6 builds barn 44 and connects it to barn 22. Query 7 asks for the farthest barn from barn 22. Barns 11, 33 and 44 all sit at distance 11, so the answer is 11.

Examples3

  1. Example 1

    Input
    7
    B -1
    Q 1
    B 1
    B 2
    Q 3
    B 2
    Q 2
    
    Expected output
    0
    2
    1
    
  2. Example 2

    Input
    10
    B -1
    Q 1
    B -1
    B 2
    B 3
    Q 4
    Q 2
    B 1
    Q 1
    Q 5
    
    Expected output
    0
    2
    2
    1
    1
    
  3. Example 3

    Input
    11
    B -1
    B 1
    B 2
    B 3
    B 4
    Q 1
    Q 3
    Q 5
    B 3
    Q 6
    Q 1
    
    Expected output
    4
    2
    4
    3
    4