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的更多相关文章

  1. zoj 1002 Fire Net (二分匹配)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  2. fafu 1568 Matrix(二分匹配+二分)

    Description:   You are given a matrix which <= n <= m <= ). You are supposed to choose n el ...

  3. ZOJ 3156 Taxi (二分匹配+二分查找)

    题目链接:Taxi Taxi Time Limit: 1 Second      Memory Limit: 32768 KB As we all know, it often rains sudde ...

  4. UVALive 5903 Piece it together 二分匹配,拆点 难度:1

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. zoj 2362 Beloved Sons【二分匹配】

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...

  6. POJ-1274The Perfect Stall,二分匹配裸模板题

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23313   Accepted: 103 ...

  7. ZOJ 1654 二分匹配基础题

    题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...

  8. UVALive 6525 Attacking rooks 二分匹配 经典题

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...

  9. 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 ...

随机推荐

  1. apt-get下载的文件

    1. http://kurenai.elastos.org/2013/05/02/ubuntu-apt-get%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86/ http:// ...

  2. Linux命令之nslookup

    http://www.computerhope.com/unix/unslooku.htm About nslookup The nslookup command is used to query i ...

  3. hostapd源代码分析(一):网络接口和BSS的初始化

    [转]hostapd源代码分析(一):网络接口和BSS的初始化 原文链接:http://blog.csdn.net/qq_21949217/article/details/46004349 最近在做一 ...

  4. Android布局_相对布局RelativeLayout

    一.RelativeLayout(相对布局)概述 RelativeLayout是相对布局控件,它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列 二.RelativeLayo ...

  5. IOS开发证书变成“此证书的签发者无效”解决方法

    IOS开发证书全部变成无效,如下图  打包提示错误  解决方法: 1. 下载https://developer.apple.com/certificationauthority/AppleWWDRCA ...

  6. 配置Java EE Eclipse+Tomcat开发环境

    以下将详细介绍在Eclipse下搭建Java EE开发环境的每一步, 环境:Win 7 + JDK 1.7 + Eclipse IDE for Java EE Developers 3.7 +Tomc ...

  7. jstree级联查找树

    <script > obj=$.jstree.reference($('#plugins4')); function PP(obj,e,filename){ if (obj.get_par ...

  8. Unity5中叹为观止的实时GI效果

    http://www.manew.com/thread-43970-1-1.html 今天为大家分享unity与Alex Lovett共同使用unity5制作的Shrine Arch-viz Demo ...

  9. Farseer.Net

    Farseer.Net V0.2 ORM开源框架 目录 http://www.cnblogs.com/steden/archive/2013/01/22/2871160.html V1.0教程:htt ...

  10. jquery通过ajax方法获取json数据不执行success

    1.jquery通过ajax方法获取json数据不执行success回调 问题描述:jquery通过ajax方法获取json数据不执行success回调方法 问题原因:json格式存在问题或不符合标准 ...