Paradox Sort (Small)
Time limit5sMemory limit512 MB
Find the lexicographically smallest candy order whose sequential pairwise elimination leaves candy A standing, or report IMPOSSIBLE.
- Level
Medium6 of 10
- Topics
- Graph, Brute force, Simulation
- Solved
- No attempts yet
Problem
Vlad likes candy. You have a bag of distinct candies, and you are going to let Vlad keep exactly one of them. You first choose an order for the candies, then hand them to Vlad one at a time in that order. Starting with the second candy, Vlad compares the candy he just received to the one he is holding, keeps the one he prefers, and throws the other away. A candy that has been thrown away never comes back.
You might expect Vlad to finish holding his favorite candy no matter which order you choose. That is not what happens. Vlad does not necessarily have a favorite candy. His choice between any two candies is known, but those choices do not have to come from a single ranking. He may pick Orange over Lemon, Banana over Orange, and Lemon over Banana.
There is a particular candy that you want Vlad to finish with. Given Vlad's preference for every pair of candies, decide whether some order leaves Vlad holding candy . If one exists, find the lexicographically smallest such order.
Input
The first line contains the number of test cases, . test cases follow.
Each test case starts with a line containing two integers and separated by one space. is the number of candies and is the number of the candy you want Vlad to finish with. The candies are numbered from to .
The next lines each contain characters. Character of line is Y if Vlad prefers candy to candy , N if he prefers candy to candy , and - if . Lines and characters are both numbered from . For , character of line differs from character of line .
Limits
Output
For each test case, print Case #x: followed by the answer. x is the test case number, starting at .
If no order leaves Vlad holding candy , print IMPOSSIBLE. Otherwise print the lexicographically smallest such order as candy numbers separated by one space. Orders are compared lexicographically as sequences of candy numbers.