Notice how to change it, but if you put a black square on the diagonal, the black squares of its original row will not work.
So to choose the N group of not heavy row and column combination, here with the Hungarian algorithm to do two-figure matching (with time stamp optimization)
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int N=405;int t,n, cnt,h[n],lk[n],v[n],ti;struct qwe{int ne,to;} E[n*n];int read () {int r=0,f=1; Char P=getchar (); while (p> ' 9 ' | | p< ' 0 ') {if (p== '-') f=-1; P=getchar (); } while (p>= ' 0 ' &&p<= ' 9 ') {r=r*10+p-48; P=getchar (); } return r*f;} void Add (int u,int v) {cnt++; E[cnt].ne=h[u]; E[cnt].to=v; h[u]=cnt;} bool Dfs (int u) {for (int i=h[u];i;i=e[i].ne) if (v[e[i].to]!=ti) {v[e[i].to]=ti; if (!lk[e[i].to]| | DFS (Lk[e[i].to])) {lk[e[i].to]=u; return 1; }} return 0;} int main () {t=read (); while (t--) {cnt=0;ti=0; memset (h,0,sizeof (h)); memset (v,0,sizeof (v)); memset (lk,0,sizeof (LK)); N=read (); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {int x=read (); if (x) Add (I,J); } int con=0; for (int i=1;i<=n;i++) {ti++; if (Dfs (i)) con++; } if (Con==n) puts ("Yes"); Else puts ("No"); } return 0;}
Bzoj 1059: [ZJOI2007] Matrix game "Hungarian algorithm"