比较明显的二分图匹配
一行内如果没有被染色格子的话怎样变换都是不行的
一列内也是这样
所以以行和列建二分图,能完美匹配就是YES ,否则就是NO
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200+10;
int T, n;
int g[maxn][maxn];
int used[maxn];
int line[maxn][maxn];
int other_side[maxn];
bool Find(int x)
{
for(int i = 1 ; i <= n ; i++)
{
if(line[x][i] && !used[i])
{
used[i] = 1;
if(other_side[i] == 0 || Find(other_side[i]))
{
other_side[i] = x;
return 1;
}
}
}
return 0;
}
void solve()
{
int sum = 0;
for(int i = 1 ; i <= n ; i++)
{
memset(used,0,sizeof(used));
if(Find(i))
{
sum++;
}
}
if(sum >= n) cout << "Yes" << endl;
else cout << "No" << endl;
}
void init()
{
memset(line,0,sizeof(line));
memset(other_side,0,sizeof(other_side));
memset(used,0,sizeof(used));
}
int main()
{
ios::sync_with_stdio(false);
cin >> T;
while(T--)
{
init();
cin >> n;
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= n ; j++)
{
cin >> line[i][j];
}
}
solve();
}
return 0;
}