Elf Tournament Bracket
Time limit5sMemory limit512 MB
Decide if up to 8 elves can be ordered so no sensitive elf meets a listed friend within its first K rounds under any match outcomes.
- Level
Medium5 of 10
- Topics
- Brute force, Implementation
- Solved
- No attempts yet
Problem
The elf country is holding an elimination tournament, and elves want to take part. When the tournament starts, each elf receives a distinct ID number from 1 to , and the elf president lines them all up in an order of their own choosing.
A match is played by two elves, and every match produces one winner and one loser. There are no draws. In round 1 the first elf in the line plays the second elf, the third elf plays the fourth elf, and so on. After round 1 the elves that lost leave the line, and the elves that won stay in their original relative order. The remaining elves then play round 2 the same way. After rounds one elf is left, and that elf wins the tournament.
of the elves are sensitive, which means they become very sad when they have to play against a friend. Precisely, the sensitive elf becomes sad if it plays against one of its friends in any of rounds 1 through . Friendship can go one way only. One elf may treat another elf as a friend while the other one does not.
Decide whether the president can fix a starting order that leaves no elf sad, whatever the results of the matches turn out to be.
Input
The first line has the number of test cases . The first line of each test case has two integers and . The sensitive elves follow, two lines each. The first line has three integers , , , and the second line has the ID numbers of the elves that elf treats as friends.
Limits
- , and the values are all different.
- , and the ID numbers on one line are different from each other and from .
Output
For each test case, print Case #x: on one line, then YES if a starting order that satisfies the condition exists and NO otherwise. Here is the test case number, starting from 1.