Problem B
Parents
A certain software architect stores hierarchical data by assigning a parent to each item. While explaining his idea he drew the following table and chart on the whiteboard.
ID |
Parent |
Item |
0 |
-1 |
Geometry |
1 |
0 |
Polyhedron |
2 |
1 |
Prism |
3 |
1 |
Platonic Solid |
4 |
3 |
Tetrahedron |
5 |
3 |
Cube |
6 |
3 |
Icosahedron |
7 |
1 |
Pyramid |
8 |
0 |
Polygon |
9 |
8 |
Triangle |
10 |
8 |
Quadrilateral |
11 |
10 |
Parallelogram |
12 |
10 |
Rectangle |
13 |
12 |
Square |
![\includegraphics[width=\textwidth ]{diagram.png}](/problems/parents/file/statement/en/img-0001.png)
The architect needs to find the deepest item(s). Write a program to print the deepest items in the heirarchy in alphabetical order. In the example above, there is only one deepest item: Square. If however, Square were not included in the example above, five deepest items exist: Tetrahedron, Cube, Icosahedron, Parallelogram, and Rectangle.
Input
The first line of input contains the number ($n$) of data sets to consider. ($0 < n \le 1000$) Each data set consists of an initial line containing a single positive integer, the number of items in that set ($0 < m \le 1000$). Each item appears on a line by itself containing, in order, a single integer, a space, and a sequence of upto $100$ lower-case letters identifying the item. The integer is the index of the parent of the item, which must already exist, however if the item has no parent, the index is $-1$. Item identifiers are guarenteed to be unique.
Output
Each data set should produce one line of output containing the deepest items in alphabetical order.
Sample Input 1 | Sample Output 1 |
---|---|
2 14 -1 geometry 0 polyhedron 1 prism 1 platonicsolid 3 tetrahedron 3 cube 3 icosahedron 1 pyramid 0 polygon 8 triangle 8 quadalateral 10 parallelogram 10 rectangle 12 square 13 -1 geometry 0 polyhedron 1 prism 1 platonicsolid 3 tetrahedron 3 cube 3 icosahedron 1 pyramid 0 polygon 8 triangle 8 quadalateral 10 parallelogram 10 rectangle |
square cube icosahedron parallelogram rectangle tetrahedron |