Ways of Seeing

4.5

13 votes
Medium-Hard
Problem

Ways of Seeing

"The relation between what we see and what we know is never settled. Each evening we see the sun set. We know that the earth is turning away from it. Yet the knowledge, the explanation, never quite fits the sight"
~ John Berger, Ways of Seeing.

At the Smithsonian Museum, the ledger storing information of all the paintings has been stolen.
Larry Daley, the night guard, has to do damage control, or else he'll be fired the next morning!

There are M boxes and N paintings in the museum, each box comprising some non-negative number of paintings. A painting can have more than one copy, so it can be present in more than one box. Each box contains paintings of either the Renaissance Era, or the Neoclassical Era.

There are some 'interesting' paintings in the museum. A painting is 'interesting' if it belongs to both the Renaissance and the Neoclassical Era, indicating that it was probably made during the transition period between the two eras.

For some boxes, you already know which era it belongs to. The museum administration wants to minimize the number of interesting paintings. Larry has to assign an era to the remaining unassigned boxes, such that the number of interesting paintings at the Smithsonian is minimized.

Note that assigning an era to box would assign the era to all the paintings in that box. As a painting can be present in multiple boxes, it can be assigned multiple eras. It is interesting if it is assigned both eras at least once.

Can you help Larry out by minimizing the number of interesting paintings after assigning an era to each unassigned box?

Input Format :

First line contains T, the number of test cases.
First line of each test contains N (number of paintings) and M (number of boxes).
Next line contains M integers, where each integer {0,1,+1}
1 shows that box belongs to Renaissance Era.
1 shows that box belongs to Neoclassical Era.
0 shows that box is unassigned any Era.
Next M lines contain N integers each, where each integer is either 0 or 1.
If jth integer in ith line i.e.contains[i][j]=1, it means that ith box has a copy of jth painting present in it.

Output Format:

Output the minimum number of interesting paintings after assigning eras to all unassigned boxes in a single line for each test case.

Constraints :

1T100
1N100
1M100
contains[i][j] {0,1}

Subtasks :

For 5 points, M15
For 10 points, count of unassigned boxes 1
For 20 points, N10

Sample Input
2
2 2
-1 0
0 1
1 1
3 4
0 1 0 -1
1 0 1
0 0 1
0 1 1
1 1 0
Sample Output
0
1
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For first sample, only box 2 is unassigned any era.

If we assign box 2 '1' era, then for each painting, set of assigned eras are:
Painting 1 : Eras {1}
Painting 2 : Eras {1}
Interesting Paintings = {}

If we assign box 2 '1' era, then for each painting, set of assigned eras are:
Painting 1 : Eras {1}
Painting 2 : Eras {1,+1}
Interesting Paintings = {Painting 2}

Thus, assigning box 2 as '1' era results in minimum number of interesting paintings.

For second sample, optimal answers is assigning box 1 a '1' era and box 3 a '1' era.
After this, the set of interesting paintings : {Painting 3}.

Editor Image

?