The Tree Problem

0

0 votes
Hard
Problem

It's Christmas time and Leonard has bought a small Christmas tree and placed right in the center of hall. Seeing the tree, Sheldon remembers that he has to solve a graph problem.

The problem states that given a tree graph, initially containing just one node labeled 1. There are Q queries, each one being one of following two types--

  • 1 p - You must connect a node to node with label p. The label of this new node will be the minimum label which is not currently present in the tree.
  • 2 - You must output the maximum possible distance between a pair of nodes u and v of the tree. Distance between nodes u and v is the number of edges on the unique path from node u to node v.

Input Format:

First line contains Q, number of queries.

Each of the next Q lines denote a query. if it starts with 1, it is of type 1 and another integer p follows. If it starts with 2, it denotes the query of type 2.

Output Format:

Output the required answer for each query of type 2.

Constraints:

1Q105

1p max label currently in the tree

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For 1st query, there is only one node. Hence, max distance is 0.

For 2nd query, we connect node labeled 2 with node labeled 1 as 2 is the least label which is not currently present in the tree.

For3rd query, we connect node labeled 3 with node labeled 2 as 3 is the least label which is not currently present in the tree.

For 4th query, max distance is between node 1 and node 3 which is 2.

For 5th query, we connect node labeled 4 with node labeled 3 as 4 is the least label which is not currently present in the tree.

For 6th query, max distance is between node 1 and node 4 which is 3.

Editor Image

?