Colors
A manager for a toy company wants to reduce the cost of manufacturing their line of toys. Briefly, the toys are created by robots that operate on assembly tracks by adding and linking track components into modules and by merging existing modules into more complex modules. Components can be either active or inactive. At any moment there is exactly one active component per each module and track; and this is the only component that can be linked or merged on that track for that module.
The new budget for this company will only support components which consist of three colors and adjacent componentsmust have different colors. Your job is to decide which of the current toys in the inventory can be produced with this coloring limitation.
The company uses the following BNF formalism to describe more precisely the blueprints of its toys:
1. <toy> ::= <last-track><module>
The current <toy> consists of a main <module>. <last-track> gives the number of the last track used to build the current <toy>; the tracks are numbered from 0 to <last-track>.
2. <module> ::= '(' <operator-sequence> ')'
<module> represents a simple module that only contains operators. The operators given by the <operator-sequence> are processed in a left-to-right order.
This <module> starts with empty tracks and then automatically adds one active component on each of the available tracks.
3. <merged-module> ::= '(' <module>_1<module>_2 ')'
This is a merge operation that builds a complex <merged-module> by merging the active components of<module>_1 and <module>_2, after both modules are completely built.
4. <module> ::= '(' <merged-module><operator-sequence> ')'
The components of the <merged-module> remain on the tracks and its active components are further worked upon by the operators given by <operator-sequence>.
5. <operator-sequence> ::= λ | <operator><operator-sequence>
6. <operator> ::= <node-operator> | <edge-operator>
7. <node-operator> ::= <track-number>
A <node-operator> adds an active component on the specified <track-number> for the current module and the previously active component on that track becomes inactive.
8. <edge-operator> ::= <track-number-pair>
An <edge-operator> links the active components of the two track-numbers.
The following examples show several simple toy blueprints; in the figures tracks are represented as horizontal dotted lines, components as circles, links as full lines, and the time axis flows from left to right.
Example 1 Figure 1 depicts a 3-colorable toy that can be built using the following blueprint:
2 ( 20 10 21 2 20 )
Figure 1: 3-colorable toy
There are 3 tracks numbered 0, 1, 2. The toy consists of a single module containing 4 components labeled a, b, c,d, linked by the lines c–a, b–a, c–b, d–a. To build this toy the robot will execute in order the following operations:
Add a on track 0, b on track 1, c on track 2. At this stage a, b and c are the active components.
Make the links c–a, b–a and c–b.
Add d on track 2, which makes d active and inactivates c.
Make the link d–a. At this stage a, b and d are the active components.
Note that the same toy can also be built using several other blueprints, such as the following two:
2 ( 10 20 21 2 20 )
2 ( ( ( 20 10 ) ( 21 ) ) 2 20 )
Example 2 Figures 2 and 3 illustrate a sequence of operations involving a merge, for another 3-colorable (in fact even 2-colorable) toy that can be built as specified by the following blueprint:
1 ( ( ( 10 1 10 0 ) ( 10 1 10 0 ) ) 10 1 10 )
1. First, module 1 is built:
(a) Add a on track 0 and b on track 1.
(b) Link b–a.
(c) Add c on track 1.
(d) Link c–a.
(e) Add d on track 0. c and d are now the active components of module 1.
2. Secondly, module 2 is built using similar operations. g and h are now the active components of module 2.
3. Thirdly, modules 1 and 2 are merged together, which means that active components of each track are identified, i.e., c = g and d = h. The snapshot of Figure 2 illustrates this moment, with braces showing component identification. (c + g) and (d + h) are now the active components of the merged module 1 + 2.
4. Fourthly, the just merged components are linked, i.e., (c + g)–(d + h).
5. Lastly, i is added to track 1 and linked with (d+h). The final result is depicted in Figure 3 and links made after the merging are depicted with double lines. At the end, i and (d+h) are the active components of the main module.
Input
The input will consist of a sequence of toy blueprints, one per line of at most 250 characters. Each toy blueprint contains a sequence of tokens separated by single spaces, and conforming to the BNF rules stated earlier.
The first token is a positive integer t, 0 ≤ t ≤ 6, denoting the maximum track number for the robot’s arms to grab (i.e., there are t + 1 current components for the robot).
The interpretations for the remaining tokens are given in the next table.
The input will be terminated by a toy description with t = 0, which is not processed.
Output
The required output is a line of the form "Toy #: ?", where # denotes the toy sequence number starting at 1 and ?is either Yes or No depending whether the toy can be properly built using at most 3 colors.