二分图最大匹配

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

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

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

  2. UVaLive 6525 Attacking rooks (二分图最大匹配)

    题意:给定一个 n * n的图,X是卒, . 是空位置,让你放尽量多的车,使得他们不互相攻击. 析:把每行连续的 . 看成X集体的一个点,同理也是这样,然后求一个最大匹配即可. 代码如下: #prag ...

  3. The 2013 South America/Brazil Regional Contest 题解

    A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...

  4. BUPT2017 wintertraining(15) #3 题解

    我觉得好多套路我都不会ヘ(;´Д`ヘ) 题解拖到情人节后一天才完成,还有三场没补完,真想打死自己.( ˙-˙ ) A - 温泉旅店 UESTC - 878  题意 ​ 有n张牌,两人都可以从中拿出任意 ...

  5. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  6. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  7. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  8. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  9. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

随机推荐

  1. c++11:copy_n

    copy_n: Copies exactly count values from the range beginning at first to the range beginning at resu ...

  2. android的入门学习

    android 入门学习. 活动:就是一个包含应用程序的用户界面的窗口.目的就是与用户交互. 意图:就是能够将来自不同应用程序的不同活动无缝连接在一起工作的"胶水",确保这些任务执 ...

  3. PHP 网站保存快捷方式的实现代码

    介绍下使用PHP实现网站快捷方式的保存方法. PHP怎么实现网站保存快捷方式呢?下面是一段PHP代码,下面这段代码,可以PHP实现网站保存快捷方式,以便用户随时浏览.  <?php  /** * ...

  4. java递归方法

    一个方法体内调用他自身,称为方法递归. 方法递归是一种隐式的循环,Tahiti重复执行某段代码,但这种重复执行无需循环控制 /* Author:oliver QIN DATE:2015-12-19 D ...

  5. LINQ.CS

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Zdso ...

  6. 关于Oracle表空间数据文件自增长的一些默认选项

    昨天,一个同事请教了一些关于Oracle表空间数据文件自增长的问题,解答过程中顺便整理起来,以后其他同事有同样的疑问时可以直接查阅. 实验内容: 创建MYTEST表空间,默认不开启自增长. 给MYTE ...

  7. Unity 优化

    1. 尽量避免每帧处理比如: function Update() { DoSomeThing(); } 可改为每5帧处理一次: function Update() { == ) { DoSomeThi ...

  8. php操作mysqli(示例代码)

    <?php define("MYSQL_OPEN_LOGS",true); class mysqliHelp { private $db; public function _ ...

  9. inputstream与其他格式的转换

    1.InputStream 转换成InputSource . InputStream inputStream = request.getInputStream(); InputSource input ...

  10. C++中的快速排序(使用vector和数组的不同)

    1.快速排序是最最基本的排序算法之一,时间复杂度是O(nlog2(n)) 基本思想:分治法+递归 假设key为该序列的第一个元素,从后往前遍历,找到第一个小于key值的元素,将该元素赋值给左边的起始值 ...