矩阵游戏 - 黑暗爆炸 1059 - Virtual Judge (csgrandeur.cn)
又是一个抽象二分图的题。
因为经过行变换或列变换后相对位置不改变,故最终起作用的是行与列都不相同的1
故我们可以用纵坐标和横坐标建立二分图求出最大匹配,若是n则说明可以。
代码如下:
这里直接用邻接矩阵存图还是比较方便的。
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cstdio>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
int mp[210][210];
int vis[210];
int match[210];
int viss[210][210];
int n;
bool find(int x)
{
for (int i = 1; i <= n; i++)
{
if (mp[x][i] && !vis[i])
{
vis[i] = 1;
if (!match[i] || find(match[i]))
{
match[i] = x;
return true;
}
}
}
return false;
}
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
cin >> n;
memset(match, 0, sizeof match);
memset(viss, 0, sizeof viss);
memset(mp, 0, sizeof mp);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
int x;
cin >> x;
if (x)
mp[i][j] = 1;
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
memset(vis, 0, sizeof vis);
if (find(i))
ans++;
}
if (ans == n)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}