Problem E
Flight Plans
As you are probably aware, flight pricing can sometimes be surprisingly complex. For example, it can often be cheaper to take a much longer flight with several legs instead of flying directly between two airports. One of the reasons pricing seems to be complex is that airlines often try to obfuscate exactly how pricing works, in order to ensure that their customers choose more expensive flights.
One particular airline has deciced to take this obfuscation
to the next level; they do not even offer an automated search
of their flights. Instead, they describe their flights in a
very peculiar format. For every one of their
-
what airports they travel to from this airport, or
-
what airports they do not travel to from this airport.
.
To compensate for this complexity, the airline sets the price of every direct flight between two airports to the same amount.
Can you write a program that, given the descriptions of all
the flights the airline provides, determine the minimum number
of flights required to travel from airport
Input
The first line of input contains an integer
The next
Following this letter is an integer
The sum of
Output
Output a single integer, the minimum number of flights
required to travel from airport
If there is no path, output “impossible”.
Explanation of Sample Input 1
The only flight from airport
Since no airport has a direct flight to airport
Explanation of Sample Input 2
The only flight from airport
Thus, there is a flight plan from
Sample Input 1 | Sample Output 1 |
---|---|
4 0 1 N 1 2 C 1 2 N 1 3 C 1 1 |
impossible |
Sample Input 2 | Sample Output 2 |
---|---|
4 0 1 N 1 2 C 1 2 N 1 3 C 1 0 |
3 |