UVALive 6525
二分图最大匹配
#include<cstdio>
#include<iostream>
#include<cstring>
#define MAX 10010 using namespace std ; int n, t, n1, n2, g1[1100][1100], g2[1100][1100];
char s[110][110]; bool visit[MAX];
int match[MAX];
int head[MAX]; struct edge
{
int to,next;
}e[300005]; void addedge(int u,int v)
{
e[++t].to=v;
e[t].next=head[u];
head[u]=t;
} void init ( )
{
t = n1 = n2 = 0;
memset(head, 0, sizeof(head));
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
if(s[i][j] == '.') g1[i][j] = (s[i][j] != s[i][j-1]) ? ++ n1 : g1[i][j-1];
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
if(s[i][j] == '.') g2[i][j] = (s[i][j] != s[i-1][j]) ? ++ n2: g2[i-1][j];
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
if(s[i][j] == '.') addedge(g1[i][j], g2[i][j]);
}
bool dfs(int u)
{
int i,v;
for(i = head[u]; i != 0; i = e[i].next)
{
v = e[i].to;
if(!visit[v])
{
visit[v] = true;
if(match[v] == -1 || dfs(match[v]))
{
match[v] = u;
return true;
}
}
}
return false;
}
int MaxMatch()
{
int i,sum=0;
memset(match,-1,sizeof(match));
for(i = 1 ; i <= n1 ; ++i)
{
memset(visit,false,sizeof(visit));
if( dfs(i) )
{
sum++;
}
}
return sum;
}
int main ( )
{
while(scanf("%d", &n) == 1)
{
for(int i = 1; i <= n; ++ i)
scanf("%s", s[i]+1);
init();
int ans = MaxMatch();
printf("%d\n", ans);
}
return 0 ;
}
UVALive 6525的更多相关文章
- UVALive 6525 Attacking rooks 二分匹配 经典题
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...
- UVaLive 6525 Attacking rooks (二分图最大匹配)
题意:给定一个 n * n的图,X是卒, . 是空位置,让你放尽量多的车,使得他们不互相攻击. 析:把每行连续的 . 看成X集体的一个点,同理也是这样,然后求一个最大匹配即可. 代码如下: #prag ...
- The 2013 South America/Brazil Regional Contest 题解
A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...
- BUPT2017 wintertraining(15) #3 题解
我觉得好多套路我都不会ヘ(;´Д`ヘ) 题解拖到情人节后一天才完成,还有三场没补完,真想打死自己.( ˙-˙ ) A - 温泉旅店 UESTC - 878 题意 有n张牌,两人都可以从中拿出任意 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- 20150301—ASP.NET的Repeater
Repeater与GridView等数据列表一样,都是用来显示数据库的信息的,其中Repeater是最基本的列表形式,其用法也比较灵活. 一.Repeater的位置: 工具箱-数据-Repeater ...
- hibernate 一对多映射关系
1. 单项多对一映射 custom(顾客)与order(订单) :一个顾客可以有多个订单,一个订单只能有一个顾客 配置方法:在多的一端配置<many -to one& ...
- 将string转换成char*
string 是c++标准库里面其中一个,封装了对字符串的操作 把string转换为char* 有3中方法: 1.data 如: string str="abc"; ch ...
- mysql快速上手3
上一章给大家说的是数据库的视图,存储过程等等操作,这章主要讲索引,以及索引注意事项,如果想看前面的文章,url如下: mysql快速上手1 mysql快速上手2 索引简介 索引是对数据库表中一个或多个 ...
- 4.Servlet_Form表单处理
1.建项目"3Servlet_Form",src下建包“com.amaker.servlet”,web-root下建Register.html <!DOCTYPE html& ...
- linux 网桥的配置与实现
==================================================================================from: http://www.i ...
- 第一行代码 Android 第二版到货啦
今日android第一行代码[第二版]已到,收获的季节到了 先看一下封面 书签: 以后就把空闲时间送给它吧 先来看一下本书的目录: 第1章 开始启程--你的第1行Android代码 第2章 先从看得到 ...
- Ubuntu16.04.1 安装Redis-Cluster
Redis在3.0版正式引入了集群这个特性.Redis集群是一个分布式(distributed).容错(fault-tolerant)的 Redis内存K/V服务, 集群可以使用的功能是普通单机 Re ...
- [大牛翻译系列]Hadoop(9)MapReduce 性能调优:理解性能瓶颈,诊断map性能瓶颈
6.2 诊断性能瓶颈 有的时候作业的执行时间会长得惊人.想靠猜也是很难猜对问题在哪.这一章中将介绍如何界定问题,找到根源.涉及的工具中有的是Hadoop自带的,有的是本书提供的. 系统监控和Hadoo ...
- java 语法糖
package syntax.autoCase; import java.util.Arrays; import java.util.List; public class autoCase { pub ...