1059: [ZJOI2007] Matrix game time limit:10 Sec Memory limit:162 MB
submit:2154 solved:1053
[Submit] [Status] 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
Sum: ... The first success in this life of the real meaning of the network flow problem (Phile: Today and the life of the dry? Hansbug: Nasty *^_t*), the problem because everywhere is the row Exchange column exchange, so get to get all the "1" point of the horizontal axis or so a few numbers, ordinate is also, so the problem becomes from all the black lattice to choose from N, and the horizontal ordinate each different, Therefore, the Networkflow can be modeled-the horizontal axis 1-n is established as (n+1), the ordinate is established as (n+2)-(2*n+1), the source point is 1, the meeting point is (2*n+2), for the black spot in the checkerboard (x, y), in the Network Diagram, (n+x)-(N+1+y) There is a forward edge of 1, and then the source point to each of the horizontal points of each of the edges, the various ordinate points to the sink point is also, and then the network flow is (this will be divided into two points set and then find a match called binary map matching, can also be done with the Hungarian algorithm, and better, unfortunately I will not TT), as long as the maximum flow = n means you can do it, otherwise you can't, that's all ... (only with a gap optimization even the adjacency table is useless sap incredibly 612ms I was drunk *_*)
1 var2 I,j,k,l,m,n,aug,jl,mi,tmp,ans,vi,vx:longint;3 Flag:boolean;4A:Array[0.. -,0.. -] ofLongint;5DI,DIS,HIS,PRE,VH:Array[0.. +] ofLongint;6 functionMax (x,y:longint): Longint;7 begin8 ifX>y ThenMax:=xElsemax:=y;9 End;Ten functionmin (x,y:longint): Longint; One begin A ifX<y ThenMin:=xElsemin:=y; - End; - begin the READLN (VX); - forvi:=1 toVx Do - begin - + READLN (n); -Fillchar (A,sizeof (a),0); + fori:=1 toN Do A begin ata[1, i+1]:=1; -a[n+1+i,n*2+2]:=1; - End; - fori:=1 toN Do - begin - forj:=1 toN Do in begin - read (k); toa[1+i,n+1+j]:=K; + End; - Readln; the End; *Fillchar (dis,sizeof (DIS),0); $Fillchar (His,sizeof (HIS),0);Panax NotoginsengFillchar (vh,sizeof (VH),0); - fori:=1 toN2+2 Do thedi[i]:=1; +vh[0]:=n*2+2; Aaug:=Maxlongint; thei:=1; ans:=0; + whiledis[1]< (n2+2) Do - begin $flag:=false; $his[i]:=; - forJ:=di[i] toN2+2 Do - begin the if(a[i,j]>0) and((dis[i]-1) =dis[j]) Then - beginWuyiaug:=min (aug,a[i,j]); thepre[j]:=i; -di[i]:=J; Wui:=J; - ifI= (2*n+2) Then About begin $ans:=ans+; - whileI<>1 Do - begin -tmp:=i; Ai:=Pre[i]; +a[i,tmp]:=a[i,tmp]-; thea[tmp,i]:=a[i,tmp]+; - End; $aug:=Maxlongint; the End; theflag:=true; the Break ; the - End; in End; the ifFlag Thencontinue; thejl:=-1; mi:=2*n+1; About forj:=1 to 2*n+2 Do the begin the if(DIS[J]<MI) and(a[i,j]>0) Then the begin +mi:=Dis[j]; -jl:=J; the End;Bayi End; thedi[i]:=JL; the Dec (vh[dis[i]]); - ifvh[dis[i]]<=0 ThenBreak ; -dis[i]:=mi+1; the Inc (Vh[dis[i]); the ifI<>1 Then the begin thei:=Pre[i]; -aug:=His[i]; the End; the End; the ifAns=n ThenWriteln ('Yes')ElseWriteln ('No');94 the End; the End. the
1059: [ZJOI2007] Matrix game