Elf Tournament Bracket

Time limit5sMemory limit512 MB

Summary
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 2N2^N elves want to take part. When the tournament starts, each elf receives a distinct ID number from 1 to 2N2^N, 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 2N−12^{N-1} elves that lost leave the line, and the 2N−12^{N-1} elves that won stay in their original relative order. The remaining elves then play round 2 the same way. After NN rounds one elf is left, and that elf wins the tournament.

MM of the elves are sensitive, which means they become very sad when they have to play against a friend. Precisely, the sensitive elf EiE_i becomes sad if it plays against one of its friends in any of rounds 1 through KiK_i. 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 TT. The first line of each test case has two integers NN and MM. The MM sensitive elves follow, two lines each. The first line has three integers EiE_i, KiK_i, BiB_i, and the second line has the BiB_i ID numbers of the elves that elf treats as friends.

Limits

  • 1≤T≤2001 \le T \le 200
  • 1≤N≤31 \le N \le 3
  • 0≤M≤2N0 \le M \le 2^N
  • 1≤Ei≤2N1 \le E_i \le 2^N, and the MM values EiE_i are all different.
  • 1≤Ki≤N1 \le K_i \le N
  • 1≤Bi1 \le B_i, and the BiB_i ID numbers on one line are different from each other and from EiE_i.
  • M≤B1+B2+⋯+BM≤min⁡(2M,2N)M \le B_1 + B_2 + \cdots + B_M \le \min(2M, 2^N)

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 xx is the test case number, starting from 1.

Examples1

  1. Example 1

    Input
    3
    1 1
    1 1 1
    2
    2 2
    1 1 1
    2
    3 1 1
    4
    3 3
    1 2 2
    3 4
    2 2 2
    5 6
    7 1 1
    8
    
    Expected output
    Case #1: NO
    Case #2: YES
    Case #3: YES