Time Limit: Java: 2000 ms / Others: 2000 ms
Memory Limit: Java: 65536 KB / Others: 65536 KB
One of the greatest alchemists of the lower Middle Renaissance, Algernon da
Vinci (one of Leonardo's lesserknown cousins), had the foresight to construct
his chemical works directly over a fast-running stream. Through a series of
clever pipes and sluices, he routed portions of the stream past each of the
tables where his alchemists prepared their secret brews, allowing them to dispose
of their chemical byproducts into the waters flowing by the table.
As Algernon's business grew, he even added additional floors to his factory, with water lifted to the higher floors by treadmill-powered pumps (much to the dismay of the apprentices who found themselves assigned to pump duty). The pipework for the entire disposal system became quite complex. It was even rumored by some that the pipes actually circled back in some places, so that a particularly odorous compound flushed away from one table might return to that very same spot a few minutes later.
All was not well, however. Algernon's factory suffered from a series of mishaps, minor explosions, gas clouds, etc. It became obvious that chemicals dumped at one table might react violently with other chemicals dumped from another table downstream. Algernon realized that he needed to trace the possible chemical flows through his factory.
Write a program to aid Algernon in this task. To preserve the secrecy of the chemical processes that are Algernon's stock in trade, all chemicals will be identified by a single upper-case letter. All tables are identified by positive numbers in the range 1��N, where N is the number of tables.
No (I,J) pair will be listed more than once, but the pairs may occur in any
order. I and J will never be the same number.
The end of input is signaled by a pair of zeros:
Note that if a table only receives water directly from the stream entering the building, that table will never occur in the second position of a pair. Similarly, any table that discharges only into the stream leaving the building will never occur in the first position of a pair.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
For the figure at the right, an input would be: 1 4 AB C C BDA BCD . . A 1 2 2 4 3 1 1 3 3 4 0 0