bzoj1059题解
【解题思路】
因为只要验证可行性,所以考虑行和考虑列是等价的,故我们只考虑行的交换操作。
这样,拆一波点,把每一行拆成两个点,左边为原交换行,右边为目标交换行,原问题等价于能否对这个二分图进行完全匹配,题中给定邻接矩阵即为二分图的邻接矩阵。
于是直接匈牙利一波,复杂度O(n3)。
【参考代码】
#include <cctype>
#include <cstdio>
#define REP(I,low,high) for(register int I=(low);I<=(high);I++)
#define PER(I,high,low) for(register int I=(high);I>=(low);I--)
inline int getint()
{
char ch=getchar();
for(;!isdigit(ch)&&ch!='+'&&ch!='-';ch=getchar());
bool impositive=ch=='-';
if(impositive)
ch=getchar();
int result=;
for(;isdigit(ch);ch=getchar())
result=(result<<)+(result<<)+ch-'';
return impositive?-result:result;
}
template<typename integer> inline int write(integer n)
{
integer now=n;
bool impositive=now<;
if(impositive)
{
putchar('-');
now=-now;
}
char sav[];
sav[]=now%+'';
int result=;
for(;now/=;sav[result++]=now%+'');
PER(i,result-,)
putchar(sav[i]);
return result+impositive;
}
//Header Template
#include <cstring>
bool used[],map[][];
int n,result[];
bool find(int S)
{
REP(T,,n)
if(map[S][T]&&!used[T])
{
used[T]=true;
if(!result[T]||find(result[T]))
{
result[T]=S;
return true;
}
}
return false;
}
int main()
{
for(int T=getint();T--;)
{
n=getint();
REP(i,,n)
REP(j,,n)
map[i][j]=getint();
memset(result,,sizeof(result));
int ans=;
REP(i,,n)
{
memset(used,,sizeof(used));
ans+=find(i);
}
puts(ans==n?"Yes":"No");
}
return ;
}
bzoj1059题解的更多相关文章
- BZOJ1059:[ZJOI2007]矩阵游戏——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1059 https://www.luogu.org/problemnew/show/P1129 小Q是 ...
- BZOJ1059 [ZJOI2007]矩阵游戏 二分图匹配 匈牙利算法
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1059 题意概括 有一个n*n(n<=200)的01矩阵,问你是否可以通过交换整行和整列使得左 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- 【bzoj1059】 ZJOI2007—矩阵游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=1059 (题目链接) 题意 一个01矩阵,可以任意交换两行或两列,问能否经过若干次交换后使主对角线全为 ...
随机推荐
- vue指令系统
一.vue基础 使用vue需在官网上先下载vue.js,网址:https://cn.vuejs.org/v2/guide/installation.html.然后: 在project中引入vue.js ...
- vue组件参数校验与非props特性
组件参数校验 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- DenyHosts 安全限制ssh防暴力破解
DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP ...
- js 加载验证码
<img id="captchaPic" src="{{captcha_src('math')}}" onclick="this.src='{{ ...
- springboot输出hello world,3种方式(String,JSON,jsp),IDEA开发工具
新建项目: File -> new -> Project -> Spring Initializr -> Next -> Next -> Next-> Pro ...
- OpenSearch最新功能介绍
摘要:阿里云开放搜索(OpenSearch)是一款结构化数据搜索托管服务,其能够提供简单.高效.稳定.低成本和可扩展的搜索解决方案.OpenSearch以平台服务化的形式,将专业搜索技术简单化.低门槛 ...
- lvs+keepalived+mariadb集群
1.环境准备节点1:172.16.2.95节点2:172.16.2.160节点3:172.16.2.220LVS1:172.16.2.67LVS2:172.16.2.234 2.mariadb集群的安 ...
- Linux环境下安装PHP的mbstring模块
cd /home/local/php-5.6.25/ext/mbstring/usr/local/php/bin/phpize./configure --with-php-config=/usr/lo ...
- 4-基于DoG的特征检测子(SIFT:稳定性好,实时性差)
opencv实现 详细原理:https://blog.csdn.net/u010440456/article/details/81483145
- linux浏览器,邮件客户端,输入法,双屏设置,应用软件,gnome-screenshot/scrot -s截图,office
搜狗输入法linux版:http://pinyin.sogou.com/linux/help.php win/linux同时支持比较好用的浏览器:maxthon,firefox,maxthon,ope ...