Polish version    English version  
  About Olympic -> Problems


 News
 About Olympic
About contest
Problems
I OI 1993/1994
II OI 1994/1995
III OI 1995/1996
IV OI 1996/1997
V OI 1997/1998
VI OI 1998/1999
VII OI 1999/2000
VIII OI 2000/2001
IX OI 2001/2002
X OI 2002/2003
XI OI 2003/2004
XII OI 2004/2005
XIII OI 2005/2006
XIV OI 2006/2007
XV OI 2007/2008
Problems archive
 History of OI
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
III Olympiad in Informatics 1995/1996

Task: HAZ
Author: Wojciech Plandowski
Gambling

II stage contest  

A gambling machine consists of n generators of integers: G1, ..., Gn, where 1 <= n <= 1000. The generator 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 activated for the first time it generates an integer from the set Si. The next activating ends up in generating an integer from the set Si, which was not chosen before. If there is no such an integer, Gi generates 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 generator is Gr.
  • If zero is generated the machine stops.

The machine looses if the generator Gn generates zero and the rest of the generators had exhausted their sets of integers.

The machine is well constructed 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.

Input

In the first line of the text file HAZ.IN 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

In the text file HAZ.OUT 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.

Example

For the input file HAZ.IN:
2
2 1 2
1 2

the correct answer is the output file HAZ.OUT:

2 2 0

For the input file HAZ.IN:
2
1 2
0

the correct answer is the output file HAZ.OUT:
NIE

Your program should look for the file HAZ.IN in the current directory and produce the output file HAZ.OUT in the current directory too. The file containing the source code of your program should have a name HAZ.???, whereas you should put three-letter abbreviation of the used programming language name instead of ???. The same program in executable form should be written in file HAZ.EXE




Print friendly version