What's the Order Anyway?
InterviewTime limit2sMemory limit1024 MB
Count the orderings of n comedy acts (n up to 10) that satisfy all given precedence and non-adjacency clues.
- Level
Medium4 of 10
- Topics
- Backtracking, Brute force, Bit manipulation
- Solved
- No attempts yet
Statement
Alice, Bob, Carol, Denise, Eddie and Frank are performing in a local comedy show. Their stand-up routine order is not known. Instead, you have these cryptic clues (A is Alice, B is Bob, C is Carol, D is Denise, E is Eddie and F is Frank):
A > BF < Anot C D
The first clue means that A performs before B. The second clue means that F performs after A. The third clue means that C and D do not perform consecutively. (If the acts are treated as numbers, the inequality signs assume the acts are sorted from greatest to least.)
Given these clues, one possible order is Alice, Carol, Bob, Denise, Frank, Eddie. The exact order may not be fixed by the clues, so the task is to count how many orders satisfy all of them.
Input
The first line contains two space-separated integers () and (). Here is the number of comedy acts and is the number of clues. The acts are denoted by the first uppercase letters.
Each of the next lines contains a clue as three space-separated values , , and , where . The letters and are distinct uppercase letters among the first letters. The value gives the type of restriction:
- : the act of comes before the act of .
- : the act of comes after the act of .
- : the acts of and do not occur consecutively, in either order.
The clues are not contradictory, and at least one valid ordering exists.
Output
Print the number of different orders of the comedy acts that satisfy all the clues.