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 ...
随机推荐
- poj1927Area in Triangle
链接 物理渣只能搜题解了.. 分三种情况 1.len>=a+b+c 2.len<内切圆半径 圆的面积是最大的 -->以len为周长的圆 3.看这篇http://blog.sina.c ...
- android模拟器中文乱码
问题:在xml文件中设置的中文能正确输出,但是在java文件中设置的中文会在模拟器上呈现乱码 解决方案:在build.gradle文件中添加一行代码 android {compileOptions.e ...
- 【服务器环境搭建-Centos】jdk的安装
1.查看是否已安装openjdk 使用rpm命令查看是否已安装openjdk[root@linuxidc ~]# rpm -qa | grep java tzdata-java-2012c-.el6. ...
- linux下在jar包中找类是否存在
find /usr/lib -name "*.jar" -exec grep -Hsli 类名 {} \;
- Request 接收参数乱码原理解析
起因: 今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下. 实际情景: 同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码: &l ...
- hiho1099_constellation
题目 一 个NxM(N, M <= 1000)的矩阵形成星空,矩阵中的点有两种字符,'#'代表星星,'.'代表空白,星空中的星星最多5000个:给出K(K<=20)个星图,每 个星图都是H ...
- vim 空格和换行的删除和替换
%s/\s//g %s/\r//g %s/\n//g 把一个很长的一行按空格分为多行 :%s/ +/\r/g简单解释一下:%s :在整个文件范围查找替换/ :分隔符+ :匹配空格,其中“ ”表 ...
- idea+git
http://www.cnblogs.com/java-maowei/p/5950930.html
- typedef void (*funcptr)(void)
定义一个函数指针类型.比如你有三个函数:void hello(void) { printf("你好!"); }void bye(void) { printf("再见!&q ...
- java 抽象类
抽象类: 1)函数没有方法体,就必须用abstract修饰. 2)抽象函数所在的类必须也是抽象的. 3)非抽象的类继承于抽象类,必须实现其全部方法. 4)抽象类中可以存在抽象方法,也可以不存在. 5) ...