1059: [ZJOI2007] Matrix game time limit:10 Sec Memory limit:162 MB
submit:2447 solved:1206
[Submit] [Status] [Discuss] Description
Little Q is a very clever kid, besides chess, he also likes to play a computer puzzle game-matrix game. The matrix game is carried out in a n*n black-and-white phalanx (as in chess, except that the color is random). You can do two operations on the Matrix at a time: Select any two rows of the matrix, swap the columns of the two lines (that is, the color of the swap grid): Select any column of the matrix, swap the two columns (that is, swap the color of the corresponding lattice) the target of the game, that is, by several operations, the main diagonal The upper-left corner of the line to the lower-right corner of the grid is black. For some of the levels, the small q can not be solved, so that he began to wonder whether these levels are simply no solution!! So little Q decided to write a program to determine whether these levels have a solution.
Input
The first line contains an integer t, which represents the number of groups of data. Next contains the T-group data, each set of data first behaves as an integer n, representing the size of the Phalanx; next n behaves as a n*n 01 matrix (0 for White and 1 for black).
Output
The output file should contain a T line. For each set of data, if the level has a solution, the output line is yes;
Sample Input2
2
0 0
0 1
3
0 0 1
0 1 0
1 0 0Sample OutputNo
Yes
"Data Size"
For 100% of data, n≤200HINT
The direct Hungarian algorithm is embarrassing.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath > #include <cstdlib> #include <algorithm>using namespace Std;int n,mp[205][205],mark[205],lnk[205]; bool Dfs (int x) {for (int i=1;i<=n;i++) {if (Mark[i]==-1&&mp[x][i]) {mark[i]=1; if (lnk[i]==-1| | DFS (Lnk[i])) {lnk[i]=x; return true; }}} return false;} BOOL Check () {for (int i=1;i<=n;i++) {memset (mark,-1,sizeof (Mark)); if (!dfs (i)) return false; } return true; int main () {int TT; scanf ("%d", &TT); while (tt--) {scanf ("%d", &n); memset (Lnk,-1,sizeof (LNK)); for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) scanf ("%d", &mp[i][j]); } if (check ()) printf ("yes\n"); else printf ("no\n"); } return 0;}
(Hungarian algorithm) Bzoj 1059