Bzoj 1059 [ZJOI2007] Matrix game

Source: Internet
Author: User

1059: [ZJOI2007] Matrix game time limit:10 Sec Memory limit:162 MB
submit:2707 solved:1322
[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 Source

Puzzle: Nest Meng can think: no matter how the transformation, the same column or the same row of the relative relationship is not changed. Then the question is turned into the ability to find n points that are different rows or columns in each other. In fact, let each horizontal axis to find an ordinate, is a binary map matching.

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <stack>6#include <queue>7#include <cstring>8 #definePAU Putchar (")9 #defineENT Putchar (' \ n ')Ten using namespacestd; One Const intmaxn= $+Ten, maxm=50000+Ten; A structted{intX,Y;TED*NXT;} adj[maxm],*fch[maxn],*ms=adj; - voidAddintXintY) {*ms= (Ted) {x,y,fch[x]};fch[x]=ms++;return;} - intN,LNK[MAXN];BOOLVIS[MAXN]; theInlineintRead () { -     intx=0;BOOLsig=1;CharCh=GetChar (); -      for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=0; -      for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0'; +     returnsig?x:-x; - } +InlinevoidWriteintx) { A     if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; at     intlen=0, buf[ -]; while(x) buf[len++]=x%Ten, x/=Ten; -      for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; - } - BOOLMatchintx) { -      for(ted*e=fch[x];e;e=e->NXT) { -         intv=e->y;if(!Vis[v]) { invis[v]=true;if(!lnk[v]| | Match (Lnk[v])) {lnk[v]=x;return true;} -         } to}return false; + } - BOOLHungary () { thememset (LNK,0,sizeof(LNK)); *      for(intI=1; i<=n;i++){ $memset (Vis,false,sizeof(VIS));if(!match (i))return false;Panax Notoginseng}return true; - } the intT; + intMain () { At=read (); the      while(t--){ +Ms=adj;memset (Fch,null,sizeof(FCH)); -n=read (); $          for(intI=1; i<=n;i++) $              for(intj=1; j<=n;j++) -                 if(Read ()) Add (i,j); -         if(Hungary ()) puts ("Yes"); the         ElsePuts"No"); -     }Wuyi     return 0; the}

Bzoj 1059 [ZJOI2007] Matrix game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.