UVALive 5903 Piece it together 二分匹配,拆点 难度:1
对L研究可以发现
相当于黑-横白,黑-纵白,每个黑白都要被匹配到,其中黑的横纵各两次,
很自然的想到拆点,黑点拆成专门和横白连接的,专门和纵白连接的,
如果2*黑==白数,运行一次二分匹配算法,匹配数==白数则为可行,剩下的都不可行
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
char maz[600][601];
int ind[600][600];
int n,m;
bool inmaz(int x,int y){return 0<=x&&x<n&&0<=y&&y<m;}
struct pnt{
int x,y;
pnt(){x=y=0;}
pnt(int x,int y){this->x=x;this->y=y;}
};
pnt pw[600*600],pb[600*600];
int lenw,lenb;
int e[2*600*600][8];
int len[2*600*600]; int mch[2*600*600];
bool used[2*600*600]; bool dfs(int s){
used[s]=true;
for(int i=0;i<len[s];i++){
int t=e[s][i];
if(mch[t]==-1||(!used[mch[t]]&&dfs(mch[t]))){
mch[t]=s;
mch[s]=t;
return true;
}
}
return false;
} int match(){
memset(mch,-1,sizeof(int)*2*lenw);
int res=0;
for(int i=0;i<lenw*2;i++){
if(mch[i]==-1){
memset(used,false,2*lenw);
if(dfs(i)){
res++;
}
}
}
return res;
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
lenb=lenw=0;
memset(len,0,sizeof(len));
memset(ind,0,sizeof(ind));
for(int i=0;i<n;i++)scanf("%s",maz[i]);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(maz[i][j]=='W'){
ind[i][j]=lenw;
pw[lenw++]=pnt(i,j);
}
else if(maz[i][j]=='B'){
ind[i][j]=lenb;
pb[lenb++]=pnt(i,j);
}
}
}
if(lenw!=lenb*2){
puts("NO");
continue;
}
for(int i=0;i<lenb;i++){
int x=pb[i].x,y=pb[i].y;
for(int j=0;j<2;j++){
int txj=x+dx[j],tyj=y+dy[j];
if(!inmaz(txj,tyj)||maz[txj][tyj]!='W')continue;
int indj=ind[txj][tyj],hor=lenw+2*i+1; e[indj][len[indj]++]=hor;
e[hor][len[hor]++]=indj;
}
for(int k=2;k<4;k++){
int txk=x+dx[k],tyk=y+dy[k];
if(!inmaz(txk,tyk)||maz[txk][tyk]!='W')continue;
int indk=ind[txk][tyk],gra=lenw+2*i;
e[indk][len[indk]++]=gra;
e[gra][len[gra]++]=indk;
}
}
int res=match();
if(res!=lenw)puts("NO");
else puts("YES");
}
return 0;
}
UVALive 5903 Piece it together 二分匹配,拆点 难度:1的更多相关文章
- UVALive 5903 Piece it together(二分图匹配)
给你一个n*m的矩阵,每个点为'B'或'W'或'.'.然后你有一种碎片.碎片可以旋转,问可否用这种碎片精确覆盖矩阵.N,M<=500 WB <==碎片 W 题目一看,感觉是精确覆盖(最近 ...
- UVALive - 7427 the math 【二分匹配】
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 5903 Piece it together
一开始用的STL一直超时不能过,后来发现AC的代码基本都用的普通邻接表,然后改了一下13s,T=T,效率太低了.然后把某大神,详情戳链接http://acm.hust.edu.cn/vjudge/pr ...
- ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设 ...
- UVALive 6525 Attacking rooks 二分匹配 经典题
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...
- kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树
二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...
- HDU-4619 Warm up 2 二分匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 一看就知道是二分匹配题目,对每个点拆点建立二分图,最后答案除2.因为这里是稀疏图,用邻接表处理. ...
- ZOJ 1654 二分匹配基础题
题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...
- HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧
题意: 每一个' . '有一个姑娘, E是出口,'.'是空地 , 'X' 是墙. 每秒钟每一个姑娘能够走一步(上下左右) 每秒钟每一个出口仅仅能出去一个人 给定n*m的地图, 时限T 问全部姑娘是否能 ...
随机推荐
- mysql 数据操作 单表查询 where约束 between and or
WHERE约束 where字句中可以使用: 比较运算符:>< >= <= != between 80 and 100 值在80到100之间 >=80 <= ...
- Spark ListenerBus 和 MetricsSystem 体系分析
转载自:https://yq.aliyun.com/articles/60196 摘要: Spark 事件体系的中枢是ListenerBus,由该类接受Event并且分发给各个Listener.Met ...
- PAT 1117 Eddington Number [难]
1117 Eddington Number (25 分) British astronomer Eddington liked to ride a bike. It is said that in o ...
- 454 Authenti cation failed, please open smtp flag first! (Net::SMTPAuthenticationError)
在用ruby的smtp库发送邮件的时候,出现了这个错误454 Authenti cation failed, please open smtp flag first! (Net::SMTPAuthen ...
- try with resources简洁的异常捕获机制
通过前篇的<Java文件IO流的操作总结>,我们知道了基本输入输出流的使用方式,但是每次都需要在finally处关闭流资源,这样操作起来既啰嗦又麻烦,有没有更简洁的方式呢?本篇就来讲解jd ...
- ng-深度学习-课程笔记-7: 优化算法(Week2)
1 Mini-batch梯度下降 在做梯度下降的时候,不选取训练集的所有样本计算损失函数,而是切分成很多个相等的部分,每个部分称为一个mini-batch,我们对一个mini-batch的数据计算代价 ...
- centos设置代理上网
centos设置代理上网 假设我们要设置代理为 IP:PORT 1.网页上网 网页上网设置代理很简单,在firefox浏览器下 Edit-->>Preferences-->> ...
- map.containsKey
该方法判断Map集合对象中是否包含指定的键名.如果Map集合中包含指定的键名,则返回true,否则返回false. 语法 containsKey(Object key) . e.g public s ...
- 20145104张家明 《Java程序设计》第4周学习总结
20145104张家明 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 1.继承的定义及目的 面向对象中,子类继承父类,避免重复的行为定义.不过并非为了避免重复定义行为就使用 ...
- github上fork别人的代码之后,如何保持和原作者同步的更新
1.从自己fork之后的版本库clone $ git clone -o chucklu https://github.com/chucklu/Hearthstone-Deck-Tracker.git ...