Problem E
Fuzzy Family Tree
Your friend Sue B. Tree, of the Rootingford Trees is facing a problem. Her clan of well-known algorithmic problem solvers has a large painting of their family tree hanging on the wall, tracing the lineage of the $N$ family members all the way back to Enar E. Tree himself, known for the invention of the B-tree data structure. After some $32$ generations or so, many names on the painting have faded so much that nobody knows exactly which vertex in the family tree belongs to the founder of the clan.
Sue has managed to read the names of some adjacent pairs of names for which she knows which one of them is the parent of the other. Given the layout of the family tree and the list of known parental relations, can you help Sue figure out what vertices in the tree could possibly represent Enar E. Tree? It is known that each person (except Enar) has exactly one parent within the clan – causing cycles in the family tree is grounds for immediate excommunication.
Input
The first line contains the integer $N$ ($1 \le N \le 100\, 000$), the number of family members on the painting. Each family member is numbered between $0$ and $N - 1$, inclusive.
The next $N - 1$ lines describes the parental relations on the painting. Each line has the format a > b or a ? b, where $0 \le a \neq b < N - 1$ are two integers. The first format means that Sue knows that $a$ is the parent of $b$, while the second one means that Sue does not know which person is the parent of the other.
It is guaranteed that there exists at least one vertex that could correspond to Enar.
Output
Output, in increasing order, the numbers of the family members on the painting who could be Enar, separated by spaces.
Scoring
Your solution will be tested on a set of test groups, each worth a number of points. To get the points for a test group you need to solve all test cases in the test group.
Group |
Points |
Constraints |
$1$ |
$1$ |
$N \le 1000$ |
$2$ |
$1$ |
No additional constraints |
Sample Input 1 | Sample Output 1 |
---|---|
1 |
0 |
Sample Input 2 | Sample Output 2 |
---|---|
2 0 > 1 |
0 |
Sample Input 3 | Sample Output 3 |
---|---|
2 1 ? 0 |
0 1 |
Sample Input 4 | Sample Output 4 |
---|---|
3 1 ? 2 0 > 1 |
0 |
Sample Input 5 | Sample Output 5 |
---|---|
3 1 ? 0 1 > 2 |
0 1 |