二分图最大匹配

#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. vi 命令行模式功能键

    目录 目录内容 I 切换到插入模式,此时光标位于开始输入文件处 A 切换到插入模式,并从目前光标所在位置的下一个位置开始输入文字 O 切换到插入模式,并从行首开始插入行的一行 [ctrl]+[b] 屏 ...

  2. Ueditor 1.4.3 单独调用上传图片,或文件功能

    第一步, 引入文件 <script src="ueditor/ueditor.config.js" type="text/javascript" char ...

  3. Git tricks: Unstaging files

    NOTE: Following content is directly reprinted from http://andrewberls.com/blog/post/git-tricks-unsta ...

  4. Using Git Submodules

    NOTE: Following content is directly reprinted from http://patrickward.com/2013/01/09/using-git-submo ...

  5. UIKit,Core Data , Core Graphics, Core Animation,和OpenGLES框架

    iOS的主要框架介绍   框架是一个目录,这个目录包含了共享库,访问共享库里代码的头文件,和其它的图片和声音的资源文件.一个共享库定义的方法或函数可以被应用程序调用. IOS提供了很多你可以在应用程序 ...

  6. 2.servlet 生命周期

    1.建Web project“2Servlet_Basic” 2.建包com.amaker.servlet 3.建类“ServletBasic.java” package com.amaker.ser ...

  7. html5 app开发重大消息-腾讯在技术端推进Html5生态发展

    中新网5月3日电 日前,腾讯正式发布腾讯浏览服务(Tencent Browser Service,以下简称TBS),宣布为合作伙伴提供整合腾讯底层技术.内容框架.广告体系以及大数据等多方面能力的升级浏 ...

  8. Array和ArrayList的异同点【转】

    相信数组是大家在编程最常使用的,不论任何语言都存在数组这样的数据结构,由于C#语言是完全面向对象的,所以在C#中的数组也是对象,实际上就是Array类的实例,Array类的使用可以说是使用最频繁的,只 ...

  9. cppcheck使用

    一.splint介绍 splint是一个动态检查C语言程序安全弱点和编写错误的程序.splint会进行多种常规检查,包括未使用的变量,类型不一致,使用未定义变量,无法执行的代码,忽略返回值,执行路径未 ...

  10. 【rest】 深入理解rest

    起因是想搞明白 ajax.rest风格和http请求数据会有什么区别 再来回顾一下概念: REST即表述性 状态 传递 满足这些约束条件和原则的应用程序或设计就是RESTful.需要注意的是,REST ...