DAG Serialization
Time limit3sMemory limit2048 MB
Order n set and unset operations under DAG precedence so each returned result matches a register that starts false, or print -1.
- Level
Medium6 of 10
- Topics
- Topological sort, Graph, Implementation
- Solved
- No attempts yet
Problem
A single-bit boolean register supports two operations:
- set: sets the register to true if it was false, and returns true. Otherwise, it returns false.
- unset: sets the register to false if it was true, and returns true. Otherwise, it returns false.
The initial state of the register is false. There are operations (for ), and at most two of them return true. The order constraints form a directed acyclic graph (DAG): an edge means that happened before . Determine whether the operations can be arranged in a linear order that respects these constraints and produces the given results when applied to the register in that order.
Input
The first line contains an integer , the number of operations (). Each of the next lines contains an operation in the format type result. Here type is set or unset, and result is true or false. It is guaranteed that at most two operations have the result true.
The next line contains an integer , the number of arcs in the DAG (). Each of the next lines contains two integers and (; ), meaning that happened before .
Output
Print any linear order of the operations that satisfies the DAG constraints and matches the given results. If no such order exists, print .