Case of Two Bipartite Graphs

0

0 votes
Maximum Bipartite Matching, Graph, Medium-Hard, Bipartite graph
Problem

You will be given two Bipartite Graphs, each with N vertices. The vertices in both the graphs are labeled from 1 to N.

You have to find the maximum size of a set S of vertices, such that it forms an Independent Set in the first graph, and the subgraph induced by S in the second graph has a perfect matching.

Input Format

The first line will contain an integer T, denoting the number of test cases that follow.

For each of the test cases, the first line will contain an integer N denoting the number of vertices in the two graphs

The next N lines will contain N integers each, denoting the adjacency matrix A1 of the first graph. The jth value in the ith row will represent the value of A1(i,j), and it is guaranteed that each value of A1 is either 0 or 1, and A1(i,j)=A1(j,i).

The next N lines will again contain N integers each, this time denoting the adjacency matrix A2 of the second graph, in a similar fashion as A1.

It is guaranteed that A1(i,i)=0 and A2(i,i)=01iN and both A1 and A2 represent adjacency matrices of a bipartite graph.

Output Format

For every test case, output the maximum size of the set S.

Constraints

1T3

1N35

Time Limit: 4
Memory Limit: 256
Source Limit:
Explanation

The two graphs from the sample are shown below. Note that you cannot pick a set S of size greater than 2.

One such set S is 1,2 since it forms an independent set in Graph A and the subgraph induced by 1,2 forms a perfect matching in Graph B

Graphs from Sample

Editor Image

?