2623: Matrix Games
Time limit: 1 Sec Memory Limit: 64 MBsubmitted: 15 Solution: 9
[ submit ] [ state ] [proposition man: ADMIN ]
Title Description
Small Q is a very bright child, in addition to chess, he also liked to play a computer puzzle game - Matrix Games. In a game matrix N * N matrix for black and white (in general as chess, but the color is arbitrary). Two operations may be performed each time the matrix:
Objective of the game, i.e. through several operations, such that the main diagonal of matrix (left to bottom right connection) are black on a lattice.
For some points, a small Q puzzling that he began to suspect that these levels are not simply no solution! ! The little Q decided to write a program to determine whether these levels solvable.
- Line exchange operation: Select any two rows of the matrix, the exchange of two lines (i.e., corresponding to the color grid exchange)
- Column exchange: Select an arbitrary row and column matrix of two rows exchange (i.e., exchange of color corresponding to the lattice)
Objective of the game, i.e. through several operations, such that the main diagonal of matrix (left to bottom right connection) are black on a lattice.
For some points, a small Q puzzling that he began to suspect that these levels are not simply no solution! ! The little Q decided to write a program to determine whether these levels solvable.
Entry
The first row contains an integer T, represents a group of data.
Next T sets of data comprising, a first line data of each integer N, the size of the square; a next behavior N N * N matrix 01 (0 represents white, 1 black).
Next T sets of data comprising, a first line data of each integer N, the size of the square; a next behavior N N * N matrix 01 (0 represents white, 1 black).
Export
It contains T lines. For each set of data, if the points solvable, Yes output line; otherwise, the output line No.
Sample input
2
2
0 0
0 1
3
0 0 1
0 1 0
1 0 0
Sample Output
No
Yes
prompt
Data for 20%, N ≤ 7
data for 50%, N ≤ 50
Data for 100%, N ≤ 200
#include <bits/stdc++.h> //#define scan(x) scanf("%d",&x); //#define scan2(x,y) scanf("%d%d",&x,&y); //#define rg register int #define lowbit(x) x&(-x) using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const ll INF = 1e18; const int M = 1e3 + 6; const int maxn = 6e2 + 10; int T,n; int o[M][M],e[maxn]; int use[maxn]; inline bool dfs(int cur){ for(register int i=1;i<=n;++i){ if(o[cur][i]&&!use[i]){ use[i]=1; if(e[i]==-1||dfs(e[i])){ e[i]=cur; return true; } } } return false; } inline void solve(){ for(register int i=1;i<=n;++i)e[i]=-1; int res=0; for(register int i=1;i<=n;++i){ memset(use,0,sizeof(use)); if(dfs(i))++res; } if(res==n)puts("Yes"); else puts("No"); } int main() { #ifndef ONLINE_JUDGE freopen("splay.txt", "r", stdin); #endif scanf("%d",&T); while(T--){ scanf("%d",&n); for(register int i=1;i<=n;++i){ for(register int j=1;j<=n;++j){ scanf("%d",&o[i][j]); } } solve(); } return 0; }