BZOJ 1059 [ZJOI2007]矩阵游戏 (二分图最大匹配)
1059: [ZJOI2007]矩阵游戏
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 5281 Solved: 2530
[Submit][Status][Discuss]
Description
Input
Output
输出文件应包含T行。对于每一组数据,如果该关卡有解,输出一行Yes;否则输出一行No。
Sample Input
2
0 0
0 1
3
0 0 1
0 1 0
1 0 0
Sample Output
Yes
【数据规模】
对于100%的数据,N ≤ 200
HINT
Source
析:对于同行或者是同列的数,那么无论经过多少次变化,那么肯定也是同行同列,那么也就是求能不能找到 n 个不同不同列的1。也就是二分图的最大匹配。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 400 + 10;
const int maxm = 3e5 + 10;
const ULL mod = 3;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int to, next;
};
Edge edges[maxn*maxn<<1];
int head[maxn], cnt; void addEdge(int u, int v){
edges[cnt].to = v;
edges[cnt].next = head[u];
head[u] = cnt++;
} int match[maxn];
bool vis[maxn]; bool dfs(int u){
vis[u] = 1;
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to, w = match[v];
if(w < 0 || !vis[w] && dfs(w)){
match[u] = v;
match[v] = u;
return true;
}
}
return false;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
ms(head, -1); cnt = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j){
int x; scanf("%d", &x);
if(x) addEdge(i, j+n), addEdge(j+n, i);
}
int ans = 0; ms(match, -1);
for(int i = 0; i < n; ++i) if(match[i] < 0){
ms(vis, 0); if(dfs(i)) ++ans;
}
puts(ans == n ? "Yes" : "No");
}
return 0;
}
BZOJ 1059 [ZJOI2007]矩阵游戏 (二分图最大匹配)的更多相关文章
- bzoj 1059: [ZJOI2007]矩阵游戏 二分图匹配
1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1891 Solved: 919[Submit][Statu ...
- bzoj 1059: [ZJOI2007]矩阵游戏 [二分图][二分图最大匹配]
Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N *N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行 ...
- BZOJ 1059: [ZJOI2007]矩阵游戏( 匈牙利 )
只要存在N个x, y坐标均不相同的黑格, 那么就一定有解. 二分图匹配, 假如最大匹配=N就是有解的, 否则无解 ------------------------------------------- ...
- BZOJ 1059 [ZJOI2007]矩阵游戏
1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2707 Solved: 1322[Submit][Stat ...
- bzoj 1059 [ZJOI2007]矩阵游戏(完美匹配)
1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2993 Solved: 1451[Submit][Stat ...
- BZOJ 1059: [ZJOI2007]矩阵游戏 匈牙利算法
1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2351 Solved: 1156 题目连接 http:// ...
- 【bzoj1059】[ZJOI2007]矩阵游戏 二分图最大匹配
题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换 ...
- BZOJ 1059 [ZJOI2007]矩阵游戏:二分图匹配
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1059 题意: 给你一个n*n的01矩阵. 你可以任意次地交换某两行或某两列. 问你是否可以 ...
- 1059: [ZJOI2007]矩阵游戏 二分图匹配
https://www.lydsy.com/JudgeOnline/problem.php?id=1059 裸的二分图匹配,行列匹配即可 /****************************** ...
随机推荐
- vue路由传值方式
打印this.$route显示结果: 跳转路由传递参数如下 this.$router.push({ name: 'Page', query/params: { key: value }) <ro ...
- 关于map::erase的使用说明
C++ 中经常使用的容器类有vector,list,map.其中vector和list的erase都是返回迭代器,但是map就比较不一样. 当在循环体中使用map::erase语句时,为了能够在任何机 ...
- RSA加密遇到的一个问题
1,最近在项目里面使用了RSA加密解密的功能 出现的异常情况是加解密时对于有中文的情况会出现乱码,导致无法正常解析参数 解决方案人认为:针对中文应该 先encode ,这样能有效的避免乱码
- sqlite c#
https://www.cnblogs.com/icebutterfly/p/7850689.html https://www.cnblogs.com/sdadx/p/7127098.html
- oracle 单表导出导入
exp username/password@服务名 file=d:\daochu.dmp tables=(tableneme,...)
- hdu 1495 (搜索) 非常可乐
http://acm.hdu.edu.cn/showproblem.php?pid=1495 搜索模拟出每此倒得情况就好,详情见代码\ (好困啊!!!!1) #include<cstdio> ...
- BZOJ1935或洛谷2163 [SHOI2007]园丁的烦恼
BZOJ原题链接 洛谷原题链接 很容易想到二维前缀和. 设\(S[i][j]\)表示矩阵\((0, 0)(i, j)\)内树木的棵数,则询问的矩形为\((x, y)(xx, yy)\)时,答案为\(S ...
- BZOJ3191或洛谷2059 [JLOI2013]卡牌游戏
BZOJ原题链接 洛谷原题链接 我们可以倒着来\(DP\). 设\(f[i][j]\)表示剩余\(i\)个人,从庄家数起第\(j\)个人的胜率,设当前枚举到第\(k\)张牌,该情况下这一轮淘汰的位置为 ...
- BZOJ1059或洛谷1129 [ZJOI2007]矩阵游戏
BZOJ原题链接 洛谷原题链接 通过手算几组例子后,很容易发现,同一列的\(1\)永远在这一列,且这些\(1\)有且仅有一个能产生贡献,行同理. 所以我们可以只考虑交换列,使得每一行都能匹配一个\(1 ...
- BZOJ1233 干草堆 - 单调队列优化DP
问题描述: 若有干个干草, 分别有各自的宽度, 要求将它们按顺序摆放, 并且每层的宽度不大于 它的下面一层 , 求最多叠几层 题解: zkw神牛证明了: 底边最短, 层数最高 证明: ...