Elf Tournament Lineup
Time limit5sMemory limit512 MB
Decide whether 2^N elves can be lined up for a knockout tournament so every sensitive elf avoids its listed friends through the first K rounds.
- Level
Medium7 of 10
- Topics
- Backtracking, Graph, Divide and conquer
- Solved
- No attempts yet
Problem
The elves are holding a single elimination tournament for elves. Before it starts, the elves receive distinct ID numbers from to , and the elf president lines all of them up in an order of the president's choosing.
Every match is played by two elves and ends with one winner and one loser. There are no draws. In round 1 the 1st and the 2nd elf in the line play each other, the 3rd and the 4th play each other, and so on. The elves who lost leave the line, the elves who won stay where they are, and round 2 pairs up the remaining elves in the same way. After rounds a single elf is left, and that elf wins the tournament.
of the elves are sensitive. The sensitive elf with ID number has a list of friends and becomes sad if that elf has to play a match against one of those friends during the first rounds. Friendship does not have to be mutual: elf may count elf as a friend while elf does not count elf as a friend.
The president wants to fix the starting order so that no elf becomes sad, no matter how the matches turn out. Decide whether such an order exists.
Input
The first line has the number of test cases . Each test case starts with a line holding two integers and . After that come groups of two lines each. The first line of a group has three integers , and , and the second line has the ID numbers of the friends of elf .
Limits
- , and the values are all different
- every friend ID is between and , differs from , and appears at most once in the list of elf
Output
For each test case, print one line containing "Case #x: ", where x is the test case number starting from 1, followed by YES if the president can line the elves up so that no elf ever becomes sad, and NO otherwise.