[ZJOI2007]矩阵游戏
题目描述:
给你一个N*N的矩阵,0代表白色,1代表黑色,你可以任意交换矩阵的两行或两列,问交换若干次能否使得方针的主对角线的颜色均为黑色
思路:
首先思考一下交换行和交换列的目的
交换行,假设该行最终交换到了第 i 行,也就是代表该行的第 i 个元素必须是黑色,当然这行不一定只有第 i 个是黑色,也就是说他可以换到任意一个黑色对应的行,举个例子:假如该行 3 5 7的位置是黑色,那最后代表他可以换到3 5 7行去
这样其实就不用交换列了,因为换行已经解决了
这时候左集合是行,右集合是列,算最大匹配数是不是n即可
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<sstream>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define endl '\n'
#define inf 0x3f3f3f3f
#define MAX 500 + 50
#define mod 1000000007
#define lowbit(x) (x & (-x))
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d %d",&n,&m)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d\n",n, m)
#define sddd(n,m,z) scanf("%d %d %d",&n,&m,&z)
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
typedef long long ll ;
typedef unsigned long long ull;
//不开longlong见祖宗!提交不看数据范围见祖宗!
inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;}
int t, n, m, x, y;
int tr[MAX][MAX];
bool vis[MAX];
int link[MAX];
bool dfs(int x){
for(int i = 1; i <= n; ++i){
if(!vis[i] && tr[x][i]){
vis[i] = 1;
if(!link[i] || dfs(link[i])){
link[i] = x;
return true;
}
}
}
return false;
}
bool work(){
for(int i = 1; i <= n; ++i){
mem(vis, 0);
if(!dfs(i))return false;
}
return true;
}
int main(){
cin>>t;
while (t--){
mem(link, 0);
sd(n);
m = n;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
sd(tr[i][j]);
}
}
if(work())cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}

京公网安备 11010502036488号