Zoo Management
Time limit5sMemory limit1024 MB
Decide whether animals can move along tunnels, in simultaneous cycles, from their starting enclosures to the target arrangement.
Problem
When managing a zoo, you sometimes want to move animals between enclosures. For example, the zebras may like the spacier enclosure currently occupied by the penguins, the penguins may want to move to the colder enclosure where the koalas live, and the koalas will move to an empty enclosure filled with eucalyptus. You would move the koalas first to free the colder enclosure, then move the penguins there, and finally move the zebras.
Animals move through special tunnels that connect the enclosures. They must never go outside, because they may get scared or run away and hurt themselves.
Now every enclosure is full, which makes moving animals much harder. For example, if the koalas had to move into the former zebra enclosure, no animal could move first. Instead, the animals move at the same time. The zebras, koalas, and penguins start down different tunnels together and arrive at their new enclosures together, so they never meet. You cannot simply swap the animals in two connected enclosures, because they would meet in the tunnel and become scared.
You can, any number of times, choose a set of animals and move each one to an adjacent enclosure through a tunnel, as long as the animal in that enclosure also moves in the same move. No tunnel may be used more than once in the same move. Is it possible to reach the desired arrangement in a series of moves?
Input
The first line contains two integers () and (), the number of enclosures and tunnels. The next lines each contain two integers () and (), the type of animal in enclosure at the start and the type of animal you want in enclosure after all moves. It is guaranteed that is a permutation of . The next lines each contain two integers and (), meaning enclosures and are connected by a two-way tunnel. No two enclosures are connected by more than one tunnel.
Output
If it is possible to move the animals so that every enclosure contains the desired animal type, print possible. Otherwise, print impossible.