A research project is being conducted to study the evolution of species. In the beginning, there exists a single species, numbered as index \(1\).
The process of evolution may be expressed as a tree: At any point, an existing species may evolve to form a new species - said existing species is the new species' parent, and the new species is the existing species' child. Whenever this happens, we assign the new species the next integer index.
For example, in Figure 1 below, species \(1\) initially evolves to create species \(2\) and \(3\); after which species \(2\) evolves to create \(4\), \(5\), and \(6\); species \(3\) evolves to species \(7\); and finally species \(6\) forms species \(8\) and \(9\).
The first line of input consists of a single integer \(N + Q\), the number of studies over the project.
\(N + Q\) lines of input follow. The \(i^{th}\) of these lines contains 2 integers, representing a valid study as described in the task description.
Output \(Q\) lines, one for each study of type \(2\) - each containing a single integer, the minimum evolutionary complexity of the required evolutionary subtree.
| Subtask # | Score | Constraints |
|---|---|---|
| 1 | 7 | \(N, Q \leq 15\) |
| 2 | 11 | \(N \leq 3000\) \(Q \leq 15\) |
| 3 | 5 | The parent of species \(i\) is \(\left\lfloor\frac{i}{2}\right\rfloor\). \((2 \leq i \leq N + 1)\) |
| 4 | 21 | Each species will have no more than 2 direct children. |
| 5 | 15 | \(N, Q \leq 3000\) |
| 6 | 41 | No additional constraints |
| Sample Input 1 | Sample Output 1 |
12
|
2
|
The four figures below demonstrate an optimal classification scheme for each type \(2\) study respectively, where major branches are bolded edges:
| Subtask | Score |
|---|---|
| 1 | 7 |
| 2 | 11 |
| 3 | 5 |
| 4 | 21 |
| 5 | 15 |
| 6 | 41 |
| 7 | 0 |