Polish version    English version  
  History of OI -> XIII OI 2005/2006 -> Problems


 News
 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
Schedule
Problems
Stage III - results
Stage II - results
Stage I - results
Stage II
Stage III
Regulations
For contestants
Helpful resources
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
V OI 1997/1998
IV OI 1996/1997
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN

The Postman

Memory limit: 64 MB

Each morning Byteasar the Postman has to visit every street of his district and deliver the mail. All of the roads are one-way and are connected by the (pairwise distinct) crossroads. A pair of crossroads may be connected by two roads at the most: one for each of the opposing directions. The crossroads are numbered from 1 to .

Byteasar starts and ends each route in Byteotian Post headquarters, by the first crossroads. For as long as it can be remembered Byteasar had been choosing his route by himself, but recently the board of directors has issued a new regulation, limiting the freedom of choice of the routes. Each postman has been assigned a collection of route fragments - a set of sequences of crossroads. Byteasar has to choose such a route which:

  • leads down each street once only,
  • comprises each of the assigned sequences (as a consistent subsequence),
  • starts and ends by the first crossroads.
Unfortunatelly, it is possible that the board of directors issued a regulation, for which a route satisfying the requirements does not in fact exist (it requires in one of its sequences that a road be taken, which does not exist, for instance). Help Byteasar and write a programme which verifies if a correct route exists and finds it if it does.

Task

Write a programme which:

  • reads from the standard input a description of the streets and assigned sequences,
  • verifies if it is possible for Byteasar to get around his district in such a way as to allow him to visit each street exactly once and to fulfill the directors' assigment,
  • writes the route found to the standard ouput or concludes that such a route does not exist.

Input

The first line of the standard input contains two integers and separated by a single space, , , denoting the number of crossroads and roads, respectively. The following lines contain the descriptions of the roads: two integers , , separated by a single space, , , denote a one-way road from the crossroads to . Each (ordered) pair appears in the data once at the most. In the following line there is an integer , , denoting the number of assigned sequences. The successive lines contain the descriptions of these sequences. A description consists of an integer , , and a sequence of crossroads' numbers. The numbers in a line are separated by single spaces. The total length of all of the sequences does not exceed .

Ouput

Your programme should write in the first line of the standard output:

  • TAK (YES in Polish) - if a route satisfying the requirements of the task exists,
  • NIE (NO in Polish) - if such a route does not exist.
Should the answer be YES, the following lines should contain a description of the route found. Should more such routes exists, write out any of them. The description consists of the sequence of crossroads the postman visits while on his route - numbers: , each written out in a separate line, such that:
  • ,
  • and (for ) are connected by a street,
  • each street appears on the list once only,
  • the route comprises, as a consistent subsequence all of the sequences imposed by the board of directors.

For the input data:
6 10
1 5
1 3
4 1
6 4
3 6
3 4
4 3
5 6
6 2
2 1
4
3 1 5 6
3 3 4 3
4 4 3 6 4
3 5 6 2
the correct outcome is:
TAK
1
3
4
3
6
4
1
5
6
2
1



Print friendly version