## 18 Sep Tuesday Python Homework 19:00 – 22.09.13.

**Question 1:**

Here is a list for you left just this morning by your parental figure.

- Do your Math homework.
- Call your grandma.
- Call me at work.
- Call your friend.
- Feed the dog.
- Let the dog out.
- Watch television.

As well, your parental figure has given you constraints on these tasks:

- Do your Math homework BEFORE you watch Television.
- Do your Math homework BEFORE you call your friend.
- Call your grandma BEFORE you do your Math homework.
- Call me at work BEFORE you call your friend.
- Feed the dog AFTER you call me at work.

Your “to do” list (above) can now be abbreviated to:

- 1,7
- 1,4
- 2,1
- 3,4
- 3,5

where x, y indicates that the task numbered x should be done before the task numbered y.

Unfortunately, during the day additional instructions are emailed to you by your parental figure. Write a program to use your original “to do” list and the additional instructions to output a list of your jobs in the order you must do them, or alternately, if you cannot complete them, you should output that there is no way to complete these tasks, and you are just going to go to bed.

**Input**

You will be given pairs of numbers, one number per line, to represent the additional instructions to be included with your original “to do” list given above. The data terminates with the input pair 0 and 0. You can assume that there will be at most 10 additional constraints.

**Output**

You should output a list of tasks in the order that they should be performed, or an error message saying that the tasks cannot be completed. If there are multiple orders in which the tasks may be completed, the following tie-breaking rule must be used:

If there is a set of tasks which may be performed at the same time during the process, the smallest numbered task should be performed first.

Sample Input 16 2 5 4 0 0Sample Output 13 5 6 2 1 4 7Explanation for Sample Output 1The input data tells us that task 6 must be performed before task 2, and task 5 before task 4. The only tasks with no prerequisites are 3 and 6, so we choose 3 because it has the lower number. Then 5 and 6 are possible; 5 is chosen, then 6. Next must come 2, followed by 1. Then both 4 and 7 are possible; the lower one is chosen first.Sample Input 27 2 4 5 0 0Sample Output 2Cannot complete these tasks. Going to bed.Explanation for Sample Output 2Notice that task 2 must be done before task 1, which must be done before task 7, which must be done before task 2. This forms a contradiction, and we cannot perform the tasks in the order prescribed.

**Question 2:** (Hint: you could use stack to solve the question)

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has ** N **≤ 1000 coaches numbered in increasing order 1, 2, . . . ,

**. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a**

*N*_{1}.a

_{2}, . . . , a

_{N}. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.

**Input**

The input file consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer ** N** described above. In each of the next lines of the block there is a permutation of 1, 2, . . . ,

**. The last line of the**

*N*block contains just ‘0’.

The last block consists of just one line containing ‘0’.

**Output**

The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains ‘Yes’ if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains ‘No’. In addition, there is one empty line after the lines corresponding to one block of the input. Note that there is a ‘\n’ after the last output case.

Sample Input5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 4 3 2 4 1 0 0Sample OutputYes No Yes Yes

Sorry, the comment form is closed at this time.