DAG Serialization

시간 제한3초메모리 제한2048 MB

요약
각각 반환값이 정해진 set과 unset 연산들이 DAG의 부분 순서로 주어질 때, 레지스터 동작과 반환값을 모두 만족하는 위상 순서를 찾거나 불가능함을 판정한다.
난이도

보통10점 중 6점

유형
위상 정렬, 그래프, 그리디, 구현
정답자
아직 제출이 없습니다

문제

Consider a simple single-bit boolean register that 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. Suppose there were nn operations op_iop\_i (for 1≤i≤n1 \le i \le n) where at most two operations returned true. Also, we are given the partial order of operations as a directed acyclic graph (DAG): an edge i→ji \rightarrow j means that op_iop\_i happened before op_jop\_j. You are asked whether it is possible to put these operations in some linear sequential order that satisfies the given partial order and such that if operations are applied to the register in that order, their results are the same as given.

입력

In the first line, you are given an integer nn --- the number of operations (1≤n≤1051 \le n \le 10^5). In the following nn lines, you are given operations in the format "type result", where type is either "set" or "unset" and result is either "true" or "false". It is guaranteed that at most two operations have "true" results.

In the next line, you are given an integer mm --- the number of arcs of the DAG (0≤m≤1050 \le m \le 10^5). In the following mm lines, you are given arcs --- pairs of integers aa and bb (1≤a,b≤n1 \leq a, b \leq n; a≠ba \neq b). Each arc indicates that operation op_aop\_a happened before operation op_bop\_b.

출력

Print any linear order of operations that satisfies the DAG constraints and ensures the results of the operations match the ones given in the input. If a correct operation order does not exist, print −1-1.

예제4

  1. 예제 1

    입력
    5
    set true
    unset true
    set false
    unset false
    unset false
    2
    1 4
    5 2
    
    예상 출력
    5 1 3 2 4
    
  2. 예제 2

    입력
    3
    unset true
    unset false
    set true
    0
    
    예상 출력
    2 3 1
    
  3. 예제 3

    입력
    2
    unset false
    set true
    1
    2 1
    
    예상 출력
    -1
    
  4. 예제 4

    입력
    2
    unset false
    set false
    0
    
    예상 출력
    -1