Description
Q is a very smart child. In addition to chess, he also enjoys playing a computer puzzle game, matrix game. Matrix games are played in an N * n black/white matrix (just like playing chess, the color is random ). You can perform two operations on the matrix each time: Row exchange operation: select any two rows of the matrix and exchange the two rows (that is, swap the color of the corresponding grid) column exchange operation: select any row and column of the Matrix, and exchange the two columns (that is, swap the color of the corresponding grid) to the game's target, that is, through several operations, the main diagonal line of the matrix (the line from the upper left to the lower right corner) the grid on is black. Q cannot solve certain levels, so that he began to doubt whether these levels are completely unsolvable !! Therefore, Xiao Q decided to write a program to determine whether these levels were resolved.
Input
The first line contains an integer T, indicating the number of data groups. Next, the data contains t groups. The first behavior of each group is an integer N, indicating the size of the square matrix. The next n behavior is an 01 matrix of N * n (0 indicates white, and 1 indicates black ).
Output
The output file should contain t rows. For each group of data, if the level is resolved, a row of yes is output; otherwise, a row of no is output.
Sample Input
2
2
0 0
0 1
3
0 0 1
0 1 0
1 0 0
Sample output
No
Yes
[Data scale]
For 20% of the data, n ≤ 7
For 50% of the data, n ≤ 50
For 100% of data, n ≤ 200
Idea: Here is a very classic idea. There are two rows or two columns in the matrix, and the relationship between the two grids in the same row or the same column remains unchanged, therefore, the number of the same row or the same column is obviously only on the primary diagonal. Therefore, this question becomes the number of different rows and columns in the grid. After creating the rows and columns, we can clearly see that, this problem is equivalent to the maximum matching of the graph, so it is valid only when the maximum matching number is N.
# Include <stdio. h>
# Include <iostream>
# Include <queue>
# Include <string. h>
# Include <algorithm>
# Define maxn9000
# Define maxm 500090
# Define ESP 0.001
Using namespace STD;
Int head [maxn], now, point [maxm], next [maxm], Match [maxn];
Bool visit [maxn];
Void add (int x, int y)
{
Next [++ now] = head [x];
Head [x] = now;
Point [now] = y;
}
Int DFS (int K)
{
For (INT I = head [k]; I; I = next [I]) if (! Visit [point [I])
{
Int u = point [I];
Visit [u] = 1;
If (Match [u] =-1 | DFS (Match [u])
{
Match [u] = K;
Return 1;
}
}
Return 0;
}
Int main ()
{
Int t, n, x;
Scanf ("% d", & T );
While (t --)
{
Now = 0;
Memset (Head, 0, sizeof (head ));
Int flag = 0;
Scanf ("% d", & N );
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= N; j ++)
{
Scanf ("% d", & X );
If (x = 1) Add (I, j );
}
Memset (match,-1, sizeof (MATCH ));
For (INT I = 1; I <= N; I ++)
{
Memset (visit, 0, sizeof (visit ));
If (! DFS (I ))
{
Printf ("NO \ n ");
Flag = 1; break;
}
}
If (flag = 0) printf ("Yes \ n ");
}
Return 0;
}
Bzoj1059: [zjoi2007] matrix game [bipartite graph matching]