eolymp
bolt
Try our new interface for solving problems

Task

In most recipes, certain tasks have to be done before others. For each task, if we are given a list of other tasks that it depends on, then it is relatively straightforward to come up with a schedule of tasks that satisfies the dependencies and produces a stunning dish. Many of us know that this can be solved by some algorithm called toplogical sort. But life is not so easy sometimes. For example, here is a recipe for making pizza dough: \begin{enumerate} \item Mix the yeast with warm water, wait for \textbf{5} to \textbf{10} minutes. \item Mix the the remaining ingredients \textbf{7} to \textbf{9} minutes. \item Mix the yeast and the remaining ingredients together for \textbf{10} to \textbf{15} minutes. \item Wait \textbf{90} to \textbf{120} minutes for the dough to rise. \item Punch the dough and let it rest for \textbf{10} to \textbf{15} minutes. \item Roll the dough. \end{enumerate} In this case, tasks \textbf{1} and \textbf{2} may be scheduled after the first minute (we always spend the first minute to read the recipe and come up with a plan). The earliest task \textbf{3} may be started is at \textbf{8} minutes, and task \textbf{4} may start at \textbf{18} minutes after the start, and so on. This recipe is relatively simple, but if some tasks have many dependent tasks then scheduling can become unmanageable. Sometimes, the recipe may in fact be impossible to execute. For example, consider the following abstract recipe: \begin{enumerate} \item task \textbf{1} \item after task \textbf{1} but within \textbf{2} minutes of it, do task \textbf{2} \item at least \textbf{3} minutes after task \textbf{2} but within \textbf{2} minutes of task \textbf{1}, do task \textbf{3} \end{enumerate} In this problem, you are given a number of tasks. Some tasks are related to another based on their starting times. You are asked to assign a starting time to each task to satisfy all constraints if possible, or report that no valid schedule is possible. \InputFile The input consists of a number of cases. The first line of each case gives the number of tasks \textbf{n}, (\textbf{1} ≤ \textbf{n} ≤ \textbf{100}). This is followed by a line containing a non-negative integer \textbf{m} giving the number of constraints. Each of the next \textbf{m} lines specify a constraint. The two possible forms of constraints are: task \textbf{i} starts at least \textbf{A} minutes later than task \textbf{j}task \textbf{i} starts within \textbf{A} minutes of the starting time of task \textbf{j} where \textbf{i} and \textbf{j} are the task numbers of two different tasks (\textbf{1} ≤ \textbf{i}, \textbf{j} ≤ \textbf{n}), and \textbf{A} is a non-negative integer (\textbf{A} ≤ \textbf{150}). The first form states that task \textbf{i} must start at least \textbf{A} minutes later than the start time of task \textbf{j}. The second form states that task \textbf{i} must start no earlier than task \textbf{j}, and within \textbf{A} minutes of the starting time of task \textbf{j}. There may be multiple constraints involving the same pair of tasks. Note that at least and within include the end points (i.e. if task \textbf{1} starts at \textbf{1} minute and task \textbf{2} starts at \textbf{4} minutes, then task \textbf{2} starts at least \textbf{3} minutes later than task \textbf{1}, and within \textbf{3} minutes of the starting time of task \textbf{1}). The input is terminated by \textbf{n = 0}. \OutputFile For each case, output a single line containing the starting times of task \textbf{1} through task \textbf{n} separated by a single space. Each starting time should specify the minute at which the task starts. The starting time of each task should be positive and less than \textbf{1000000}. There may be many possible schedules, and any valid schedule will be accepted. If no valid schedule is possible, print \textbf{Impossible.} on a line instead.
Time limit 2 seconds
Memory limit 64 MiB
Input example #1
6
10
task 3 starts at least 5 minutes later than task 1
task 3 starts within 10 minutes of the starting time of task 1
task 3 starts at least 7 minutes later than task 2
task 3 starts within 9 minutes of the starting time of task 2
task 4 starts at least 10 minutes later than task 3
task 4 starts within 15 minutes of the starting time of task 3
task 5 starts at least 90 minutes later than task 4
task 5 starts within 120 minutes of the starting time of task 4
task 6 starts at least 10 minutes later than task 5
task 6 starts within 15 minutes of the starting time of task 5
3
4
task 2 starts at least 0 minutes later than task 1
task 2 starts within 2 minutes of the starting time of task 1
task 3 starts at least 3 minutes later than task 2
task 3 starts within 2 minutes of the starting time of task 1
0
Output example #1
3 1 8 18 108 118
Impossible.