Description
A gambling machine consists of n generators of integers: G1, ..., Gn, where 1 <= n <= 1000. The gene
rator Gi can generate integers only from the certain set Si included in the interval {1,..., n}, or
0 which means that the game is over. The Si can be an empty set. Let ki be the number of elements of
the set Si. The sum of all the integers ki, for i = 1,..., n, cannot exceed 12000.While Gi is activ
ated for the first time it generates an integer from the set Si. The next activating ends up in gene
rating an integer from the set Si, which was not chosen before. If there is no such an integer, Gi g
enerates zero.The machine starts with activating G1. Then the generators are activated according to
the following rule :? In case when a generator generated a positive integer r, the next activated ge
nerator is Gr.? If zero is generated the machine stops.The machine looses if the generator Gn genera
tes zero and the rest of the generators had exhausted their sets of integers.The machine is well con
structed if it may generate a sequence of integers ending with zero, but not leading to a defeat (i.
e. a sequence shorter then 1 + ( the sum of integers ki for i = 1,...,n) ).
Task
Write a program that:
Reads from the text file HAZ.IN the description of the machine, i.e. the number of generators n, integers ki and sets Si,
Verifies, whether the machine described in the input file is well constructed,
if it is well constructed, writes a sequence of integers, which may be generated by the machine, is ended with zero, and does not lead to defeat,
if not, writes one word NIE ("no" in Polish) in the text file HAZ.OUT.
一台赌博机有n个发生器G1,G2,G3,…,Gn。Gi可以产生的自然数集合是Si(Si中只包括1到n的自然数,Si可以
是空集)。令ki表示Si中数的个数,则所有的ki的和不超过12000.一次活动时,Gi从Si中选择一个自然数现实。以
后Gi每次从Si中选择一个以前没有选择过的数显示。如果Si中所有的数都已选过,则它显示0。开始时从G1开始,
然后赌博机这样运行:如果当前发生器显示了一个数字r,则下一活动的发生器是Gr,如果显示的数是0,则机器停
止运作。如果Gn产生了0,并且所有的Si为空,那么机器失败。如果机器在停止时没有失败,那么它获胜
Input
In the first line there is written one positive integer n <= 1000.
This is the number of generators.
In the (i + 1)?st line (for i = 1,..., n) there is written an integer ki
followed by all the elements of the set Si (written in arbitrary order).
The integers in each line are separated by single spaces.
Output
there should be written one word NIE (if the machine isn't well constructed) or
one line containing an appropriate finite series of integers separated by single spaces.
Sample Input
For the input file HAZ1.IN:2
2 1 2
1 2
For the input file HAZ2.IN:
2
1 2
0
Sample Output
the correct answer is the output file HAZ1.OUT:2 2 0
the correct answer is the output file HAZ2.OUT:
NIE