This page is still under construction.

Parts of this page are still being built. What you see may change.

Cheating a Boolean Tree

Interview

Time limit2sMemory limit512 MB

Summary
In a tournament-style Boolean tree, flip the fewest changeable AND/OR gates so the root evaluates to V, or report it impossible.
Level

Medium5 of 10

Topics
Tree, Dynamic programming
Solved
No attempts yet

Problem

A boolean tree is a kind of binary tree. It has an odd number of nodes numbered 1 through MM. Nodes 1 through (M−1)/2(M-1)/2 are internal nodes, and node ii has node 2i2i and node 2i+12i+1 as its children. Nodes (M+1)/2(M+1)/2 through MM are leaf nodes.

Node values work like this. A leaf node holds 0 or 1. An internal node is either an AND gate or an OR gate. The picture below shows one boolean tree.

Example of a boolean tree

The value of a subtree follows directly. If the root of the subtree is a leaf node, the value of that leaf is the value of the subtree. If the root of the subtree is an internal node, the value of the subtree is what you get by combining the value of the left subtree and the value of the right subtree with that node's gate. For example, the subtree rooted at node 2 in the picture above has this value.

Val[2] = Val[4] AND Val[5] = (Val[8] OR Val[9]) AND 1 = (0 OR 1) AND 1 = 1

We want the value of the whole tree, that is Val[1], to be VV. The value VV is 0 or 1 and comes from the input. The tree may currently evaluate to something else, so you are allowed to change the gate of some internal nodes. You can turn an AND gate into an OR gate and an OR gate into an AND gate. The nodes drawn in blue in the picture above are the ones you can change.

Given the boolean tree and which internal nodes you can change, find the minimum number of gates you have to change so that the whole tree evaluates to VV.

Input

The first line contains the number of test cases TT (1≤T≤201 \le T \le 20).

Each test case has this form.

  • The first line contains MM and VV. MM is the number of nodes of the boolean tree and is odd. VV is the value you want the whole tree to have. (1≤M≤100001 \le M \le 10000, VV is 0 or 1)
  • The next (M−1)/2(M-1)/2 lines contain aia_i and bib_i, the information of the internal nodes, in order starting from node 1. aia_i is the gate of that node, where 0 means an OR gate and 1 means an AND gate. bib_i tells whether the gate of that node can be changed: 1 means it can be changed, 0 means it cannot.
  • The next (M+1)/2(M+1)/2 lines contain aia_i, the value of each leaf node, in order starting from node (M+1)/2(M+1)/2.

Output

For each test case, print one line in the form Case #c: x. Here cc is the test case number, counted from 1. xx is the minimum number of gates you have to change so that the whole tree evaluates to VV. If no combination of gate changes makes the whole tree evaluate to VV, print IMPOSSIBLE in place of xx.

Examples6

  1. Example 1

    Input
    2
    9 1
    1 0
    1 1
    1 1
    0 0
    1
    0
    1
    0
    1
    5 0
    1 1
    0 0
    1
    1
    0
    
    Expected output
    Case #1: 1
    Case #2: IMPOSSIBLE
    
  2. Example 2

    Input
    3
    1 1
    1
    1 0
    1
    1 0
    0
    
    Expected output
    Case #1: 0
    Case #2: IMPOSSIBLE
    Case #3: 0
    
  3. Example 3

    Input
    1
    7 1
    0 0
    1 0
    0 0
    1
    0
    0
    1
    
    Expected output
    Case #1: 0
    
  4. Example 4

    Input
    1
    7 0
    0 0
    0 0
    0 0
    1
    0
    0
    0
    
    Expected output
    Case #1: IMPOSSIBLE
    
  5. Example 5

    Input
    20
    5 0
    1 0
    0 1
    1
    1
    0
    5 1
    0 0
    1 1
    0
    0
    1
    13 1
    1 1
    0 0
    1 0
    1 1
    0 1
    0 0
    0
    0
    1
    1
    1
    0
    0
    7 1
    1 1
    0 0
    0 0
    0
    1
    1
    0
    9 1
    1 1
    1 1
    1 0
    0 1
    0
    1
    0
    0
    1
    5 0
    1 1
    1 1
    0
    1
    0
    15 1
    0 0
    0 0
    0 1
    1 1
    0 0
    0 1
    0 0
    0
    0
    1
    1
    1
    1
    1
    1
    5 0
    1 0
    0 0
    1
    0
    0
    9 0
    1 0
    1 1
    0 1
    0 0
    1
    0
    1
    0
    1
    15 0
    1 0
    0 0
    0 0
    0 1
    1 0
    1 1
    0 0
    0
    1
    1
    1
    0
    1
    1
    0
    3 1
    0 0
    1
    0
    7 0
    1 1
    0 0
    0 0
    0
    0
    1
    0
    3 1
    0 1
    1
    0
    11 0
    0 0
    1 0
    1 1
    1 0
    0 0
    1
    1
    1
    0
    1
    1
    15 1
    0 0
    1 1
    0 0
    1 0
    0 0
    0 0
    1 0
    1
    0
    0
    0
    0
    0
    0
    0
    11 0
    0 1
    1 0
    0 0
    1 0
    1 0
    1
    1
    0
    1
    0
    1
    11 0
    1 0
    1 1
    0 1
    0 1
    0 0
    0
    1
    1
    1
    0
    0
    5 0
    1 0
    0 0
    0
    0
    1
    3 1
    0 1
    1
    1
    15 1
    0 1
    0 0
    1 1
    0 1
    1 0
    1 0
    1 0
    1
    0
    1
    1
    1
    0
    1
    0
    
    Expected output
    Case #1: 1
    Case #2: 1
    Case #3: 1
    Case #4: 0
    Case #5: 2
    Case #6: 0
    Case #7: 0
    Case #8: 0
    Case #9: 1
    Case #10: IMPOSSIBLE
    Case #11: 0
    Case #12: 0
    Case #13: 0
    Case #14: IMPOSSIBLE
    Case #15: IMPOSSIBLE
    Case #16: 1
    Case #17: 0
    Case #18: 0
    Case #19: 0
    Case #20: 0
    
  6. Example 6

    Input
    1
    3 1
    1 1
    1
    0
    
    Expected output
    Case #1: 1