Airport Show

Time limit1sMemory limit128 MB

Summary
Determine whether two runway request sequences can interleave into a deadlock and, if so, output the lexicographically smallest interleaving sequence that causes it.
Level

Hard8 of 10

Topics
Simulation, Graph, Greedy
Solved
No attempts yet

Problem

The airline Flying Bugs is preparing an aerial show as part of an advertising campaign. The show takes place at the company airport, which has N runways numbered 1 to N, and consists of two aerobatic groups performing in parallel. Each performance follows a fixed program: to preserve its visual effect, its planes must take off from and land on specific runways in a prescribed order.

The airport security system tracks which runways are in use and forbids any other plane from entering a runway while it is in use. Each performance is delivered to the security team as a sequence of reservation (RES) and release (REL) requests for specific runways. It cannot be predicted in advance when these requests will actually arrive. The authorities want to know whether the two performances could interleave in such a way that, at some moment, neither performance can continue without violating the order of its own runway uses.

Each performance's own request sequence satisfies the following conditions:

  • a runway is never reserved again until it has first been released,
  • only a runway that is currently reserved (and not yet released) may be released,
  • every reserved runway is released before the performance ends.

If a performance needs a runway that is currently reserved by the other performance, it may wait until the other releases it (the other performance may reserve and release several runways in the meantime and eventually release the required runway).

The airport becomes blocked when one performance requests a runway held by the other while itself holding a runway that the other is requesting. When that happens, neither performance can proceed without breaking its prescribed order. Your task is only to check whether such a blocking interleaving exists; you do not have to schedule the performances so that blocking is avoided.

Input

The first line contains a single integer N (1 ≤ N ≤ 1000), the number of runways at the airport. Two blocks follow, one per performance. Each block starts with a line containing a single even integer L (2 ≤ L ≤ 5000), the number of reservation and release requests made during that performance. Each of the next L lines contains the 3-character string RES or REL and an integer A (1 ≤ A ≤ N), separated by a single space. RES A requests reserving runway A and REL A requests releasing runway A.

Output

If every possible interleaving of the two request sequences lets both performances finish without blocking the airport, print exactly:

The performances will always finish.

Otherwise at least one interleaving reaches a blocked state. Report one such interleaving as a sequence of the digits 1 and 2 written with no separators. Reading the sequence from left to right, each digit names the performance that carries out its next pending request: the first digit's performance performs its first pending request, then the next digit's performance performs its next pending request, and so on. The sequence must stay valid at every step (each runway is reserved by at most one performance at a time) and must stop exactly at a blocked state — the next pending request of performance 1 asks to reserve a runway currently held by performance 2, and the next pending request of performance 2 asks to reserve a runway currently held by performance 1.

Several interleavings may reach a blocked state, so print the lexicographically smallest such sequence. Compare two sequences as strings over the characters 1 and 2 (with 1 < 2): scan them left to right and, at the first position where they differ, the sequence with the smaller digit is the smaller sequence.

Examples4

  1. Example 1

    Input
    2
    4
    RES 1
    RES 2
    REL 1
    REL 2
    4
    RES 2
    RES 1
    REL 2
    REL 1
    
    Expected output
    12
    
  2. Example 2

    Input
    4
    8
    RES 1
    RES 2
    RES 3
    REL 3
    REL 2
    RES 2
    REL 1
    REL 2
    4
    RES 3
    REL 3
    RES 4
    REL 4
    
    Expected output
    The performances will always finish.
    
  3. Example 3

    Input
    2
    4
    RES 1
    RES 2
    REL 2
    REL 1
    4
    RES 2
    RES 1
    REL 1
    REL 2
    
    Expected output
    12
    
  4. Example 4

    Input
    3
    4
    RES 1
    RES 2
    REL 2
    REL 1
    6
    RES 3
    REL 3
    RES 2
    RES 1
    REL 1
    REL 2
    
    Expected output
    1222