ENG  RUSTimus Online Judge
Online Judge
Online contests
About Online Judge
Frequently asked questions
Site news
Problem set
Submit solution
Judge status
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests

1176. Hyperchannels

Time limit: 1.0 second
Memory limit: 64 MB
The Galaxy Empire consists of N planets. Hyperchannels exist between most of the planets. New Emperor urged to extend hyperchannels network in such a way, that he can move from any planet to any other using no more than one channel. One can pass through the channel only in one direction.
The last channel-establishing ship is located on the base near planet A. This ship can’t pass through the existing channel, it always establishes a new one. But presence of two channels connecting the same planets in one direction makes navigation too difficult, almost impossible. The problem is to find a route for this ship to establish all necessary channels with no excessive ones. In the end of this route ship should return to the base.


First line contains integer N ≤ 1000 and number of the planet A (AN) where the base is situated. Each of the following N lines contain N numbers, the j-th number of the i-th line equals to 1 if there exists channel from planet i to planet j, and equals to 0 otherwise. It is known, that Empire can fulfill its need of hyperchannels by establishing no more than 32000 new ones.


Output should contain the sequence in which channels should be established. Each line should contain two integers — numbers of source and destination planet of channel. You may assume, that solution always exists.


4 2
0 0 1 0
0 0 1 0
1 1 0 1
0 0 1 0
2 4
4 1
1 2
2 1
1 4
4 2
Problem Author: Pavel Atnashev
Problem Source: Third USU personal programming contest, Ekaterinburg, Russia, February 16, 2002