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.
Medium7BacktrackingGraphDivide and conquerNo attempts yetTime limit5sMemory limit512 MBThe elves are holding a single elimination tournament for 2N elves. Before it starts, the elves receive distinct ID numbers from 1 to 2N, 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 2N−1 elves who lost leave the line, the 2N−1 elves who won stay where they are, and round 2 pairs up the remaining elves in the same way. After N rounds a single elf is left, and that elf wins the tournament.
M of the elves are sensitive. The sensitive elf with ID number Ei has a list of Bi friends and becomes sad if that elf has to play a match against one of those friends during the first Ki rounds. Friendship does not have to be mutual: elf a may count elf b as a friend while elf b does not count elf a 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.
The first line has the number of test cases T. Each test case starts with a line holding two integers N and M. After that come M groups of two lines each. The first line of a group has three integers Ei, Ki and Bi, and the second line has the Bi ID numbers of the friends of elf Ei.
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.