ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836
因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设(i,j)的位置是U,
以U为边,连接点i和点j+n,也即连接行点和列点,最大匹配为n则必定有解,否则必定无解
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#define clr(x,y) memset(x, y, sizeof x)
#include <cmath>
using namespace std;
const int maxn=6e2+6;
const int maxm=maxn*maxn*2;
int first[maxn];
struct edge{
int nxt,t,f;
}e[maxm]; void addedge(int f,int t,int ind){
e[ind].nxt=first[f];
e[ind].t=t;
e[ind].f=f;
first[f]=ind;
}
int n;
char maz[maxn][maxn]; bool vis[maxn];
int match[maxn];
bool dfs(int f){
vis[f]=true;
for(int p=first[f];p!=-1;p=e[p].nxt){
int t=e[p].t;
int mch=match[t];
if(mch==-1||(!vis[mch]&&dfs(mch))){
match[t]=f;
match[f]=t;
return true;
}
}
// printf("dfs %d no\n",f);
return false;
}
int findmatch(){
int ans=0;
for(int i=0;i<n;i++){
if(match[i]==-1){
clr(vis,0);
if(dfs(i))ans++;
}
}
return ans;
}
void init(){
clr(first,-1);
clr(match,-1);
}
int main(){
//freopen("input.txt","r",stdin);
while(scanf("%d",&n)==1){
init();
int en=0;
for(int i=0;i<n;i++)scanf("%s",maz[i]);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(maz[i][j]=='U'){
addedge(i,j+n,en++);
addedge(j+n,i,en++);
}
}
}
int ans=findmatch();
if(ans==n){
puts("YES");
}
else {
puts("NO");
}
}
return 0;
}
ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2的更多相关文章
- zoj 1002 Fire Net (二分匹配)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
- fafu 1568 Matrix(二分匹配+二分)
Description: You are given a matrix which <= n <= m <= ). You are supposed to choose n el ...
- ZOJ 3156 Taxi (二分匹配+二分查找)
题目链接:Taxi Taxi Time Limit: 1 Second Memory Limit: 32768 KB As we all know, it often rains sudde ...
- UVALive 5903 Piece it together 二分匹配,拆点 难度:1
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- zoj 2362 Beloved Sons【二分匹配】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...
- POJ-1274The Perfect Stall,二分匹配裸模板题
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23313 Accepted: 103 ...
- ZOJ 1654 二分匹配基础题
题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...
- UVALive 6525 Attacking rooks 二分匹配 经典题
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...
- HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- [转载] 深入理解 docker ulimit
深入理解docker ulimit 2015年7月23日 10:00 阅读 12778 [编者的话]Docker大规模应用后,如果你没踩过坑,说出去肯定没人信.昨天就遇到一个ulimit的经典问题:业 ...
- UML建模
1.包含<<include>> 包含是指当多个用例中存在相同的事件流时,可以把这些公共事件流抽象成公共用例,这个公共用例称之为抽象用例(跟类的概念有点相像,类是多个对象的抽象定 ...
- 【Todo】LR-逻辑回归
因为逻辑回归对于计算广告学非常重要.也是我们平时广告推荐.CTR预估最常用到的算法.所以单独开一篇文章讨论. 参考这篇文章:http://www.cnblogs.com/sparkwen/p/3441 ...
- Android开发面试经——4.常见Android进阶笔试题(更新中...)
Android开发(29) 版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http:/ ...
- 本地获取System权限CMD方法汇总(转)
本地获取System权限CMD方法汇总(转) 稍微整理了下,大概有三种方法可以本地获取system权限的cmd,但前提都是当前用户具备administrator权限. 下面列举的三种方法各有千秋,看你 ...
- 传大附件在iis7以上的设置
IIS7下设置上传附件大小的限制,下载附件限制问题 最近项目中涉及到一个上传附件的问题,项目在本地运行时上传无论多大的附件都是没有问题,但是一旦部署到服务器上以后上传的附件超过一定大小就上传不成功 ...
- 在linux(CentOS-6.7_x86_64)上安装mysql成功记录
查看linux服务器的yum源设置: [root@hadoop03 yum.repos.d]# cd /etc/yum.repos.d [root@hadoop03 yum.repos.d]# ll ...
- VS2012给同一个解决方案添加多个项目
1.选择文件->添加->新建项目或现有项目 2.接下来在解决方案资源管理器中我们会发现解决方ConsoleApplication1中有两个项目,这里一个是类库项目ClassLibrary1 ...
- Django数据库设置
设置数据库,创建您的第一个模型,得到一个简单介绍 Django的自动生成管理网站. 数据库设置 现在,打开 mysite / settings.py . 这是一个普通的Python模块 模块级变量代表 ...
- [mondrian] 快速入门
一.下载地址 mondrian各个版本和工具等的下载地址: http://sourceforge.net/projects/mondrian/files/?source=navbar 注意:mondr ...